Re: App running from JAR can't find files



On 2006-04-18, Stephen Marjoribanks penned:
Monique Y. Mudama wrote:

Can you show us a code snippet of how you're trying to access the
html?



JEditorPane editorPane1 = new JEditorPane();
editorPane1.setEditable(false);
java.net.URL helpURL = HelpWindow.class.getResource("help.html");
if (helpURL != null)
{
editorPane1.setPage(helpURL);
}
else
{
editorPane1.setText("Cannot find help file");
}



I actually didn't include the html files in the Jar by mistake. However,
what confused me is that at other points in the application I load
images from an 'images' directory which isn't within the Jar but is in
the same directory as the Jar and they all work fine. Eg like this:

public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
Image img = new ImageIcon("images/key.jpg").getImage();
g.drawImage(img, 0, 0, this);
}


What I can't quite get my head round is why the html files aren't loaded
into my application now it is run from the Jar when they worked fine
when loaded from the same relative location when running the app as
normal from the command line.

Notice that you're not using getResource() in the ImageIcon
constructor. That means that Java is looking for that file in the
filesystem, not the jar file. That's why it works.

getResource(), on the other hand, is looking in the jar file. My
guess is that you need to include the fully qualified directory for
help.html (assuming it's not right at the top of the jar file, in
which case you might try "/help.html").

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
.



Relevant Pages

  • Re: How to update a jar file for one source file change?
    ... I'm new to java, so, I may get terminilogy wrong, bear with me. ... we've got a BIG jar file that includes thousands of classes. ... updated java source code was not being used. ...
    (comp.lang.java.programmer)
  • Re: Unable to run a Java jar on iSeries(AS/400) using command JAVA
    ... Have you tried using the Jar files on a PC? ... If the class name is com.myco.MyClass, the class file must be located at "com/myco/MyClass.class", and EXACTLY that name must be in the Jar file. ... We have to run Java programs on AS/400 and we would prefer to run it ... directly on the system with command JAVA. ...
    (comp.sys.ibm.as400.misc)
  • Re: Creating Jar files
    ... bit confused as to how to go about creating an executable jar file of my application. ... Does this mean that all I have to do is include these jar files in with my class files when creating my jar file or have I misunderstood this? ... The users JVM will search the user's class path to try to find the Xerces JAR, and if it's not there, the users JVM will throw a ClassNotFoundException. ...
    (comp.lang.java.help)
  • Re: Java kxml
    ... The compiler is telling you that the package that ... that the jar is in the CLASSPATH, check manually, by ... opening the jar or directory structure to ensure that the package does ... I have also had problems in the past where the JAR file ...
    (comp.lang.java.help)
  • Re: Get root directory of application
    ... Put it in a jar and add the jar to the classpath, ... but time consuming when I'm constantly modifying the file during development. ... getResource() works perfectly fine without jars as well. ... I used it all the time, and have no problems from within the IDE or when running my app from a jar file ...
    (comp.lang.java.programmer)