What would cause a NullPointerException when running a JAR from the command line, but not from an IDE?



I have a project in Eclipse. When I run it from inside Eclipse, everything works just fine. However, when I run the Ant build script and execute the JAR either from the command line or using a batch script, I get a NullPointerException.

The NullPointerException in question is being thrown from a third-party JAR that I compiled from source. But, I doubt that's the problem - it works when I execute it inside Eclipse!

What could be causing this and how can I go about isolating the problem and correcting it?

Here is as much of the stack trace as I can show:

        at java.io.FilterInputStream.read(Unknown Source)
        at java.io.BufferedInputStream.fill(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)
        at java.io.DataInputStream.readUnsignedByte(Unknown Source)
        at com.jhlabs.dbf.DBFFile.readHeader(DBFFile.java:129)
        at com.jhlabs.dbf.DBFFile.<init>(DBFFile.java:76)
        at com.jhlabs.map.shapefile.Shapefile.<init>(Shapefile.java:102)
        at com.jhlabs.map.layer.ShapefileLayer.<init>(ShapefileLayer.java:62)

I checked the classpath - the third party JAR is indeed on the classpath. However, I expected that, as I would most likely get a NoClassDefFoundException if it was not and I tried to utilize the classes in the JAR.

I also checked the locations where I used a classloader, and they were not null and were correctly loading the proper files.

  • Did you put the third party JAR in the build path?

    AlbertoPL16 июля 2009, 12:23
  • –Woops, hit Add Comment too quickly, and did you package it with your JAR?

    AlbertoPL16 июля 2009, 12:24

6 ответов


One guess: it's trying to find a resource using ClassLoader.getResourceAsStream() or similar, and it's not finding it in the jar.

Of course we'd be guessing rather less if you'd tell us the rest of the stack trace instead of just that it's an NPE... and the fact that you've got the source means you should be able to work out exactly where it's going wrong!

EDIT: Yes, with that stack trace it's probably creating a FilterInputStream but passing in a null stream to wrap. (The JDK should really throw an exception in the constructor, but it's too late to fix that now.)

  • Я не могу поделиться большей частью трассировки стека, но думаю, что вы, возможно, на правильном пути. Я сейчас изучаю это, но я знаю, что файлы находятся в JAR, но они могут быть не найдены должным образом.

    Thomas Owens16 июля 2009, 12:27

Вы уверены, что сторонний JAR-файл находится на вашем пути при запуске из командной строки?


I would guess that the classpath in your Eclipse run task and the classpath in your manual approach are different. Go to the Run Configurations dialog and check the classpath tab of your Eclipse run task and ensure that your batch has the same entries.


You should learn how to attach a debugger to a stand alone program, so you can attach your IDE with the source to this running program. I agree with Jon Skeet that this is most likely a file that is not found - try changing your current directly for your Run configuration to see if it breaks too then.


Looks more like the DBF file path is not resolving correctly in the latter case. The file path from where you run your class file in Eclipse is mostly different from where you are running the batch file from. (Not the class-path, but the physical path from where you are doing java -jar ..., like say, D:\my\app\bin) The path Eclipse runs from is in Run Configurations -> Arguments tab -> Working directory field.


Try to run it on JDK and not on JRE, this way you'll see on which line you receive NullPointerException, and find out what to do by looking at the source.

Also, compile it with debugging info enabled (don't remember how, see Ant javac task manual).

  • But I do think that this might help me. Especially since I don’t know what is null, and knowing that will help me fix the problems.

    Thomas Owens16 июля 2009, 12:48
  • Простите за глупость, но как мне указать запускать JAR на JDK? Я никогда раньше этого не делал.

    Thomas Owens16 июля 2009, 12:45