jdk/lib目录下的tool.jar包含了一个完整的java编译器包,利用它可以实现类文件的运行时动态生成并且编译加载。下面是一个例子:
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = todayClass + ".java";
public static void main (String args[]){
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":/n/n");
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(/""+todayMillis+"/");");
aWriter.write(" }}/n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = { new String(todaySource)};
ByteArrayOutputStream baos= new ByteArrayOutputStream();
new sun.tools.javac.Main(baos,source[0]).compile(source);
// if using JDK >= 1.3 then use
// public static int com.sun.tools.javac.Main.compile(source);
return (baos.toString().indexOf("error")==-1);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
MyEclipse开发几个常用的包,我也是边用边收集的,很多MyEclipse会自带
一款好用的shiro检测利用工具,使用方式java -jar shiro_tool.jar https://xx.xx.xx.xx,Github有开源下载链接,在这里上传是为了赚积分下载别的工具,欢迎使用
解决appium-doctor报bundletool.jar cannot be found问题 解决appium-doctor报bundletool.jar cannot be found问题 解决appium-doctor报bundletool.jar cannot be found问题 解决appium-doctor报bundletool.jar ...
JDK8.0,含tools.jar和dt.jar,可能直接安装,里面有JRE,只要设置好环境变量,就可以进行开发。
springBoot的pom头部报tools.jar的错所需要的jar包 错误为Missing artifact com.sun:tools:jar:1.8.0
JDK tools.jar
tool.zip后台生成echarts图片工具
JDK1.6中的tools.jar //呵呵,今晚JCreator出错无数,检查了好久,才发现原来是我的tools.jar不见了,痛定思痛,决定将其收藏!
Eclipse文件转码插件
hibernate-tool.jar 这个jar文件用来和ANT搭配由hbm.xml自动生成数据库schema,和java类
在以前的jdk版本中,只要安装jdk文件就会在本地自动生成jdk和jre两个文件,但是在新的jdk版本中没有自动生成jre文件,也没有tools.jar包。没办法就下载放进去了,不知道有没有其他的办法解决这个问题。
tool.jar下载Unable to create schema compiler需要加载tool.jar的同学们。
FileTool.dll_已编译, filetool.dll文件是一款重要的系统文件,当电脑出现“filetool.dll注册失败”或“filetool.dll无法加载”时,手动注册filetool.dll文件重新下载安装可解决该问题。
com.lifesting.tool.encoding_1.0.0.jar.rar;eclipse批量转码工具配置使用说明书
使用方式:java -jar xxx.jar usage: parquet-tools cat [option...] where option is one of: --debug Enable debug output -h,--help Show this help string -j,--json Show records in JSON format. --no-...
jdk1.8 的tool.jar ,配置spring项目是时缺少,分享给大家
3. 找到FileTool.dll 4. 将FileTool.dll复制到 5. 右键\Bin\MSDEV.exe 通过设置MSDEV.exe文件,右键–>“属性”–>“兼容性” 6. powershell(管理员)上输入:FileTool.dll" 7. 打开vc++ ,添加FileTool.dll 补充...
com.lifesting.tool.encoding_1.0.0.jar(其他的不用管,只需要复制com.lifesting.tool.encoding_1.0.0.jar这个jar包就行) jar文件,拷贝到eclipse/plugins目录下,然后重启eclipse。 如下图。重启后选择项目——...
pdfbox包(pdfbox-2.0.8.jar,fontbox-2.0.8.jar,pdfbox-tools-2.0.8.jar)
heapdump_tool.jar