jnlp从jar中提取文件

camsedfj  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(310)

这是我的jar文件:

当我用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">
    <information>
        <title>Jnlp Testing</title>
        <vendor>bermuda</vendor>
        <homepage href="http://localhost:8080/bsign/" />
        <description>jnlp Testing</description>
    </information>
    <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执行这个extaction?

z4iuyo4d

z4iuyo4d1#

我把 tokenconfig.json 在“我的服务器”的“内部资源”文件夹中。我做了一个嵌入的网址 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();
    }
}

相关问题