动态执行一个java文件:
public class Eval
{
    public static void main(String[] args)throws Exception
    {
         Object rval = eval("System.out.println(\"Hello World\");return 5;");
         System.out.println(rval);
    }
    public static Object eval(String str)throws Exception
    { 
         //生成Java源文件 
         StringBuilder s = new StringBuilder("public class Temp{"); 
         s.append("        public Object rt(){"); 
         s.append("                " + str); 
         s.append("        }"); 
         s.append("}"); 
         //在当前目录生成Java源文件
         File f = new File("Temp.java"); 
         PrintWriter pw = new PrintWriter(new FileWriter(f)); 
         pw.println(s.toString()); 
         pw.close(); 
         //动态编译
         com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main(); 
         String[] cpargs = new String[] {"-d", "." ,"Temp.java"}; 
         //动态编译
         int status = javac.compile(cpargs); 
         if(status != 0 )
         { 
                 System.out.println("您给的Java代码有错!"); 
                 return null; 
         } 
         //创建一个URL数组
         URL[] urls = {new URL("file:Temp.class")};
         //以默认的ClassLoader作为父ClassLoader,创建URLClassLoader
         URLClassLoader myClassLoader = new URLClassLoader(urls);
         //加载Temp类
         Class clazz = myClassLoader.loadClass("Temp"); 
         //获取rt方法
         Method rt = clazz.getMethod("rt"); 
         //动态调用rt方法
         return rt.invoke(clazz.newInstance()); 
    } 
}这个代码我在EditPlus里面执行提示:
com.sun.tools.javac 不存在.为什么啊?jar包有的呀,环境变量我已经配置了
在Eclipse里报:
Exception in thread "main" java.lang.ClassNotFoundException: Temp
怎么回事啊,jar包已经导入了,嗨!搞了两个小时没解决,请帮帮忙,哪里出问题了,谢谢了!

解决方案 »

  1.   

    用import导入com.sun.tools.javac.Main
    import com.sun.tools.javac.Main;int status = javac.compile(cpargs); //compile()是静态方法直接用类调用就可以了
    int status = Main.compile(cpargs);java.lang.ClassNotFoundException: Temp 
    类文件Temp.class要放到类文件下,不是源文件文件夹
    String[] cpargs = new String[] {"-d", "." ,"Temp.java"};
    .改成类文件夹就可以了 
      

  2.   

    一:构造方法有问题
    public Main(PrintStream arg0,PrintStream arg1,String arg2);
    二:
    int status = javac.compile(cpargs); 没这个方法。
    改为:
    boolean status = javac.run(cpargs);应该是这个方法。
    我就发现这两问题,做了一会做不出来,没做过这类问题。
    lz做出来拿出来一起学习!
    看看上面两个问题对不对! 
      

  3.   

    lz做这方面的东西,查查相关API
      

  4.   

    String[] cpargs = new String[] {"-d", "build/classes" ,"Temp.java"};
    //动态编译
    int status = Main.compile(cpargs);
    这个方法是有的
    我已经调试成功了
    run:
    Hello World
    5
    成功生成(总时间:0 秒)
      

  5.   

    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.lang.reflect.Method;
    import java.net.URL;
    import java.net.URLClassLoader;
    import com.sun.tools.javac.Main;public class Eval
    {
        public static void main(String[] args)throws Exception
        {
            Object rval = eval("System.out.println(\"Hello World\");return 5;");
            System.out.println(rval);
        }
        public static Object eval(String str)throws Exception
        {
            //生成Java源文件
            StringBuilder s = new StringBuilder("public class Temp{");
            s.append("        public Object rt(){");
            s.append("                " + str);
            s.append("        }");
            s.append("}");
            //在当前目录生成Java源文件
            File f = new File("Temp.java");
            PrintWriter pw = new PrintWriter(new FileWriter(f));
            pw.println(s.toString());
            pw.close();
            String[] cpargs = new String[] {"-d", "build/classes" ,"Temp.java"};//第二个参数换成类文件输出目录
            //动态编译
            int status = Main.compile(cpargs);
            if(status != 0 )
            {
                    System.out.println("您给的Java代码有错!");
                    return null;
            }
            //创建一个URL数组
            URL[] urls = {new URL("file:Temp.class")};
            //以默认的ClassLoader作为父ClassLoader,创建URLClassLoader
            URLClassLoader myClassLoader = new URLClassLoader(urls);
            //加载Temp类
            Class clazz = myClassLoader.loadClass("Temp");
            //获取rt方法
            Method rt = clazz.getMethod("rt");
            //动态调用rt方法
            return rt.invoke(clazz.newInstance());
        }
    }
      

  6.   

    对应该的文件夹你建立了吗?
    com\sun\tools\javac ,按这个路径建立文件夹(目录),如果javac是一个jar,那就要只建 com\sun\tools\,以此类推。
      

  7.   

    public class Eval 

        public static void main(String[] args)throws Exception 
        { 
            Object rval = eval("System.out.println(\"Hello World\");return 5;"); 
            System.out.println(rval); 
        } 
        public static Object eval(String str)throws Exception 
        { 
            //生成Java源文件 
            StringBuilder s = new StringBuilder("public class Temp{"); 
            s.append("        public Object rt(){"); 
            s.append("                " + str); 
            s.append("        }"); 
            s.append("}"); 
            //在当前目录生成Java源文件 
            File f = new File("Temp.java"); 
            PrintWriter pw = new PrintWriter(new FileWriter(f)); 
            pw.println(s.toString()); 
            pw.close(); 
    学习
      

  8.   

    2楼的意思我明白了。谢谢,我也已经执行成功!给分了
    《疯狂java讲义》