btalbot
btalbot

Reputation: 167

HTML file not opening in executable jar

I have a program I have written in Eclipse and it runs fine -- the HTML file opens when I run the program through Eclipse. But when I create a jar file of the program, everything else runs fine except this HTML file won't open in the browser (or anywhere):

    operation.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                File htmlFile = new File("help/operation.html");
                Desktop.getDesktop().browse(htmlFile.toURI());
            } catch (MalformedURLException MURLe) {
                MURLe.printStackTrace();
            } catch (IOException ioE) {
                ioE.printStackTrace();
            }
        }
    });

The rest of the program runs fine, and my images and sounds work fine and are opened, but this HTML file will not open in the menu or with the Ctrl+key shortcut. Your help is appreciated. Thanks.

Upvotes: 0

Views: 1410

Answers (2)

afzalex
afzalex

Reputation: 8652

Suppose your project is foo. Then help/operation.html will refer to
..\abc\help\operation.html

But the deployed jar file will not contain it.
You have include this operation.html file in your source code (where you write code).
Then eclipse (or any IDE) will add it into your jar file when you deploy it.

And now you can use your file as follows. Suppose your file is present in as shown in figure. enter image description here
Now you can refer your html file from any class. In this example referring it from Accesser class.

File resFile = new File(Accesser.class.getResource("operation.html").toURI());

If you want to open your file in browser you will have to copy this file into the user's System.

File htmlFile = new File("operation.html");
if(!htmlFile.exists) {
    Files.copy(resFile.toPath(), htmlFile.toPath());
}
Desktop.getDesktop().browse(htmlFile.toURI());

Files is present in java.nio.file package

Upvotes: 0

realUser404
realUser404

Reputation: 2231

When you have a file inside your jar, you cannot access it like you are doing now. You need to read it as a stream, that's the only way.

Upvotes: 1

Related Questions