我正在使用Java的ProcessBuilder来启动子进程,这是另一个必须在单独的JVM中运行的Java程序.
我启动两个Threads来从Process读取stdout和stderr流,这样如果流缓冲区已满,就没有挂起.对Process.waitFor的调用返回但流不会终止.
我使用的代码看起来像(命令是字符串列表):
ProcessBuilder pb = new ProcessBuilder(command);
final Process p = pb.start();
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
Thread outputThread = new Thread() {
@Override
public void run() {
try {
IOUtils.copy(p.getInputStream(), outStream);
} catch (IOException e) {
e.printStackTrace();
}
};
};
outputThread.start();
Thread errorThread = new Thread() {
@Override
public void run() {
try {
IOUtils.copy(p.getErrorStream(), errStream);
} catch (IOException e) {
e.printStackTrace();
}
};
};
errorThread.start();
int returncode = p.waitFor();
outputThread.join(); …Run Code Online (Sandbox Code Playgroud) 我的清单文件的Class-Path条目中有一个带有两个jar的runnable jar:
Class-Path: module1-0.0.1-SNAPSHOT.jar base-0.0.1-SNAPSHOT.jar
Main-Class: test.MySPI
Run Code Online (Sandbox Code Playgroud)
程序运行正常,并且满足引用的jar中的所有依赖项.但是,当我尝试访问类路径时,jar不存在:
String classpath = System.getProperty("java.class.path");
String[] entries = classpath.split(System.getProperty("path.separator"));
for (String entry : entries) {
System.out.println("Entry: " + entry);
}
Run Code Online (Sandbox Code Playgroud)
只给出
Entry: .\module2-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)
有没有办法访问实际的类路径,因为很明显,系统在路径上找到了这些jar?