jQuery in Action, 2nd edition*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Directory structure & getResource() problems" Watch "Directory structure & getResource() problems" New topic
Author

Directory structure & getResource() problems

karl nilsson
Greenhorn

Joined: Jan 20, 2005
Posts: 20
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.



Here is a basic overview of the file structure:

Project/
-src/
--App.java
--Applet.java
--com/
---ImagePackage
----ImageLoader.java
-media/
--images/
---imgsConfig.txt

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
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

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 ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
karl nilsson
Greenhorn

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

Joined: Jan 20, 2005
Posts: 20
I forgot to add the directory to the classpath, thanks for pointing it out.
 
jQuery in Action, 2nd edition
 
subject: Directory structure & getResource() problems
 
Similar Threads
MultipartRequest syntax question
Deploying my Japplet in Netbeans
All images are showing in Eclipse, only half when exported as Runnable Jar
writing back to jar file ??
Proper location of external file for applet