动态生成java文件的方法

[java]  view plain  copy
  1. package com.hongsoft.test;   
  2. import java.io.*;  
  3. //定制的类装入器  
  4. public class TestCompile extends ClassLoader  
  5. {  
  6. String _compiler;  
  7. String _classpath;  
  8.   
  9. public static void main(String[] args)  
  10. {  
  11.   
  12. new TestCompile();  
  13. }  
  14. public TestCompile()  
  15. {  
  16. super(ClassLoader.getSystemClassLoader());  
  17. //默认编译器  
  18. if (_compiler == null)  
  19.    _compiler = "D://j2sdk1.4.2//bin//javac";  
  20.   
  21. _classpath = ".";  
  22. String extraclasspath =  
  23.    "c://Program Files//Java//j2re1.4.2//lib//rt.jar";  
  24. // = System.getProperty("calc.classpath");  
  25. if (extraclasspath != null)  
  26. {  
  27.    _classpath =  
  28.     _classpath  
  29.      + System.getProperty("path.separator")  
  30.      + extraclasspath;  
  31. }  
  32. compile();  
  33. }  
  34.   
  35. public void compile()  
  36. {       
  37. String filename = "";  
  38. String classname = "";  
  39. try  
  40. {  
  41.    //创建临时文件  
  42.    File javafile =  
  43.     File.createTempFile("compiled_"".java"new File("."));  
  44.    filename = javafile.getName();  
  45.    classname = filename.substring(0, filename.lastIndexOf("."));  
  46.    generateJavaFile(javafile, classname);  
  47.    //编译文件  
  48.    invokeCompiler(javafile);  
  49.    //创建java类  
  50.    byte[] buf = readBytes(classname + ".class");  
  51.    Class c = defineClass(buf, 0, buf.length);  
  52.    try  
  53.    {  
  54.        c.newInstance();  
  55.    }  
  56.    catch (IllegalAccessException e)  
  57.    {  
  58.     throw new RuntimeException(e.getMessage());  
  59.    }  
  60.    catch (InstantiationException e)  
  61.    {  
  62.     throw new RuntimeException(e.getMessage());  
  63.    }  
  64. }  
  65. catch (IOException e)  
  66. {  
  67.    throw new RuntimeException(e.getMessage());  
  68. }  
  69. }  
  70.   
  71. //生成java文件  
  72. void generateJavaFile(File javafile, String classname) throws IOException  
  73. {  
  74. FileOutputStream out = new FileOutputStream(javafile);  
  75. String text =  
  76.    "public class "  
  77.     + classname  
  78.     + " {"  
  79.     + " public int getCreater() {return 1;}"  
  80.     + "}";  
  81. out.write(text.getBytes());  
  82. out.close();  
  83. }  
  84.   
  85. //编译java文件  
  86. void invokeCompiler(File javafile) throws IOException  
  87. {  
  88. String[] cmd =  
  89.    { _compiler, "-classpath", _classpath, javafile.getName()};  
  90. //执行编译命令  
  91. //A1:  
  92. Process process = Runtime.getRuntime().exec(cmd);  
  93. try  
  94. //等待编译器结束  
  95.    process.waitFor();  
  96. }  
  97. catch (InterruptedException e)  
  98. {  
  99. }  
  100. int val = process.exitValue();  
  101. if (val != 0)  
  102. {  
  103.    throw new RuntimeException("编译错误:" + "错误代码" + val);  
  104. }  
  105. }  
  106.   
  107. //以byte数组形式读入类文件  
  108. byte[] readBytes(String filename) throws IOException  
  109. {  
  110.    File classfile = new File(filename);  
  111. byte[] buf = new byte[(int) classfile.length()];  
  112. FileInputStream in = new FileInputStream(classfile);  
  113. in.read(buf);  
  114. in.close();  
  115. return buf;  
  116. }  
  117.   
  118. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值