我正在开发一个简单的java程序,它运行另一个java程序,它位于文件系统的任何位置。这是代码: -
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
class CompileTest {
public static void main(String args[]) {
try {
int k = runProcess("javac H://Study//eclipse_workspace//advance//src//Hello.java");
if (k==0)
k=runProcess("java H://Study//eclipse_workspace//advance//src//Hello");
} catch (Exception e) {
e.printStackTrace();
}
}
private static int runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
pro.waitFor();
//System.out.println(command + " exitValue() " + pro.exitValue());
return pro.exitValue();
}
private static void printLines(String name, InputStream ins) throws Exception {
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(name + " " + line);
}
}
}
这是Hello.java
class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello");
}
}
但是当它是编译时,它将输出提供为: -
java H://Study//eclipse_workspace//advance//src//Hello stderr: Error: Could not find or load main class H:..Study..eclipse_workspace..advance..src..Hello
我正在使用Eclipse IDE
答案 0 :(得分:0)
尝试类似:java -classpath . Hello
。
如果您没有指定包,则必须从同一目录运行Hello.class
。
runProcess(cd H:/Study/eclipse_workspace/advance/src/)
然后
runProcess(java Hello)