假设那个.class文件最后会打印输出一个字符串。

解决方案 »

  1.   

    模拟控制台执行java命令,然后获取输出。Process pro = runtime.exec(command);  
    InputStream in = pro.getErrorStream();  
      

  2.   

    我试过,但不知道为什么一直卡着,没有输出,下面我的代码文件:package com.online_exam.util;import java.io.BufferedReader;
    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文件的时候出错
      

  3.   

    试试也调用p.getErrorStream(),有可能输出是在err流里。
      

  4.   

    BufferedReader in = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
    这里是向进程输入吧?程序应该是等待你输入呢你试试Output瞧瞧
      

  5.   

    嗯,问题怎么解决还是没搞定,不过有几个调试建议:
    1.
    java E:/test/Code
    这个条指令可以在cmd里执行下看看可能执行。2.
    Code.java源码是否存在package3.
    OnlineExamUtil 类是在package com.online_exam.util里的,如何跳转到Code.class所在目录的问题就像1L说的,能不能将Code里的打印操作后,增加一个return String的操作,然后Class.forName?稍微变通下,可能节省很多时间。
      

  6.   

     String cmd1 = "java E:/test/Code"; 这个地方不对,java命令不能带路径执行。试试这个
    String cmd1 = "java -cp \"E:/test\" Code";
      

  7.   

    编译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;
     
    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);
    }
    }
      

  8.   

    引入外部class文件,并执行,需要自定义ClassLoader。