Runtime runtime = Runtime.getRuntime();
Process process = null;
String line = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
process = runtime.exec("javac someprogram.java");    //关键是这行
is = process.getInputStream();
isr=new InputStreamReader(is);
br =new BufferedReader(isr);
while( (line = br.readLine()) != null) {
  System.out.println(line);
}

解决方案 »

  1.   

    先配置环境变量
    set path=jdk目录\bin
    set classpath=.;jdk目录\lib然后 javac youprogram.java 就行了!
      

  2.   

    runtime.exec("javac someprogram.java");    
    执行外部程序!
      

  3.   

    我按照formalin写了下面的程序但是没有编译呀,也不报错,到底哪错了
    public void CompileJava(String sFileName){
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        String line = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        System.out.println("compile1");
        try{
          process = runtime.exec("javac       D:/sysconstruct/src/sysconstruct/genbean/yuangongEntity.java"); //关键是这行
          System.out.println("success");
        }catch(IOException e){
          System.out.println("compile error");
        }
              System.out.println("compile2");
        is = process.getInputStream();
        isr=new InputStreamReader(is);
        br =new BufferedReader(isr);
              System.out.println("compile3");
        try{
          while ( (line = br.readLine()) != null)
          {
            System.out.println(line);
          }
        }catch(IOException e){
          System.out.println("compile error1");
        }
    }
      

  4.   

    可运行的,但是我想写成下面这样就不行了
    javac -d . d:\test.javatest.java文件如下package com.test;
    public class test{
      public test(){
      }}
      

  5.   

    给个比较另类的解决方法:
    import com.sun.tools.javac.*;class TestCompiler
    {
    public static void main(String[] args) 
    {
    Main.compile(args);
    }
    }
      

  6.   

    这样的话,javac中的参数都可以用了!
      

  7.   

    我试了楼上的方法但是好象也不行,不知道为什么import com.sun.tools.javac.*;
    public class Bean1
    {
      public static void main(String[] args)
      {
              String[] a = new String[3];
              a[0] = "-d";
              a[1] = ".";
              a[2] = "d:/test.java";
             // Main.compile(args);
              System.out.println("before completed");
              Main.main(a);
              System.out.println("after completed");
      }}
      

  8.   

    import com.sun.tools.javac.*;
    public class Bean1
    {
      public static void main(String[] args)
      {
              String[] a = new String[3];
              a[0] = "-d";
              a[1] = "d:/";         如果此处改为具体目录就可以生成带有包名的java文件
              a[2] = "d:/test.java";
             // Main.compile(args);
              System.out.println("before completed");
              Main.main(a);
              System.out.println("after completed");
      }}
    但是在dos下,文件为test.java,里面的包名这package com.test: 
    用javac -d . d:\test.java 编译时就能相应的生成包目录和对应的test.class文件d:\com\test\test.class
      

  9.   

    public static void main(String[] args)
      {
              String[] a = new String[3];
              a[0] = "-d";
              a[1] = "d:/test";
              a[2] = "d:/test.java";
             // Main.compile(args);
              System.out.println("before completed");
              Main.compile(a);
              System.out.println("after completed");
      }
    这样就可以了,谢谢各位