动态编译 Java 文件 与 动态加载 Java 类
2016-01-25 16:35
686 查看
原文:http://hi.baidu.com/helloween0809/blog/item/709c8126e7edb2148a82a12e.html动态编译java文件 动态加载java类 动态执行java类的main方法。
动态编译需要加载 JDK/lib/tools.jar
下面是代码: 点击(此处)折叠或打开
/*
* IBM Confidential
*
* OCO Source Materials
*
* #ID# IBM CRL Supply Chain Management Research
*
* (C) Copyright IBM Corp. 2005, 2006
*
* The source code for this program is not published or otherwise divested of
* its trade secrets.
*
*/
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class CompileTest
{
public static void main(String[] args)
throws Exception
{
String className = "Test";
File file = new File(new File(System.getProperty("user.dir")),
className + ".java");
StringBuffer sb = new StringBuffer();
sb.append("public class " + className + " { \r\n");
sb.append(" public static void main(java.lang.String[] args) throws Exception { \r\n");
sb.append(" System.out.println(\"This is a class compiled. ");
sb.append(" class: \" + " + className + ".class.newInstance()); \r\n");
sb.append(" } \r\n");
sb.append("} \r\n");
FileOutputStream ous = new FileOutputStream(file);
ous.write(sb.toString().getBytes());
ous.close();
String[] arguments = new String[] { "-d",
System.getProperty("user.dir"), className + ".java" };
// Compile
int result = com.sun.tools.javac.Main.compile(arguments);
System.out.println(result == 0 ? "Compiled successly" : "Failed");
URL classpath = new URL("file:/" + System.getProperty("user.dir") + "/");
URLClassLoader classLoader = new URLClassLoader(new URL[] { classpath });
// Load
Class testClass = classLoader.loadClass(className);
Method mainMethod = testClass.getMethod("main",
new Class[] { String[].class });
// Invoke main(String[] args)
System.out.println(mainMethod);
testClass.newInstance();
mainMethod.invoke(null, new Object[] { null });
}
}
运行结果:
Compiled successly public static void Test.main(java.lang.String[]) throws java.lang.Exception This is a class compiled. class: Test@fc9944
阅读(646) | 评论(0) | 转发(1) |
0
上一篇:Function importPackage must be called with a package; had "[JavaClass java.util.ArrayList]"
下一篇:【转载】java class运行时热替换(hotswap)
相关热门文章
saltstack批量部署并配置nginx...
开源负载均衡LVS随机自启动异...
jsp嵌套
BLE-NRF51822教程9—动态密码(...
BLE-NRF51822教程8-动态广播...
Tomcat 6 配置SSI
tomcat + ssi
JDK1.6官方下载_JDK6官方下载_...
Java 判断文件夹、文件是否存...
如何正确(完美)卸载Java/JDK/J...
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
动态编译需要加载 JDK/lib/tools.jar
下面是代码: 点击(此处)折叠或打开
/*
* IBM Confidential
*
* OCO Source Materials
*
* #ID# IBM CRL Supply Chain Management Research
*
* (C) Copyright IBM Corp. 2005, 2006
*
* The source code for this program is not published or otherwise divested of
* its trade secrets.
*
*/
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class CompileTest
{
public static void main(String[] args)
throws Exception
{
String className = "Test";
File file = new File(new File(System.getProperty("user.dir")),
className + ".java");
StringBuffer sb = new StringBuffer();
sb.append("public class " + className + " { \r\n");
sb.append(" public static void main(java.lang.String[] args) throws Exception { \r\n");
sb.append(" System.out.println(\"This is a class compiled. ");
sb.append(" class: \" + " + className + ".class.newInstance()); \r\n");
sb.append(" } \r\n");
sb.append("} \r\n");
FileOutputStream ous = new FileOutputStream(file);
ous.write(sb.toString().getBytes());
ous.close();
String[] arguments = new String[] { "-d",
System.getProperty("user.dir"), className + ".java" };
// Compile
int result = com.sun.tools.javac.Main.compile(arguments);
System.out.println(result == 0 ? "Compiled successly" : "Failed");
URL classpath = new URL("file:/" + System.getProperty("user.dir") + "/");
URLClassLoader classLoader = new URLClassLoader(new URL[] { classpath });
// Load
Class testClass = classLoader.loadClass(className);
Method mainMethod = testClass.getMethod("main",
new Class[] { String[].class });
// Invoke main(String[] args)
System.out.println(mainMethod);
testClass.newInstance();
mainMethod.invoke(null, new Object[] { null });
}
}
运行结果:
Compiled successly public static void Test.main(java.lang.String[]) throws java.lang.Exception This is a class compiled. class: Test@fc9944
阅读(646) | 评论(0) | 转发(1) |
0
上一篇:Function importPackage must be called with a package; had "[JavaClass java.util.ArrayList]"
下一篇:【转载】java class运行时热替换(hotswap)
相关热门文章
saltstack批量部署并配置nginx...
开源负载均衡LVS随机自启动异...
jsp嵌套
BLE-NRF51822教程9—动态密码(...
BLE-NRF51822教程8-动态广播...
Tomcat 6 配置SSI
tomcat + ssi
JDK1.6官方下载_JDK6官方下载_...
Java 判断文件夹、文件是否存...
如何正确(完美)卸载Java/JDK/J...
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- Eclipse启动报错:no java virtual machine was found after searching
- 解决java compiler level does not match the version of the installed java project facet的办法
- Eclipse启动报错:no java virtual machine was found after searching
- 用VC + Eclipse调试jni的native代码
- Java获取DB元数据
- Java获取DB元数据
- 用VC + Eclipse调试jni的native代码
- Java nio epoll mina
- Eclipse中,打开文件所在文件夹的插件,及设置
- Java nio epoll mina
- Eclipse中,打开文件所在文件夹的插件,及设置
- Java笔记
- 【转】java 中异常处理原则
- Java中的serialize接口与transient关键字
- 【转】java 中异常处理原则
- Java中的serialize接口与transient关键字
- Java笔记
- jdk环境变量配置:java.lang.NoClassDefFoundError
- 【转】Java命令行运行参数说明大全(偷来的)
- jdk环境变量配置:java.lang.NoClassDefFoundError