简体   繁体   English

在 .jar 中包含其他文件

[英]Including other files in a .jar

Ok, so I am experimenting using a java program to open up HTML documents.好的,所以我正在尝试使用 java 程序打开 HTML 文档。 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.我有一个名为“网站”的单独文件夹中的网站,该文件夹位于 src 文件旁边。 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.然后我想把 src 文件夹和网站文件夹放入同一个 .jar 文件中,这样你就可以将 .jar 文件发送到任何计算机,当你运行它时,它会打开网站。 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.这是打开 HTML 文件的代码。

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();
        }
    }
}

Using a command line (eg CMD on windows or Terminal for unix-based OS), you can create .jar archives using the following command:使用命令行(例如 Windows 上的 CMD 或基于 unix 的操作系统的终端),您可以使用以下命令创建 .jar 档案:
jar cf myJar.jar *
Where myJar.jar is the output file.其中 myJar.jar 是输出文件。 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. * 将存档当前目录中的所有内容,因此在使用此命令之前,请在控制台中导航到包含所有文件的目录。

Simply specify it during your jar command(assuming your running from inside src):只需在您的 jar 命令中指定它(假设您从 src 内部运行):

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

Source: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html来源: 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 Dupe: 从 jar 中读取资源文件

Update更新

Included running code on Github包含在 Github 上的运行代码

If you do not need an webserver, just do like that:如果您不需要网络服务器,只需这样做:

  1. Create a simple Maven project创建一个简单的Maven项目
  2. Copy your website to src/main/java folder.将您的网站复制到src/main/java文件夹。 It will be included in the jar automatically.它将自动包含在jar
  3. Write some code to extract your website from classpath to containing jar folder.编写一些代码将您的网站从classpath提取到包含jar文件夹。

You have to embedded an web server inside your jar.您必须在 jar 中嵌入一个 Web 服务器。 There was multiple way to do that.有多种方法可以做到这一点。 But I recommend you to try Spring Boot.但我建议您尝试使用 Spring Boot。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM