作为我的程序的一部分,我有一个连接管理器,它从客户端接收连接,然后给客户端一个端口号和一个用于连接的密码.在这一点上,管理器需要调用我必须处理此连接的jar文件,并使用一些参数,并继续,(忽略其他程序正在做的事情).
我的问题是执行jar文件.我查找了类似的问题,并尝试使用流程构建器并使用Runtime.exec.我移动了jar文件,并检查了它的权限.它只是拒绝从另一个java程序工作,但从命令行完美工作.这是我的一个测试运行的示例.
package test;
import java.io.*;
public class Main {
public static void main (String [] args ) throws IOException, ClassNotFoundException, InterruptedException {
Process p = Runtime.getRuntime().exec("java -jar \'/home/ryan/CytoscapeInterface.jar" +
"\' arg1 arg2");
//Process builder way
/*ProcessBuilder pb = new ProcessBuilder("/home/ryan/CytoscapeInterface.jar",
"-jar", "CytoscapeInterface.jar", "agr1", "arg2");
pb.redirectErrorStream();
Process p = pb.start();*/
BufferedInputStream bis = new BufferedInputStream(p.getErrorStream());
synchronized (p) { p.waitFor(); }
System.out.println(p.exitValue());//its 1 for runtime, 2 for process Builder
int read = bis.available();
//had a loop, found out I just needed to go through once
byte[] b = new byte [read];
bis.read(b);
read = bis.available();
bis.close();
FileOutputStream fos = new FileOutputStream (new File("/home/ryan/Desktop/FileTest.txt"));
fos.write(b);//Writes error file
fos.close();
}
}
Run Code Online (Sandbox Code Playgroud)
waitFor为运行时返回1,为构建器返回2.运行时的错误输出是"无法访问jarfile'/home/ryan/CytoscapeInterface.jar'.虽然使用构建器会产生几行错误,但有一些奇怪的字符,第一个错误是找不到命令.
我已经完成了对场景的测试,我可以在java程序中执行jar文件(不设置类路径).
您能否确保在具有Main-Class属性的jar中添加了Manifest文件.
我的步骤和输出:
package com.test;
public class TestJSSJar extends Object {
public static void main(String args[]) {
System.out.println("Hi! I'm in the jar");
System.out.println("Arg:" + args[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
package com.test;
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 /temp/jss.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");
}
}
Run Code Online (Sandbox Code Playgroud)
3.包装罐(移动到临时文件夹):罐子CVFM jss.jar manifest.txt COM
4.Write测试程序:
Calling jar
0
Hi! I'm in the jar
Arg:arg1
Called jar
Run Code Online (Sandbox Code Playgroud)
5.输出
public class TestJSSJar extends Object {
public static void main(String args[]) {
System.out.println("Hi! I'm in the jar");
System.out.println("Arg:" + args[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10644 次 |
| 最近记录: |