动态编译 Java 文件 与 动态加载 Java 类

动态编译java文件 动态加载java类 动态执行java类的main方法。

动态编译需要加载 JDK/lib/tools.jar

下面是代码:

点击(此处)折叠或打开

  1. /*
  2.  * IBM Confidential
  3.  *
  4.  * OCO Source Materials
  5.  *
  6.  * #ID# IBM CRL Supply Chain Management Research
  7.  *
  8.  * (C) Copyright IBM Corp. 2005, 2006
  9.  *
  10.  * The source code for this program is not published or otherwise divested of
  11.  * its trade secrets.
  12.  *
  13.  */
  14.  package test;

  15. import java.io.File;
  16.  import java.io.FileOutputStream;
  17.  import java.lang.reflect.Method;
  18.  import java.net.URL;
  19.  import java.net.URLClassLoader;

  20. public class CompileTest
  21.  {

  22.     public static void main(String[] args)
  23.              throws Exception
  24.      {
  25.          String className = "Test";

  26.         File file = new File(new File(System.getProperty("user.dir")),
  27.                  className + ".java");

  28.         StringBuffer sb = new StringBuffer();

  29.         sb.append("public class " + className + " { \r\n");
  30.          sb.append(" public static void main(java.lang.String[] args) throws Exception { \r\n");
  31.          sb.append(" System.out.println(\"This is a class compiled. ");
  32.          sb.append(" class: \" + " + className + ".class.newInstance()); \r\n");
  33.          sb.append(" } \r\n");
  34.          sb.append("} \r\n");

  35.         FileOutputStream ous = new FileOutputStream(file);
  36.          ous.write(sb.toString().getBytes());
  37.          ous.close();

  38.         String[] arguments = new String[] { "-d",
  39.                  System.getProperty("user.dir"), className + ".java" };

  40.         // Compile
  41.          int result = com.sun.tools.javac.Main.compile(arguments);

  42.         System.out.println(result == 0 ? "Compiled successly" : "Failed");

  43.         URL classpath = new URL("file:/" + System.getProperty("user.dir") + "/");

  44.         URLClassLoader classLoader = new URLClassLoader(new URL[] { classpath });

  45.         // Load
  46.          Class testClass = classLoader.loadClass(className);

  47.         Method mainMethod = testClass.getMethod("main",
  48.                  new Class[] { String[].class });

  49.         // Invoke main(String[] args)
  50.          System.out.println(mainMethod);
  51.          
  52.          testClass.newInstance();

  53.         mainMethod.invoke(null, new Object[] { null });

  54.     }
  55.  }



运行结果:

Compiled successly
 public static void Test.main(java.lang.String[]) throws java.lang.Exception
 This is a class compiled.              class: Test@fc9944

转载于:https://www.cnblogs.com/black/p/5171783.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值