编译Java程序: import java.io.FileNotFoundException; import java.io.PrintWriter;import com.sun.tools.javac.Main;public class Test { public static void main(String[] argv) throws FileNotFoundException { PrintWriter writer = new PrintWriter("result.txt"); String[] options = { "/Users/Biao/Desktop/FilteredJList.java" }; Main.compile(options, writer); } } 执行Java程序: // Process proc = Runtime.getRuntime().exec(String.format("java %s", "FilteredJList")); private void run(String directory, String className, String[] args) { try { File classLoaderDirectory = new File(directory); URL url = classLoaderDirectory.toURL(); URL[] urls = new URL[] { url }; ClassLoader loader = new URLClassLoader(urls); Class clazz = loader.loadClass(className); Method mainMethod = clazz.getMethod("main", String[].class); // mainMethod.invoke(null, new Object[] { new String[] { /* args */} // }); mainMethod.invoke(null, new Object[] { args }); } catch (Exception e) { e.printStackTrace(); } } ================================分隔线================================ JDK6.0中可以使用下面的方法: This example using the Java Compiler API introduced in JDK 1.6 to programmatically compile a Java class. Here we'll compile the Hello.java. The process of compiling can be start by obtaining a JavaCompiler from the ToolProvider.getSystemJavaCompiler().The simplest way to compile is by calling the run() method of the compiler and passing the first three arguments with null value. These three argument will use the default System.in,System.out and System.err. The final parameter is the file of the Java class to be compiled.When error happened during compilation process the non-zero result code will be returned. After the compile process you'll have the Hello.class just as if you were compiling using thejavac command.view sourceprint? package org.kodejava.example.tools;
public class CompileHello { public static void main(String[] args) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); int result = compiler.run(null, null, null, "src/org/kodejava/example/tools/Hello.java");
System.out.println("Compile result code = " + result); } }
InputStream in = pro.getErrorStream();
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;public class OnlineExamUtil {
public static void putStringIntoFile(String strCode){
String path = new String("E:/test/Code.java");
boolean flag = (new File(path)).delete();
try {
PrintWriter out = new PrintWriter(new File(path));
out.print(strCode);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void executeFile(){
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null,"E:/test/Code.java");
System.out.println("Compile result code = " + result); }
public static void main(String[] agrs) throws IOException{
putStringIntoFile(" public class Code {public static void main(String[] agrs){System.out.println(\"2\");System.out.println(\"2\");}}");
//Process child = Runtime.getRuntime().exec("Code.java");
String cmd = "javac E:/test/Code.java";
String cmd1 = "java E:/test/Code";
Runtime.getRuntime().exec(cmd);
Process p = Runtime.getRuntime().exec(cmd1);
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
}可以编译成功,但是执行.class文件的时候出错
p.getInputStream()));
这里是向进程输入吧?程序应该是等待你输入呢你试试Output瞧瞧
1.
java E:/test/Code
这个条指令可以在cmd里执行下看看可能执行。2.
Code.java源码是否存在package3.
OnlineExamUtil 类是在package com.online_exam.util里的,如何跳转到Code.class所在目录的问题就像1L说的,能不能将Code里的打印操作后,增加一个return String的操作,然后Class.forName?稍微变通下,可能节省很多时间。
String cmd1 = "java -cp \"E:/test\" Code";
import java.io.FileNotFoundException;
import java.io.PrintWriter;import com.sun.tools.javac.Main;public class Test {
public static void main(String[] argv) throws FileNotFoundException {
PrintWriter writer = new PrintWriter("result.txt");
String[] options = { "/Users/Biao/Desktop/FilteredJList.java" };
Main.compile(options, writer);
}
}
执行Java程序:
// Process proc = Runtime.getRuntime().exec(String.format("java %s", "FilteredJList"));
private void run(String directory, String className, String[] args) {
try {
File classLoaderDirectory = new File(directory);
URL url = classLoaderDirectory.toURL();
URL[] urls = new URL[] { url };
ClassLoader loader = new URLClassLoader(urls); Class clazz = loader.loadClass(className); Method mainMethod = clazz.getMethod("main", String[].class);
// mainMethod.invoke(null, new Object[] { new String[] { /* args */}
// });
mainMethod.invoke(null, new Object[] { args });
} catch (Exception e) {
e.printStackTrace();
}
}
================================分隔线================================ JDK6.0中可以使用下面的方法:
This example using the Java Compiler API introduced in JDK 1.6 to programmatically compile a Java class. Here we'll compile the Hello.java. The process of compiling can be start by obtaining a JavaCompiler from the ToolProvider.getSystemJavaCompiler().The simplest way to compile is by calling the run() method of the compiler and passing the first three arguments with null value. These three argument will use the default System.in,System.out and System.err. The final parameter is the file of the Java class to be compiled.When error happened during compilation process the non-zero result code will be returned. After the compile process you'll have the Hello.class just as if you were compiling using thejavac command.view sourceprint?
package org.kodejava.example.tools;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class CompileHello {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null,
"src/org/kodejava/example/tools/Hello.java");
System.out.println("Compile result code = " + result);
}
}