我正在尝试从另一个Java进程开始 我的jar,我正在使用ProcessBuilder:
File javaHome = new File(System.getProperty("java.home"), "bin");
List<String> javaList = new ArrayList<String>();
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.redirectErrorStream(true);
processBuilder.directory(serverDir);
{
javaList.add(javaHome + File.separator + "java");
javaList.add("-XX:MaxPermSize=512m");
javaList.add("-Xmx2048M");
// -Djava.library.path="natives-win-x64/"
javaList.add(
String.format(
"-Djava.library.path=\"natives-%s-%s/\"",
//Get system os,
//Get system arch
)
);
{
String classPath = new String();
for (File library : scanLibrary(new File(serverDir, "libraries"))) {
String libPath = library.getPath();
classPath += libPath + ";";
}
classPath += new File(new File(serverDir, "binary"), "MainJar.jar").getPath();
javaList.add("-classpath \\\"" + classPath + "\\\" ");
}
javaList.add("my.other.jar.main.class");
}
processBuilder.command(javaList);
processBuilder.start();
错误:无法创建Java虚拟机。 错误:发生了致命异常。程序将会退出。 无法识别的选项: -classpath“C:\ Absoulte \ Path \ Library.jar; C:\ Absoulte \ Path2 \ Library2.jar; C:\ Absoulte \ Path3 \ Library3.jar”
答案 0 :(得分:1)
您将--classpath "..."
作为一个参数传递。我相信你应该把它指定为两个参数:
javaList.add("-classpath");
javaList.add("\\\"" + classPath + "\\\");
(顺便说一句,我不清楚你是否真的需要所有这些反斜杠......你可能会发现只有javaList.add(classPath)
就足够了,或者javaList.add("\"" + classPath + "\"")
。)