Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: JEditorPane doesn't load html file when jar executable run

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JEditorPane doesn't load html file when jar executable run

    I'm working on a project which include a JEditorPane in which should loaded an html file from project's dir. The problem is that when I build and run the project, all works fine, and the html loads and works without any errors on JEditorPane, but when I build it(without any errors) and use the jar executable I get
    IOException->invalid URL
    instead of loading the file. All other resources(strings, images) loading fine too. This is the code for JEditorPane:

    private void bHelpActionPerformed(java.awt.event.ActionEvent evt) {                                      
        fHelp.setVisible(true);
     
        URL pageURL;
        pageURL = getClass().getResource("/com/soft/snshut/LangChooser.html");
        epHelp.setEditable(false);
        epHelp.addHyperlinkListener(new HyperlingListener());
        try {
            epHelp.setPage(pageURL);
        }catch(IOException e){
            epHelp.setText("IOExecption while loading help page.");
        }
    }

    Is something I'm doing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    Add a call to printStackTrace() to the exception's catch block to get the full text of the error message.
    Also add a println() to print out the URL to the page. Compare the value when the code works and when if fails.

    I think the getResource() method searches on the classpath for the file. Is the path in the URL on the classpath?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    I'm pretty new in java developing, so I'm not sure if I understand all that you say, but I've managed to get the printStackTrace:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at java.lang.Throwable.printStackTrace(Throwable.java:653)
            at java.lang.Throwable.printStackTrace(Throwable.java:643)
            at com.snShut.ShutdownForm.bHelpActionPerformed(ShutdownForm.java:954)
            at com.snShut.ShutdownForm.access$3800(ShutdownForm.java:59)
            at com.snShut.ShutdownForm$21.actionPerformed(ShutdownForm.java:612)
            at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
            at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
            at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
            at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
            at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
            at java.awt.Component.processMouseEvent(Unknown Source)
            at javax.swing.JComponent.processMouseEvent(Unknown Source)
            at java.awt.Component.processEvent(Unknown Source)
            at java.awt.Container.processEvent(Unknown Source)
            at java.awt.Component.dispatchEventImpl(Unknown Source)
            at java.awt.Container.dispatchEventImpl(Unknown Source)
            at java.awt.Component.dispatchEvent(Unknown Source)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
            at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
            at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
            at java.awt.Container.dispatchEventImpl(Unknown Source)
            at java.awt.Window.dispatchEventImpl(Unknown Source)
            at java.awt.Component.dispatchEvent(Unknown Source)
            at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
            at java.awt.EventQueue.access$200(Unknown Source)
            at java.awt.EventQueue$3.run(Unknown Source)
            at java.awt.EventQueue$3.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
            at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
            at java.awt.EventQueue$4.run(Unknown Source)
            at java.awt.EventQueue$4.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
            at java.awt.EventQueue.dispatchEvent(Unknown Source)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.run(Unknown Source)
    I have the file in /com/snshut/pages, but I've tried to put it directly to project's folder: /com/snshut
    and the result is the same.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    What is printed out for the value of the URL for when the code works and for when it does not work?
    Print the value of pageURL right after the call to getResource();

    What is the value of the classpath? Add the following statement after the above println() statement:
          System.out.println("classpath=" + System.getProperty("java.class.path"));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    When I run it in Netbeans this is the classpath output:
    classpath=C:\Program Files (x86)\NetBeans 7.4\java\modules\ext\AbsoluteLayout.jar;C:\Users\g\Documents\NetBeansProjects\SnShut\build\classes
    and when I run the jar from command line the classpath output is:
    classpath=snShut.jar

    Also println for URL is:
    file:/C:/Users/g/Documents/NetBeansProjects/SnShut/build/classes/com/snshut/pages/LangChooser.html
    when run works and:
    null
    when run the jar from command line.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    The getResource() method uses the classpath to find the resource.
    In the IDE the classpath included:
    C:\Users\giannis\Documents\NetBeansProjects\Snooze Shutdown\build\classes
    and the path to the file was:
    file:/C:/Users/giannis/Documents/NetBeansProjects/SnoozeShutdown/build/classes/com/d69soft/snoozeshutdown/pages/LangChooser.html

    Notice that the com folder was in the classes folder which was where the classpath pointed. getResources() appended the path given it to the classpath to find the full path to the file.

    From the commandline the classpath was only to the jar file.

    I can't remember the technique and will have to experiment.
    You might try adding a Class-Path: . entry to the jar file's manifest and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    Here is the generated manifest file:
    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.9.1
    Created-By: 1.7.0_21-b11 (Oracle Corporation)
    Class-Path: lib/AbsoluteLayout.jar
    X-COMMENT: Main-Class will be added automatically by build
    Main-Class: com.snShutn.ShutdownForm

    I tried to put the "." in place of lib/AbsoluteLayout.jar and in front of lib...(.lib), but I always get invalid or corrupt manifest file.
    I also tried to add the
    Class-Path:.
    entry without change the existing, but the result was the same.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    Sorry, I do not know how to make your IDE change the manifest file it uses.
    I tried this experiment:
    //  Test using getResource() from jar
     
    import java.net.*;
     
     
    public class TestgetResource {
     
    	public static void main(String[] args) {
    		new TestgetResource();
    	}
    	public TestgetResource() {
    	  System.out.println("classpath=" + System.getProperty("java.class.path"));
     
    	  URL pageURL = getClass().getResource("/images/duke.gif");   //  null if file not found
    	  System.out.println("url="+pageURL);
       }
    }
    /*
    //  Run from class:
    Running: java -cp . TestgetResource
     
    classpath=.
    url=file:/D:/JavaDevelopment/Testing/ForumQuestions9/images/duke.gif
     
    0 error(s)
     
    >>>>>>>>>>>>From jar with Class-Path: .        //  NOTE Classpath shows only jar BUT url good
    D:\BatchFiles>cmd.exe /K cd "D:\JavaDevelopment\Testing\ForumQuestions9"
     
    D:\JavaDevelopment\Testing\ForumQuestions9>java -jar TestgetResource.jar
    classpath=TestgetResource.jar
    url=file:/D:/JavaDevelopment/Testing/ForumQuestions9/images/duke.gif
     
    D:\JavaDevelopment\Testing\ForumQuestions9>
     
    >>>>>>>>>>>>From jar w/o Class-Path: .       //  NOTE Classpaht only jar and url null<<<<
    D:\BatchFiles>cmd.exe /K cd "D:\JavaDevelopment\Testing\ForumQuestions9"
     
    D:\JavaDevelopment\Testing\ForumQuestions9>java -jar TestgetResource.jar
    classpath=TestgetResource.jar
    url=null
     
    D:\JavaDevelopment\Testing\ForumQuestions9>
     
    */
    With this manifest file:
    Manifest-Version: 1.0
    Main-Class: TestgetResource
    Class-Path: .

    The results are shown in comments at the end of the source.
    The ForumQuestions9 folder contains the images folder. The class-path: . would point to the ForumsQuestions9 folder. I'm surprised that the java.class.path value didn't show that.

    For me, adding the Class-Path: . worked.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JEditorPane doesn't load html file when jar executable run

    Ok, thanks for your affort. I suppose I need to ask to the netbeans forum.

Similar Threads

  1. How to make an executable .jar file?
    By bdennin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 11th, 2013, 05:22 PM
  2. Cannot create executable jar file
    By viper_pranish in forum Java Theory & Questions
    Replies: 4
    Last Post: March 20th, 2013, 10:31 AM
  3. Jar Executable File Not Working
    By Kimimaru in forum Java Theory & Questions
    Replies: 6
    Last Post: October 15th, 2010, 09:32 PM
  4. Jar Executable File
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 43
    Last Post: June 23rd, 2010, 03:53 PM
  5. [SOLVED] writing html file in a jeditorpane
    By nasi in forum AWT / Java Swing
    Replies: 3
    Last Post: May 8th, 2010, 09:39 PM