[英]Executing jar file from another 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'不存在。
我不知道为什么要加上“!” 搜索资源文件时标记。
尝试这个
// 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文件。
我的步骤和输出:
创建的清单文件包含以下行:Main-Class: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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.