views:

352

answers:

6

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:

java.lang.NullPointerException
        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.

+1  A: 

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.)

Jon Skeet
I am unable to share most of the stack trace, but I think you might be on the right track. I'm looking into it now, but I do know that the files are in the JAR, but they might be not found properly.
Thomas Owens
A: 

Are you sure that the 3rd party JAR is in your path when you run from the command line?

Mike Sickler
First thing I checked.
Thomas Owens
A: 

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).

Yoni Roit
Forgive my noobishness, but how do I specify to run the JAR on the JDK? I have never done that before.
Thomas Owens
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 Owens
A: 

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.

akf
+1  A: 

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.

Narayan Raman
+1  A: 

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.

Thorbjørn Ravn Andersen