我希望在Runtime.getRuntime()。exec(...)的帮助下,从另一个内部运行NetBeans + Java 8中的程序。它正确运行javac和javah但不运行java。非常感谢您的帮助。
这是我想要运行的程序
package Programs;
public class AddTwoIntegers {
private static Integer addTwoIntegers(Integer a, Integer b)
{
return a + b;
}
public static void main(String[] args)
{
Integer a = 5;
Integer b = 8;
Integer result = addTwoIntegers(a, b);
System.out.println(a.toString()
.concat( " + ")
.concat( b.toString() )
.concat( " = " )
.concat( result.toString()));
}
}
这是假装运行上一课程的程序:
/*
This program is intended to run a java program from another java program
It runs javac and javah but not java.
*/
package Programs;
import java.io.*;
import java.util.Arrays;
//import java.util.concurrent.TimeUnit;
// java.util.concurrent.TimeUnit;
public class JavacTest {
private static void printLines(String name, InputStream ins) throws Exception {
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(name + " " + line);
}
}
private static void runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
printLines(command + " stdin:", pro.getInputStream());
printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
//Take your time
pro.waitFor();
System.out.println(command + " exitValue() " + pro.exitValue());
}
private static void runProcess2(String [] command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
printLines(Arrays.toString(command) + " stdin:", pro.getInputStream());
printLines(Arrays.toString(command) + " stdout:", pro.getInputStream());
printLines(Arrays.toString(command) + " stderr:", pro.getErrorStream());
//Take your time
pro.waitFor();
System.out.println(Arrays.toString(command) + " exitValue() " + pro.exitValue());
}
public static void main(String[] args) {
try {
System.out.println("\nCompiling with javac: .java -> .class");
runProcess("/home/jose/jdk1.8.0_111/bin/javac
/home/jose/NetBeansProjects/eJVolu17P/src/Programs/AddTwoIntegers.java");
//take your time
//TimeUnit.SECONDS.sleep(1);
System.out.println("\n\nShowing the bytecode content of the .class file with javap");
runProcess("/home/jose/jdk1.8.0_111/bin/javap -c
/home/jose/NetBeansProjects/eJVolu17P/src/Programs/AddTwoIntegers.class");
System.out.println("\n\nWorking Directory = " +
System.getProperty("user.dir"));
//TimeUnit.SECONDS.sleep(1);
System.out.println("\n\nRunning the .class file with java");
//runProcess("/home/jose/jdk1.8.0_111/bin/java -cp
/home/jose/NetBeansProjects/eJVolu17P/build/classes/Programs/AddTwoIntegers");
//runProcess("/home/jose/jdk1.8.0_111/bin/java -cp /build/classes/Programs/AddTwoIntegers");
String[] Commands = new String[3];
Commands[0] = "/home/jose/jdk1.8.0_111/bin/java";
Commands[1] = "-classpath";
Commands[2] = "/home/jose/NetBeansProjects/eJVolu17P/build/classes/Programs/AddTwoIntegers";
runProcess2(Commands );
} catch (Exception e) {
}
}
}
答案 0 :(得分:1)
您应该通过类似的方式运行程序 /home/jose/jdk1.8.0_111/bin/java -cp / home / jose / NetBeansProjects / eJVolu17P / build / classes / Programs.AddTwoIntegers 试一试,看看是否有效。
类路径应指向package.Java类名的父文件夹。检查一下是否有效。
答案 1 :(得分:0)
我有一个误解:.exec(java ...)的参数有3个令牌,但在考虑了Ayusman提出的解决方案之后,很明显它由4个令牌组成,例如它在以下最终解决方案:
plt.margins(x=0.04)