假设那个.class文件最后会打印输出一个字符串。
解决方案 »
- java中的正则表达式跟编译原理有什么联系?
- 找错题 看看这段代码中哪里有错误,先谢谢帮助菜鸟的老鸟们~~~
- 菜鸟问题求答,初学者
- email正则表达式检测
- Java 的一个cannot resolve symbol问题
- classpath=D:\java;.;D:\tomcat\common\lib\servlet-api.jar 寻找classpath路径是不是从前到后找的啊?
- 请高手帮忙指点一下,本人这方面不会,请执教,谢谢
- 正则表达式怎么匹配这样的/*ss*/sadgasd/*saasf*/
- 谈谈linux下得java应用
- 关于JAVA的出错信息的意思~
- 分享一个java编写的万年历(改进版)
- 可否解析xml类型的字符串
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);
}
}