Java运行时编译

Java运行时编译
public   static   void  main(String[] args)  throws  IOException {
        File sourceFile 
=   new  File( " c:\\java\\A.java " );
        JavaCompiler compiler 
=  ToolProvider.getSystemJavaCompiler();
        System.out.println(System.getProperties().getProperty(
" java.class.path " +   " ;F:\\IndigoSpace\\ejp " );
        compiler.run(
null null null " -cp " , System.getProperties().getProperty( " java.class.path " +   " ;F:\\IndigoSpace\\ejp " , sourceFile.getPath());
        System.out.println(
new  File( " c:\\java\\ " ).toURI().toURL());
        URLClassLoader loader 
=   new  URLClassLoader( new  URL[]{ new  File( " c:\\java\\ " ).toURI().toURL()});
        
try  {
            loader.loadClass(
" A " );
        } 
catch  (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
需要注意的是,上面的代码只有在JDK上才能运行,因为JDK里面才有javac。而且在实际应用中,你还要 自己将package声明转换成文件目录,否者装载类的时候就会找不到。

你可能感兴趣的:(Java运行时编译)