我正在实现调用另一个java程序的ProcessBuilder程序。但是,我找不到上课了。
程序只生成以下输出:
错误:无法找到或加载主类HelloWorld
计划完成
public class ProcessBuilderSample {
public static void main(String args[]) {
try {
ProcessBuilder broker = new ProcessBuilder("java.exe", "-cp",
"F:\\LunaWorkspace\\ProcessBuilderTest\\bin" ,"com\\hello\\HelloWorld");
Process runBroker = broker.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(runBroker.getInputStream()));
BufferedReader reader1 = new BufferedReader(new InputStreamReader(runBroker.getErrorStream()));
String str=null;
while((str=reader.readLine())!=null){
System.out.println(str);
}
while((str=reader1.readLine())!=null){
System.out.println(str);
}
runBroker.waitFor();
System.out.println("Program complete");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我想要执行的java程序。该程序生成Hello World作为输出。
package com.hello;
public class HelloWorld {
public static void main(String arg[]){
System.out.println("Hello World");
}
}
现在我正在使用:
ProcessBuilder broker = new ProcessBuilder(“java.exe”,“ - cp”, “F:\ LunaWorkspace \ ProcessBuilderTest \ bin”,“com \ hello \ HelloWorld”);
此命令适用于命令提示符,但不适用于processbuilder。
修改
完整的类路径:
ProcessBuilderSample.class:
F:\ LunaWorkspace \ ProcessBuilderExample \ BIN \ COM \样品
的HelloWorld.class:
F:\ LunaWorkspace \ ProcessBuilderTest \ bin \ com \ hello谢谢!!
答案 0 :(得分:1)
需要在构建流程构建器时修复HelloWorld类名称:
"com\\hello\\HelloWorld"
- > "com.hello.HelloWorld"