World
World

Reputation: 2047

Could not open external file from jar file when it is executed from different location

I have a java application that have "Help" button. When i click the button it opens index.html in default browser.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;       

public class help {
    private static JButton jButton1;
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add button
        jButton1 = new javax.swing.JButton("help");
        // button action
        jButton1.addActionListener(
          new ActionListener()
          {
            @Override
            public void actionPerformed( ActionEvent ae )
            {
                try {
                    File directory = new File ("."); 
                    File html= new File("helpFile"+File.separator+"index.html");
                    java.awt.Desktop.getDesktop().open(html);
                    } 
                catch (IOException ex) {Logger.getLogger(help.class.getName()).log(Level.SEVERE, null, ex);}
            }
          });
        frame.getContentPane().add(jButton1);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

It works properly when i execute

java help

I packed it into a.jar file and it also works

java -jar a.jar

However, if i get out of the directory that contains a.jar file, it could not find index.html while clicking the button.

Terminal output

[world@localhost Help]$ cd ..
[world@localhost Desktop]$ java -jar Help/a.jar 
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: helpFile/index.html doesn't exist.
    at java.awt.Desktop.checkFileValidation(Desktop.java:209)
    at java.awt.Desktop.open(Desktop.java:270)
    at help$1.actionPerformed(help.java:36)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6203)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:5968)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4564)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4390)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4390)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

Upvotes: 0

Views: 1068

Answers (2)

JB Nizet
JB Nizet

Reputation: 691805

That is because you're using a relative file path in your program:

helpfile/index.html 

means: find the index.html file in the directory helpfile of the current directory.

Use an absolute file path, or use a startup script that makes sure that the program is always executed from the same directory.

Side note: you should always put your classes in a package, and never put them in the default package.

Upvotes: 1

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12054

have you tried desktop.browse( uri ); it works for me

Upvotes: 1

Related Questions