This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Hello, 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.
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 ]
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 ImageLoader.java)
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.
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.
Joined: Jan 20, 2005
I forgot to add the directory to the classpath, thanks for pointing it out.