JNLP 从 jar 中提取文件

问题描述

这是我的 jar 文件

jar file

当我用我的 jar java -jar myprogram.jar 运行我的代码(如下)时,它从 jar 中提取文件(Jar 做我需要的)

List<Path> result = this.getPathsFromresourceJAR("tokenconfig.json");
for (Path path : result) {
    String filePathInJAR = path.toString();
    if (filePathInJAR.startsWith("/")) {
        filePathInJAR = filePathInJAR.substring(1,filePathInJAR.length());
    }
    System.out.println("filePathInJAR : " + filePathInJAR);
    // read a file from resource folder
    InputStream is = this.getFileFromresourceAsstream(filePathInJAR);
    FileUtils.copyInputStreamToFile(is,new File(filePathInJAR) );
}          

// get a file from the resources folder
// works everywhere,IDEA,unit test and JAR file.
private InputStream getFileFromresourceAsstream(String fileName) {

    // The class loader that loaded the class
    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsstream(fileName);

    // the stream holding the file content
    if (inputStream == null) {
        throw new IllegalArgumentException("file not found! " + fileName);
    } else {
        return inputStream;
    }

}

// Get all paths from a folder that inside the JAR file
private List<Path> getPathsFromresourceJAR(String folder)
    throws URISyntaxException,IOException {

    List<Path> result;
    // get path of the current running JAR
    String jarPath = getClass().getProtectionDomain()
            .getCodeSource()
            .getLocation()
            .toURI()
            .getPath();
    System.out.println("JAR Path :" + jarPath);

    // file walks JAR
    URI uri = URI.create("jar:file:" + jarPath);
    try (FileSystem fs = FileSystems.newFileSystem(uri,Collections.emptyMap())) {
        result = Files.walk(fs.getPath(folder)).filter(Files::isRegularFile).collect(Collectors.toList());
    }
    return result;
}

这是我的 jnlp 文件

<?xml version="1.0" encoding="utf-8"?> 
<jnlp spec="1.0+" codebase="http://localhost:8080/bsign/" href="bsign.jnlp">
    <@R_700_4045@ion>
        <title>Jnlp Testing</title>
        <vendor>bermuda</vendor>
        <homepage href="http://localhost:8080/bsign/" />
        <description>jnlp Testing</description>
    </@R_700_4045@ion>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.6+" />
        <jar href="bsignclient.jar" />

        <jar href="libs/commons-discovery-0.2.jar" />
        <jar href="libs/commons-io-2.8.0.jar" />
        <jar href="libs/commons-logging-1.1.1.jar" />
        .
        .
        .

    </resources>
    <application-desc main-class="com.bermuda.App" />
</jnlp>

当我尝试从 jnlp 运行时出现此错误

java.nio.file.FileSystemNotFoundException: C:\bsign\bsignclient.jar
    at com.sun.nio.zipfs.ZipFileSystem.<init>(ZipFileSystem.java:120)
    at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:117)
    at java.nio.file.FileSystems.newFileSystem(UnkNown Source)
    at java.nio.file.FileSystems.newFileSystem(UnkNown Source)
    at com.bermuda.helpers.FileHelper.getPathsFromresourceJAR(FileHelper.java:138)
    at com.bermuda.helpers.FileHelper.extractResources(FileHelper.java:46)
    at com.bermuda.ui.MainMenu.<init>(MainMenu.java:54)
    at com.bermuda.ui.SysTray.<init>(SysTray.java:72)
    at com.bermuda.App.main(App.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)
    at java.lang.reflect.Method.invoke(UnkNown Source)
    at com.sun.javaws.Launcher.executeApplication(UnkNown Source)
    at com.sun.javaws.Launcher.executeMainClass(UnkNown Source)
    at com.sun.javaws.Launcher.doLaunchApp(UnkNown Source)
    at com.sun.javaws.Launcher.run(UnkNown Source)
    at java.lang.Thread.run(UnkNown Source)

来自控制台:

Missing Permissions manifest attribute in main jar: http://localhost:8080/bsign/bsignclient.jar
JAR Path :/bsign/bsignclient.jar
JAR Path :/bsign/bsignclient.jar
#### Java Web Start Error:
#### C:\bsign\bsignclient.jar

因此,当我将 jar 放在 C:\bsign\bsignclient.jar 处时,它会提取文件。但这不是我需要的,我需要从 jnlp 中提取这个文件。我应该如何从 jar 中进行此提取

解决方法

我将 tokenconfig.json 放在我的服务器中的资源文件夹中。 我做了一个嵌入的 URL 像

String path = "http://localhost:8080/bsign/resources/tokenconfig.json"

    String path = "http://localhost:8080/bsign/resources/tokenconfig.json"
    downloadAndCopy(path);


    private void downloadAndCopy(String path){
        try {
            URL url = new URL(path);

            InputStream is = url.openStream();
            System.out.println("copying : " + path);

            FileUtils.copyInputStreamToFile(is,new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }