关于linux下java getRuntime.exec()的问题
我在做一个实验室项目,服务器tomcat,功能是用户通过浏览器端提交自己的java代码,然后服务器进行编译执行。在处理用户提交上来的代码时,我使用Runtime.exec()去执行一个生成的脚本文件runJava.sh,该脚本内容很简单:(Main.java为用户提交的程序)
#!/bin/sh
cd /usr/local/tomcat/apache-tomcat-7.0.39/webapps/oops/Oops/34/c_all_qr_8e243677-6238-4150-9484-c057f3d90830/Answer/1120120745/Safla/project/src
javac Main.java
/usr/java/jdk1.6.0_30/bin/java Main C 9 >> /usr/local/tomcat/apache-tomcat-7.0.39/webapps/oops/Oops/34/c_all_qr_8e243677-6238-4150-9484-c057f3d90830/Answer/1120120745/Safla/out/C/4.txt
Exception in thread "main" java.lang.NoClassDefFoundError: Main
this is getErrorStream:
Caused by: java.lang.ClassNotFoundException: Main
this is getErrorStream:
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
this is getErrorStream:
at java.security.AccessController.doPrivileged(Native Method)
this is getErrorStream:
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
this is getErrorStream:
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
this is getErrorStream:
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
this is getErrorStream:
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
this is getErrorStream:
Could not find the main class: Main. Program will exit.
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProcessBuilderTest {
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("java", "Test2");
pb.directory(new File("/Users/Biao/Desktop/")); // 关键在这里
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println("End");
}
}