您的位置:首页 > 编程语言 > Java开发

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: