File access when running program from JAR

Hello,

I'm a newcomer to Java programming trying to adapt an existing application so that it will run from within a JAR file. I've succeeded in getting the program to run from the JAR, but only in the directory where various files that it needs are located. In other words, the program is not finding many of the the files it needs within the JAR itself.

I've seen some of the posts about using getResourceAsStream and have tried using this, but still no success.

The code below uses an image file called "P17.gif" and an HTML file called "splash.html". This code works fine outside the JAR, and from in the JAR when these two files are in the same directory as the JAR. I'm assuming I have to modify the relevant lines of code somehow to tell the program to look for the two files in the JAR itself. How do I do this?

public void setup(){

Image im1=JTool.getImage("P17.gif");//extends to line 332

setIconImage(im1); //also includes local variables

//JLabel j=new JLabel("Loading");

//getContentPane().add(j);

text=new JTextPane();

//spane = new JScrollPane(text);

setContentPane(text);

text.setEditable(false);

try{

text.setPage(getURL("splash.html"));//setText("test");//

}catch(Exception e){}

pack();

show();

//ph.show();

//hide();

}

protected URL getURL(String filename) {

URL url = null;

try {

URL codeBase = new URL("file:" + System.getProperty("user.dir") + "/");

url = new URL(codeBase, filename);

} catch (java.net.MalformedURLException e) {

System.out.println(" bad URL");

return null;

}

I'll be happy to award 5 Duke Dollars to the first person who can suggest changes to make this code work properly within the JAR file!

Many thanks,

David

[1855 byte] By [dporter65] at [2007-9-27 21:18:18]
# 1
In order to do what you wanted, you need to obtain a correct url to the resources...see my response at the link shown below: http://forum.java.sun.com/thread.jsp?forum=22&thread=303644;o)V.V.
viravan at 2007-7-7 3:12:13 > top of Java-index,Desktop,Deploying...
# 2
Thanks for your reply. I'd actually seen that message and tried that approach, but it didn't work. I've decided for the time being just to leave those files outside the jar to avoid the hassle of accessing them inside it!
dporter65 at 2007-7-7 3:12:13 > top of Java-index,Desktop,Deploying...
# 3
Check out the link shown below: http://java.sun.com/docs/books/tutorial/uiswing/misc/problems.html;o)V.V.
viravan at 2007-7-7 3:12:13 > top of Java-index,Desktop,Deploying...