JNLP 从 jar 中提取文件
JNLP extracting files from jar
这是我的 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 中提取?
我将 tokenconfig.json
放在我的服务器资源文件夹中。
我做了一个嵌入式 URL like
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();
}
}
这是我的 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 中提取?
我将 tokenconfig.json
放在我的服务器资源文件夹中。
我做了一个嵌入式 URL like
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();
}
}