How does a Java app find files it needs at runtime?

Hi. I'm relatively new to Java development. I am from a C++/C#/VB background. In VB6 I could use app.path to find the path of my application executable, which I could then use to locate files (such as images, XML docs, etc.) that are distributed with the application and would always reside in its directory. In .Net, I could use Application.StartupPath to do the same thing.

I am having trouble finding out how to do this in Java. I have heard of "getResources" or something, but how do I tell it WHERE to look for my files? I tried using a class which I found on the web somewhere (JarResources) to extract files from a Jar, but it can't find the Jar files. I want to just give it a path to show it where my Jar is, but without hard-coding anything. How does one do these kinds of things in Java?

Do you understand what I'm asking? For instance, when my app starts up, I want it to create a mouse cursor from a bitmap. How do I tell the app where to look for the bitmap? I don't want to hard-code a path, of course; I want the bitmap to reside with the application files.

If you can answer my question, thanks...

-RN

[1153 byte] By [SaintFresha] at [2007-11-26 17:13:50]
# 1
Oh, by the way, I am using NetBeans IDE. Is there something I need to set in the IDE, or can it be done entirely with code?Thanks again....
SaintFresha at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 2
Try this:URL url = getClass().getClassLoader().getResource("CursorImage.bmp");You can then use the Toolkit class to create a custom cursor.
CaptainMorgan08a at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 3

Ok, thanks Captain. But I tried this already. Where can I put the bitmap so that getResource() will find it? Should it be in the same directory as my .class file? Or in the directory with my Jar file? Or is there a way to package it into my Jar file? I really don't care how, but I just want a way to locate it without hard-coding a path.

Thanks a lot.

-RN

SaintFresha at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 4
Use this:System.out.println(getClass().getClassLoader().getResource("."));And put it in that directory.
CaptainMorgan08a at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 5

You can package the image inside the jar (and then use the getResource() method), or put it in the same directory as the jar, and use e.g. ToolKit.getDefaultToolkit().createImage("filename") or new ImageIcon("filename").getImage() to create the image.

See the sun java tutorial on jar's for more info.

Jukka

duckbilla at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 6

Ok, I tried getResource(".") and it gave me the path to my .class files. So I put my default image in there and it found it ok. Cool. But... my .class files won't get distributed with my application, right? So, when I distribute, will this location exist?

Ducky, I'll try your suggestion when I go to work tomorrow...

Thanks guys.

SaintFresha at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 7

> Ok, I tried getResource(".") and it gave me the path

> to my .class files. So I put my default image in

> there and it found it ok. Cool. But... my .class

> files won't get distributed with my application,

> right? So, when I distribute, will this location

> exist?

Your compiled .class files will most definitely be distributed with your application if you intend to run it.

duckbilla at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 8

Ok, cool. Thanks guys.

So, in general, I'm guessing that Java tends to use URL's rather than regular path-strings ("C:\Program Files\etc.") because, unlike what I wrote in VB or C#, they can be platform independent... and not all platforms use the same syntax for pathnames...

Is this a correct assumption?

(I'm just trying to get into a "Java-state-of-mind".)

SaintFresha at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 9
Both URL's and regular paths are used. See http://java.sun.com/javase/6/docs/api/java/io/File.html for info on file pathnames
duckbilla at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 10

> Do you understand what I'm asking? For instance, when

> my app starts up, I want it to create a mouse cursor

> from a bitmap. How do I tell the app where to look

> for the bitmap? I don't want to hard-code a path, of

> course; I want the bitmap to reside with the

> application files.

You can hard-code paths but that is generally not a good idea as you clearly understand.

In order to find files without hardcoding paths, you need to make your lookups relative to the class path. Then you can use getResource to get the URL, or you can just use ClassLoader.getResourceAsStream().

So let's say you have a file called 'smiley.gif' in the 'etc' folder of your jar. You can look this up using:

InputStream stream = getClass().getClassLoader().getResourceAsStream("etc/smiley.gif");

And then you can load the file from the InputStream.

NOTE: You may find that there is a Class.getResourceAsStream() method. Be aware that this method has different sematics than the Classloader version. See the JavaDocs.

Suppose however, that you wanted to replace the file with one found on the local file system. You could create a file in (Windows) in 'C:\pictures\etc\smiley.gif'. Then, you need to modify the classpath to contain 'C:/pictures/' before the reference to your jar file. Since the pat 'etc/simley.gif' is found from an entry in the classpath, the item can be found.

Now if you are using an exectuble jar, the classpath will need to be specified in the manifest. I've had trouble finding documentation clarify this, but I believe that if something is found in the executable jar, the classpath will not be searched. The 'bootclasspath' option is something I just found will looking around for this and may allow you to override things in the jar.

dubwaia at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...
# 11

Thanks very much. I have been reading about JAR files, resources, and URL's all day. It seems Java's system for doing this kind of thing is anything but concrete... which I'm suprised about, because it seems to be such a very common operation.

Thanks again, guys. I'm really starting to like Java more and more. It definitely has its weirdness, but I can always write a common-sense class to encapsulate anything weird that I don't like :-)

SaintFresha at 2007-7-8 23:41:48 > top of Java-index,Java Essentials,New To Java...