File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes IDEs, Version Control and other tools and the fly likes Class.getResourceAsStream() returns null in Eclipse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Class.getResourceAsStream() returns null in Eclipse" Watch "Class.getResourceAsStream() returns null in Eclipse" New topic
Author

Class.getResourceAsStream() returns null in Eclipse

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I have the following directory structure for my project.



And source similar to:

When I run from the command line using the instruction:

it works fine. However when I try to run within Eclipse, it fails with a NullPointerExceptiion because the InputStream is null. I've tried adding the lib folder to the project classpath via Project-> Properties -> Java Build Path -> Libraries -> Add Class Folder, but the same NPE occurs. Can anyone point me in the right direction?
[ July 09, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Well after much handwringing and cussing, I got it to work by adding my /lib folder as a source folder via Project-> Properties -> Java Build Path -> Libraries -> Add Source Folder. This creates a copy of my resource.txt file in the bin folder which thereby ensures that it is on the classpath when I run the program. I'm not sure if this is the only/best way, but it was the only workable solution I found so far. If there is a way to add my /lib folder directly to the classpath I'd be very interested.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

Java doesn't support implicit library inclusion, IDE or no. You need a class directory structure. Two possible ways are:

1. Denote a "precompiled class" directory, put your resource in that, and add it to the project classpath. This is effectively what you did, but "lib" isn't a good name for such a construct.

2. Add a build function to copy the resource into your compiled classes directory as part of the build process. This is often more trouble than it's worth, so I recommend solution #1.

What I mean by "precompiled class" directory is that unlike your compiled class directory, the precompiled classes (and resources - including images, property files, resource bundles, etc.) are not created as part of the build process, but are externally supplied and static. Since Java (and your IDE) allow multiple class sources, this makes it easy to combine the generated code and the static stuff without having the generate process nuke the static stuff.


Customer surveys are for companies who didn't pay proper attention to begin with.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Your best off asking this question in the Eclipse project newsgroup. You will get a lot more and lot more detailed responses.

I am not sure if you are creating an eclipse plugin? or just a java program? I am not sure where your program is run from and what the root of the classpath is?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.getResourceAsStream() returns null in Eclipse
 
Similar Threads
How to run .java file from Runnable
Calling main method of one program from another program
Read and Parse DTD doctype XML file from jar
java.lang.ClassNotFoundException: when I put my code into a package
When is an explicit close() necessary?