我正在从另一个jar文件执行jar文件。当从子jar中访问资源文件时,它给出异常: java.io.FileNotFoundException:文件'file:PATH_TO_CHILD_JAR/example-1.0.0-0-SNAPSHOT-jar -with-dependencies.jar!resource_file_path/abs.sql.tpl ' 不存在。从另一个jar文件执行jar文件
我无法确定它为什么要“!”在搜索资源文件时标记。
我正在从另一个jar文件执行jar文件。当从子jar中访问资源文件时,它给出异常: java.io.FileNotFoundException:文件'file:PATH_TO_CHILD_JAR/example-1.0.0-0-SNAPSHOT-jar -with-dependencies.jar!resource_file_path/abs.sql.tpl ' 不存在。从另一个jar文件执行jar文件
我无法确定它为什么要“!”在搜索资源文件时标记。
试试这个
// Run a java app in a separate system process
Process proc = Runtime.getRuntime().exec("java -jar A.jar");
// Then retreive the process output
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
您可以在Java程序与执行的jar文件。
请确保您已在具有Main-Class属性的jar中添加了Manifest文件。
我的步骤和输出:主类:com.demo.TestJar
创建的测试Java程序:
package com.demo;
public class TestJar extends Object {
public static void main(String args[]) {
System.out.println("TestJar");
}
}
包的
创建了以下行清单文件jar:jar cvfm /home/user/TestJarOne.jar manifest.txt
写测试程序:
import java.io.BufferedInputStream;
import java.io.IOException;
public class TestJSS extends Object {
static int i = 0;
public static void main(String args[]) throws IOException, InterruptedException {
System.out.println("Calling jar");
Process p = Runtime.getRuntime().exec("java -jar /home/user/TestJarOne.jar arg1 arg2");
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
synchronized (p) {
p.waitFor();
}
System.out.println(p.exitValue());
int b=0;
while((b=bis.read()) >0){
System.out.print((char)b);
}
System.out.println("");
System.out.println("Called jar");
}
}
请检查一次:https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar#20389418 –