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

