请问如何在Java程序的代码中给出一个*.java的文件,然后调用javac这样的程序生成*.class文件呢?

解决方案 »

  1.   

    class TestJavac {
    public static void main(String args[]){
    try{
    Runtime rt = Runtime.getRuntime();
    String srcFile ="C:/HelloWorld.java";
    String command = "javac ";
    rt.exec(command+srcFile);
    System.out.println("Compile File Success!");
    }catch(Exception e){
    System.out.println("Compile File Fail!");
    e.printStackTrace();
    }
    }
    }
      

  2.   

    没有抛出异常就成功啦e.printStackTrace();//获取异常堆栈
    e.getMessage();//获取异常消息呵呵,楼主给分结贴,谢谢!!!!
      

  3.   

    Runtime.getRuntime().exec("javac *.java")
      

  4.   

    楼主说的.java文件有程序生成然后在硬盘上保存好的吗?这样的话,首先要确定你的Path环境变量是不是包含了JDK的bin目录,因为这样的才能够在程序运行环境下调用javac.exe。还有,这样做,如果编译出错,获得的信息你不能在程序中得到,而是在启动的Console窗口中显示。
      

  5.   

    回KingStyle以及Polarislee:我试了一下, 如果出现错误, 无论用e.getMessage()或者在Console窗口中都不会有显示呀...
      

  6.   

    %JAVA_HOME%/lib/tools.jar
    类com.sun.tools.javac.Main这个就是javac的入口类
    可以用它来编译如果愿意给分 本人只接受37分,谢谢
      

  7.   

    Runtime rt=Runtime.getRuntime();
    rt.exec("set Path=%JAVA_HOME%/lib/tools.jar");//设置环境变量
    rt.exec("javac *.java");//编译源文件
      

  8.   

    我在上面说的:   public static int javac(String[] cmd)
        {
            return com.sun.tools.javac.Main.compile(cmd,new PrintWriter(System.err));
        }
      

  9.   

    public static int compile(File _javaf) throws IOException
      {
        String fabsPath = _javaf.getAbsolutePath();
        Process p = Runtime.getRuntime().exec("new String[]{"javac","-cp", "\"./lib/j2ee1.4.jar;./classes\"", "fabsPath");
        InputStream perrorStream = p.getErrorStream();
        InputStream poutStream = p.getInputStream();
        int pec, poc;
        while(((pec = perrorStream.read()) != -1) | ((poc = poutStream.read()) != -1))
        {
          System.err.print((pec != -1) ? ((char) pec) : ' ');
          System.out.print((poc != -1) ? ((char) poc) : ' ');
        }
        return p.exitValue();
      }此方法亦可。
      

  10.   

    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.PrintWriter;
    import java.lang.reflect.Method;public class HelloWorldGenerator {
    public static void main( String[] args ) {
    try {
    FileOutputStream fstream
    = new FileOutputStream( "HelloWorld.java" );
    PrintWriter out = new PrintWriter( fstream );
    out.println(
    "class HelloWorld { \n"
    + " public static void main( String[] args ) { \n"
    + " System.out.println( \"Hello world!\" );\n"
    + " } \n"
    + "} "
    );
    out.flush();
    Process p
    = Runtime.getRuntime().exec("javac -d classes HelloWorld.java" );
    p.waitFor();
    if ( p.exitValue() == 0 ) {
    Class outputClassObject = Class.forName( "HelloWorld" );
    Class[] fpl = { String[].class };
    Method m = outputClassObject.getMethod( "main", fpl );
    m.invoke( null, new Object[]{ new String[] {} } );
    } else {
    InputStream errStream = p.getErrorStream();
    for ( int j = errStream.available(); j > 0; j-- )
    System.out.write( errStream.read() );
    }
    } catch(Exception e){
    throw new RuntimeException(e);
    }
    }
    }
      

  11.   

    顶,赞成用 com.sun.tools 包中的方法,对于动态编译全面支持
      

  12.   

    要用双斜杠的
    String srcFile ="C://HelloWorld.java";