File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Directory structure & getResource() problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Directory structure & getResource() problems" Watch "Directory structure & getResource() problems" New topic

Directory structure & getResource() problems

karl nilsson

Joined: Jan 20, 2005
Posts: 20
I am trying to get a program working as both application and applet, but I've hit a stumbling block: the base directory for some reason changes when running as an applet, compared to an application. This is a problem because I'm trying to access images and text files in a jar.

I added the following line in the ImageLoader file, just to see if that was changing. Lo and behold, the directory when running as an applet was "Project/bin/" but was simply "Project/" when running as an app.

Here is a basic overview of the file structure:


Basically, I'm just trying to get ImageLoader to load imgsConfig.txt as an application, and/or an applet.

I know a few possible solutions:
1) put all the files in one directory (I really don't want to do this)
2) have a flag for whether the program is running as an app or applet
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Your topic suggests that you are attempting to load imgsConfig.txt with getResource, but you have not shown us that code or what errors occur (have a look at Tell The Details for suggestions on asking good question). I would not anticipate that getResource would care what the user's current working directory is (that's what System.getProperty("user.dir") returns) as it uses the classpath to find resources.
[ February 15, 2007: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
karl nilsson

Joined: Jan 20, 2005
Posts: 20
sorry for the ambiguity of my question.

You were right, the issue isn't with the current working directory, rather it is with the getResource() method, because it doesn't seem to be able to find the resource

(this is part of

If I give the exact location of the resource, it works fine, but if I try locate it with getResource, it doesn't work. I get a nullpointer exception, which I am almost certain is from the location URL.
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
You still have not given enough info. Which directory have you added to your classpath? In the situation you describe, you would have to add the "images" directory to your classpath. If you add the "src" direcotry, the file will not be found, subdirectories are not searched.
karl nilsson

Joined: Jan 20, 2005
Posts: 20
I forgot to add the directory to the classpath, thanks for pointing it out.
I agree. Here's the link:
subject: Directory structure & getResource() problems
It's not a secret anymore!