Java动态编译一个简单的例子(我转载的,但是经过修定,可以在Eclipse下运行)...

先不说别的,在Eclipse下运行的代码如下
ExpandedBlockStart.gif 代码

package com.test;

import java.io.
* ;
import java.lang.reflect.
* ;
import java.io.File;

/* *
 * @author DiZhuang com.test CompileTest.java  
*/
public   class  CompileTest {
    
static  com.sun.tools.javac.Main javac  =   new  com.sun.tools.javac.Main();

    @SuppressWarnings(
" static-access " )
    
public   static   void  main(String[] args) {
        System.
out .println( " User Dir is  "   +  System.getProperty( " user.dir " )
                
+   " \\src\\com\\test " );
        
try  {
            File file 
=  File.createTempFile( " RunTime " " .java " new  File(System
                    .getProperty(
" user.dir " )
                    
+   " \\src\\com\\test " ));
            String filename 
=  file.getName();
            System.
out .println( " tempFile name is  "   +  filename);
            String classname 
=  filename.substring( 0 , filename.indexOf( ' . ' ));
            System.
out .println( " class name is  "   +  classname);
            PrintWriter 
out   =   new  PrintWriter( new  FileOutputStream(file));
            
out .println( " package com.test; " );
            
out .println( " public class  "   +  classname  +   " { " );
            
out .println( " public static void main(String[] args){ " );
            
out .println( " System.out.println(\ " Yes ! \ " ); " );
            
out .println( " }} " );
            
out .flush();
            
out .close();
            String[] arg 
=   new  String[] {  " -d " ,
                    System.getProperty(
" user.dir " +   " \\bin " ,
                    System.getProperty(
" user.dir " +   " \\src\\com\\test\\ "
                            
+  filename };
            
int  status  =  javac.compile(arg);
            System.
out .println( " compile status is  "   +  status);
            Class
<?>  cls  =  Class.forName( " com.test. " + classname);
            Method main 
=  cls.getMethod( " main " new  Class[] { String[]. class  });
            main.invoke(
null new  Object[] {  new  String[ 0 ] });
        }  catch  (Exception e) {
            e.printStackTrace();
        }

    }

}

按照网上的例子改写了一下,出现问题如下:

1 说找不到com.sun.tools.javac.Main ,主要是在Eclipse下加载了自己的JRE吧,我是这么认为的,解决办法:将别的JDK下面的tools.jar装入Eclipse类路径下(个人认为这是我们写test程序最省事的办法)

2 Eclipse下编译后的代码在bin文件夹下面,并且一般都有包名,所以编译和运行时候,千万别忘记路径,注意:这个我们在JDK下运行是不一样。

修改如下:

String[] arg = new String[] { "-d",
                    System.getProperty(
"user.dir"+ "\\bin",
                    System.getProperty(
"user.dir"+ "\\src\\com\\test\\"
                            
+ filename };
-d 指明编译后类的存放位置

 Class<?> cls = Class.forName("com.test."+classname);
指明类名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值