java中如何使用jdk1.5和jdk1.6进行项目打包
2012-06-21 08:55
501 查看
1、jdk1.6中调用java的打包工具的关键代码:
/**
* 编译java类
* @param file java文件列表
* @param dir 编译后calss文件存放目录
* @throws IOException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public void compile(String dir,String errorFile,StringBuffer lib,String source,File...file) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String version=System.getProperty("java.version");
if(compiler==null)
{//在运行环境中JRE不包含JDK中的工具包tools.jar必须重新加载JDK中LIB下面的tools.jar工具包
System.out.println("load JavaCompiler...");
compiler=Loader.loadCompile().newInstance();//加载编译器并且实例化
if(compiler==null)
{//JDK 工具包tools.jar 没有找到
System.out.println("[../jdk"+version+"/lib/tools.jar] not found");
new JOptionPane().showMessageDialog(null, "jdk"+version+"/lib/tools.jar未找到");
System.exit(0);//程序退出
//return;
}
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
//java文件管理器
StandardJavaFileManager fileManager = compiler
.getStandardFileManager(diagnostics, null, null);
//指定class文件存放目录
//-d是指定编译好的class文件目录,-source指定兼容版本,-cp指定引用类文件,-target是编译后的class文件的版本,-encoding是使用的编码,-sourcepath是java源文件引用其他目录下面的java源文件
lib.append(LoaderLibrary.getLibraryAllFiles());
Iterable<String> options=Arrays.asList("-d",dir,"-nowarn","-Xlint:none","-source","1.5","-target",version.substring(0, 3),"-encoding","utf-8","-cp",lib.toString(),"-sourcepath",source);
//设置编译的java文件列表
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjects(file);
JavaCompiler.CompilationTask task = compiler.getTask(null,
fileManager, diagnostics, options,
null, compilationUnits);
boolean rs=task.call();//执行编译
for(Diagnostic info:diagnostics.getDiagnostics())
{//输出日志信息
System.out.println("message:"+info.getMessage(null));
}
if(rs)
{
System.out.println("成功编译了"+file.length+"个JAVA文件");
}
else
{
System.out.println("本次编译失败");
}
fileManager.close();
}
2、jdk1.5中调用java的打包工具的关键代码:
/**
* 编译java类
* @param file java文件列表
* @param dir 编译后calss文件存放目录
* @throws IOException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws
* @throws
* @throws SecurityException
* @throws IllegalArgumentException
*/
public void compile(String dir,String errorFile,StringBuffer lib,String source,File...file) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException
{
try {
String version=System.getProperty("java.version");
lib.append(LoaderLibrary.getLibraryAllFiles());
String[] options=new String[]{"-d",dir,"-nowarn","-Xlint:none","-source","1.5","-target",version.substring(0, 3),"-encoding","utf-8","-cp",lib.toString(),"-sourcepath",source};
String[] list=processFile(file,options);
Class compilerClass=loadCompile();//编译器类
Class contextClass=Class.forName("com.sun.tools.javac.util.Context",true,compilerClass.getClassLoader());
Object compiler=compilerClass.getConstructor(String.class).newInstance("javac");//编译器实例
Method method=compilerClass.getMethod("compile", String[].class,contextClass);//编译方法
//指定class文件存放目录
int rs=(Integer)method.invoke(compiler, list,contextClass.newInstance());//执行编译
if(rs==0)
{
System.out.println("成功编译了"+file.length+"个JAVA文件");
}
else
{
System.out.println("本次编译失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 编译java类
* @param file java文件列表
* @param dir 编译后calss文件存放目录
* @throws IOException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public void compile(String dir,String errorFile,StringBuffer lib,String source,File...file) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String version=System.getProperty("java.version");
if(compiler==null)
{//在运行环境中JRE不包含JDK中的工具包tools.jar必须重新加载JDK中LIB下面的tools.jar工具包
System.out.println("load JavaCompiler...");
compiler=Loader.loadCompile().newInstance();//加载编译器并且实例化
if(compiler==null)
{//JDK 工具包tools.jar 没有找到
System.out.println("[../jdk"+version+"/lib/tools.jar] not found");
new JOptionPane().showMessageDialog(null, "jdk"+version+"/lib/tools.jar未找到");
System.exit(0);//程序退出
//return;
}
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
//java文件管理器
StandardJavaFileManager fileManager = compiler
.getStandardFileManager(diagnostics, null, null);
//指定class文件存放目录
//-d是指定编译好的class文件目录,-source指定兼容版本,-cp指定引用类文件,-target是编译后的class文件的版本,-encoding是使用的编码,-sourcepath是java源文件引用其他目录下面的java源文件
lib.append(LoaderLibrary.getLibraryAllFiles());
Iterable<String> options=Arrays.asList("-d",dir,"-nowarn","-Xlint:none","-source","1.5","-target",version.substring(0, 3),"-encoding","utf-8","-cp",lib.toString(),"-sourcepath",source);
//设置编译的java文件列表
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjects(file);
JavaCompiler.CompilationTask task = compiler.getTask(null,
fileManager, diagnostics, options,
null, compilationUnits);
boolean rs=task.call();//执行编译
for(Diagnostic info:diagnostics.getDiagnostics())
{//输出日志信息
System.out.println("message:"+info.getMessage(null));
}
if(rs)
{
System.out.println("成功编译了"+file.length+"个JAVA文件");
}
else
{
System.out.println("本次编译失败");
}
fileManager.close();
}
2、jdk1.5中调用java的打包工具的关键代码:
/**
* 编译java类
* @param file java文件列表
* @param dir 编译后calss文件存放目录
* @throws IOException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws
* @throws
* @throws SecurityException
* @throws IllegalArgumentException
*/
public void compile(String dir,String errorFile,StringBuffer lib,String source,File...file) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException
{
try {
String version=System.getProperty("java.version");
lib.append(LoaderLibrary.getLibraryAllFiles());
String[] options=new String[]{"-d",dir,"-nowarn","-Xlint:none","-source","1.5","-target",version.substring(0, 3),"-encoding","utf-8","-cp",lib.toString(),"-sourcepath",source};
String[] list=processFile(file,options);
Class compilerClass=loadCompile();//编译器类
Class contextClass=Class.forName("com.sun.tools.javac.util.Context",true,compilerClass.getClassLoader());
Object compiler=compilerClass.getConstructor(String.class).newInstance("javac");//编译器实例
Method method=compilerClass.getMethod("compile", String[].class,contextClass);//编译方法
//指定class文件存放目录
int rs=(Integer)method.invoke(compiler, list,contextClass.newInstance());//执行编译
if(rs==0)
{
System.out.println("成功编译了"+file.length+"个JAVA文件");
}
else
{
System.out.println("本次编译失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
相关文章推荐
- 【jar】JDK将单个的java文件打包为jar包,并引用到项目中使用【MD5加密】
- 如何使用jar命令对java工程进行打包
- Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.ja
- 使用maven插件对java工程进行打包
- 使用MyEclipse开发Java EE应用:如何配置一个独立的Java项目
- JDK1.5 JDK1.6 JDK1.7 + JAVA帮助文档全系列官方中英完整版下载
- java项目加密,使用Proguard进行项目混淆加密
- 黑马程序员 Java JDK1.5与JDK1.6的新特性
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
- eclipse 如何对maven项目进行打包?
- 【Java】如何使用java synchronized进行线程同步
- eclipse下使用java api 进行hbase的常用的操作所需的jar包,以及如何查看java build path下的jar是否使用
- BeJavaGod - 如何正确使用数据字典进行分类统一操作(一)
- IDEA中如何使用Maven进行打包。 IDEA版本是14
- 如何使用tomcat建立简单的java web项目
- 如何使用matalb中的deploytool函数打包jar包嵌入eclipse供java程序调用
- 讲述如何使用GitHub进行项目托管
- 教你如何使用webpack打包你的项目
- Java基础JDK环境变量设置及使用记事本进行简单的Java程序开发
- 图解教你如何使用ANT打包java程序