我想跑#34; ClassA"来自" RunJavaFromJava"在netbeans这里是我的代码:
让我告诉你,首先它没有netbeans工作正常,但我想通过netbeans
public class ClassA {
public static void main(String[] args) {
System.out.println("This is ClassA");
}
}
public class RunJavaFromJava {
public static void main(String[] args) throws Exception {
for (int i = 1; i <= 5; i++) {
Process p1 = Runtime.getRuntime().exec("java ClassA");
}
}
}
答案 0 :(得分:1)
您可以通过
轻松完成此操作从RunJavaFromJava调用ClassA.main()
而不是使用Process
package runjavafromjava;
public class RunJavaFromJava {
public static void main(String[] args) throws Exception {
ClassA.main(null);
}
}
//If you still want to use Process
Process p ;
try {
p = Runtime.getRuntime().exec("java ClassA")
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
p.waitFor();
p.destroy();
while (br.readLine() != null) {
System.out.println(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
在上面的代码中,程序没有任何内容,因为进程的o / p没有被重定向。 PS:确保.class文件位于类路径
中