-1

Ok, so I am experimenting using a java program to open up HTML documents. So, what I have is a website already made. I have the website in a separate folder called " website" that is next to the src file. I want to then take the src folder and the website folder and put them into the same .jar file so you can then send the .jar file to any computer and when you run it, it opens the website. I'm not sure if I worded that well, so just let me know if you need more specifications.

Here is the code that opens the HTML file.

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class BrowserTest {
    public static void main(String[] args) {
        String url = "framePage.html";
        File htmlFile = new File(url);
        try {
            Desktop.getDesktop().browse(htmlFile.toURI());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

UPDATED CODE

import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

public class BrowserTest {
    public static void main(String[] args) {
        System.out.println(BrowserTest.class.getResource("/Website/framePage.html"));
        URL url = BrowserTest.class.getResource("/Website/framePage.html");


        try {
            Desktop.getDesktop().browse(url.toURI());
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
Ryan
  • 727
  • 2
  • 14
  • 31
  • Perhaps you should start by having a look at [Packaging Programs in JAR Files](https://docs.oracle.com/javase/tutorial/deployment/jar/index.html) and [Updating a JAR File](https://docs.oracle.com/javase/tutorial/deployment/jar/update.html). Mind you, you would probably find it easier if your were using something like Ant or a decent IDE, which would make the whole process a lot simpler – MadProgrammer Nov 27 '15 at 00:56
  • @MadProgrammer I use Eclipse, and it didn't seem to work – Ryan Nov 27 '15 at 01:00
  • 1
    Not entirely familiar with eclipse, but you should be able to either tell it to include the `website` directory in the jar or place it directly in the `src` directory and it should be included. You'll then need to use `BrowserTest.class.getResource("/website/framePage.html");` to generate the `URL` reference to it. – MadProgrammer Nov 27 '15 at 01:04
  • @MadProgrammer I believe that this is the closest thing to work, I just have one problem I can't seem to fix. When I make the file, it adds the Website's location to where the project is stored in eclipse. For example: `File htmlFile = new File(BrowserTest.class.getResource("/Website/framePage.html").toString());` If I do that, I get an error saying this: "Failed to open file:/C:/Users/Ryan/EclipseWorkspace/BrowserTest/file:/C:/Users/Ryan/EclipseWorkspace/BrowserTest/bin/Website/framePage.html" – Ryan Nov 27 '15 at 01:30
  • No, don't use `File`, simply use the `URL` directly. You can not treat embedded resources as files, they're not, they are now a series of bytes within a zip file – MadProgrammer Nov 27 '15 at 01:45
  • @MadProgrammer Ok, I understand what you're saying, but I must be doing something wrong (Sorry, still decently new to Java). I updated the code above. Basically, it must work to a certain point because it works in eclipse when I run it through there. However, when I export it through eclipse to a .jar file and attempt to run it, it doesn't run, and says "you'll need a new app to open this JAR". – Ryan Nov 27 '15 at 02:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96304/discussion-between-ryan-and-madprogrammer). – Ryan Nov 27 '15 at 02:17

3 Answers3

1

Using a command line (e.g. CMD on windows or Terminal for unix-based OS), you can create .jar archives using the following command:
jar cf myJar.jar *
Where myJar.jar is the output file. The * will archive everything in the current directory so in the console navigate to the directory with all the files before you use this command.

Locke Donohoe
  • 482
  • 1
  • 7
  • 22
0

Simply specify it during your jar command(assuming your running from inside src):

jar cvf BrowserTest.jar BrowserTest.class ../website

Source: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

After you have that structure, you can retrieve the files like so:

// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
// Create page class
Page customPage  = new Page(cl.getResource("website/page1.html"));

Dupe: Reading a resource file from within jar

Community
  • 1
  • 1
stan
  • 4,885
  • 5
  • 49
  • 72
-1

Update

Included running code on Github

If you do not need an webserver, just do like that:

  1. Create a simple Maven project
  2. Copy your website to src/main/java folder. It will be included in the jar automatically.
  3. Write some code to extract your website from classpath to containing jar folder.

You have to embedded an web server inside your jar. There was multiple way to do that. But I recommend you to try Spring Boot.

Thanh Nguyen Van
  • 1,923
  • 1
  • 18
  • 18
  • Sounds like it's a set of flat HTML documents. I doubt it'll need an embedded webserver, particularly since he already has the code that'll launch a browser to the page via a `file://` URI. Besides, this question is just about how to package and access files in a JAR. – CollinD Nov 27 '15 at 01:00
  • Okay, I just worried too much. Some flat site cannot run with `file://` scheme – Thanh Nguyen Van Nov 27 '15 at 01:10