读书人

Java源文件编译示范

发布时间: 2012-11-10 10:48:51 作者: rapoo

Java源文件编译示例

java程序编译java源文件

?

JavacCompile文件:

?

package yan.demo.javac;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.FileObject;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;


/**
?* java源文件
?*
?* @author Chen Ya Yuan
?* @Email chen.ya.yuan@163.com
?* @version V1.0
?*/
public class JavacCompile {
??? public static void main(String[] args) throws Exception {
??????
??????? compilePackage(
??????????????? Conf.readProperties("project.path")+ Conf.readProperties("src.testentity"),
??????????????? Conf.readProperties("project.libpath"),
??????????????? Conf.readProperties("project.path")+ Conf.readProperties("classes"),
??????????????? Conf.readProperties("project.sourcepath"));
??? }

??? /**
???? * 包及子包的所有
???? * @param fromPath
???? *??????????? 文件源
???? * @param libPath
???? *??????????? 目依Jar包源
???? * @param savePath
???? *??????????? 後class字文件保存路
???? * @param sourcePath
???? *??????????? 依源路
???? * @throws Exception
???? */
??? public static void compilePackage(String fromPath, String libPath,
??????????? String savePath,String sourcePath) throws Exception {
??????? // 要描的文件
??????? File file = new File(fromPath);
??????? // 存文件的
??????? ArrayList<File> allFile = ScanDirectory.getFile(file);
??????? String fromFile = "";

??????? // 设置编译选项
??????? Iterable<String> options = getCompileOptions(savePath, libPath, sourcePath);

??????? for (File f : allFile) {
??????????? if (f.getName().matches(".*\\.java$")) { // 匹配java格式的文件
??????????????? fromFile = f.getPath(); // java源文件 文件名
??????????????? // 文件
??????????????? compilejava(fromFile, options, savePath);
??????????? }
??????? }
??? }

??? /**
???? * java源文件
???? *
???? * @param fromFile
???? *??????????? java源文件
???? * @param libPath
???? *??????????? 目依Jar包源
???? * @param savePath
???? *??????????? 後class字文件保存路
???? * @throws Exception
???? */
??? public static void compilejava(String fromFile, Iterable<String> options,
??????????? String savePath) throws Exception {
??????? JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
??????? // 建立DiagnosticCollector象
??????? DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
??????? StandardJavaFileManager fileManager = compiler.getStandardFileManager(
??????????????? diagnostics, null, null);

??????? // 设置编译选项
??????? Iterable option = options;

??????? // 建立用於保存被文件名的象
??????? // 每文件被保存在一JavaFileObject承的中
??????? Iterable compilationUnits = fileManager
??????????????? .getJavaFileObjectsFromStrings(Arrays.asList(fromFile));

??????? JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager,
??????????????? diagnostics, option, null, compilationUnits);
??????? // 源程序
??????? boolean success = task.call();
??????? for(Diagnostic diagnostic :diagnostics.getDiagnostics()){
??????????? System.out.format(//格式化不成功?? 出的相信息
??????????????????? "Code: %s%n" +
??????????????????? "Kind: %s%n" +
??????????????????? "Position: %s%n" +
??????????????????? "Start Position: %s%n" +
??????????????????? "End Position: %s%n" +
??????????????????? "Source: %s%n" +
??????????????????? "Message:? %s%n",
??????????????????? diagnostic.getCode(), diagnostic.getKind(),
??????????????????? diagnostic.getPosition(), diagnostic.getStartPosition(),
??????????????????? diagnostic.getEndPosition(), diagnostic.getSource(),
??????????????????? diagnostic.getMessage(null)
??????????? );
???????????
??????? }

??????? fileManager.close();
??????? System.out.println((success) ? fromFile + "? 成功" : fromFile
??????????????? + "? 失");
??? }

??? /**
???? * 设置编译选项
???? *
???? * @param savePath
???? *??????????? 後class字文件保存路
???? * @param libPath
???? *??????????? 目依Jar包源
???? * @param sourcePath
???? *??????????? 依源路
???? * @return
???? */
??? public static Iterable<String> getCompileOptions(String savePath,
??????????? String libPath,String sourcePath) {
??????? List<String> options = new ArrayList<String>();
??????? File file = new File(savePath);
??????? if (!file.exists()) {
??????????? file.mkdirs(); // 路不存在,建
??????? }

??????? // 置後生成的文件的出目
??????? if (savePath != null) {
??????????? options.add("-d");
??????????? options.add(savePath);
??????? }

??????? // 置用到的第三方的目
??????? if (libPath != null) {
??????????? options.add("-classpath");
??????????? options.add(getLibJar(libPath));
??????? }
???????
??????? //指定查找入源文件的位置
??????? if(sourcePath!=null){
??????????? options.add("-sourcepath");
??????????? options.add(sourcePath);
??????? }
???????
??????? //出有器正在行的操作的信息
//??????? options.add("-verbose");

//??????? options.add("-target");
//??????? options.add("1.6");
???????
??????? return options;
??? }

??? private static String getLibJar(String libPath) {
??????? StringBuffer sb = new StringBuffer();
??????? // 要描的文件
??????? File file = new File(libPath);
??????? // 存文件的
??????? ArrayList<File> allFile = ScanDirectory.getFile(file);
??????? for (File f : allFile) {
??????????? if (f.getName().matches(".*\\.[jJ]+[aA]+[rR]+$")) { // 匹配jar格式的文件
??????????????? sb.append(libPath + File.separator + f.getName() + ";"); // 多jar文件用分隔
??????????? }
??????? }
???????
??????? return sb.substring(0, sb.lastIndexOf(";"));
??? }
???
??? public JavacCompile(){
???????
??? }

}

?

?

?

Conf文件:

?

package yan.demo.javac;

import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
?*
?* @author Chen Ya Yuan
?* @Email chen.ya.yuan@163.com
?* @version V1.0
?*/
public class Conf {
?// 根据key读取value
??? public static String readProperties(String key) {
??????? Properties props = new Properties();
??????? try {
??????????? String path = Conf.class.getResource("/").getPath();
??????????? FileInputStream fis =
??????????????? new FileInputStream(path+"config/Conf.chenyy.properties");
??????????? props.load(fis);
??????????? String property = props.getProperty(key.trim());
??????????? if(property!=null){
??????????????? property=property.trim();
??????????? }
//??????????? System.out.println(key + value);
??????????? return property;
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????????? return null;
??????? }
??? }
???
??? // 读取properties的全部信息
??? public static Map<String,Object> readAllProperties() {
??????? Properties props = new Properties();
??????? Map<String,Object> map = new HashMap<String,Object>();
??????? try {
??????????? FileInputStream fis = new FileInputStream(Conf.class.getResource("/").getPath()+"config/Conf.chenyy.properties");
??????????? props.load(fis);
??????????? Enumeration en = props.propertyNames();
???????????
??????????? while (en.hasMoreElements()) {
??????????????? String key = (String) en.nextElement();
??????????????? String property = props.getProperty(key.trim());
??????????????? if(property!=null){
??????????????????? property=property.trim();
??????????????? }
??????????????? map.put(key, property);
//??????????????? System.out.println(key + "?? " + property.trim());
??????????? }
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? return map;
??? }
???
??? public static void main(String[] args) {
??????? Map<String,Object> map = new HashMap<String,Object>();
??????? map = readAllProperties();
??????? for(Object object: map.keySet()){?
??????????? System.out.println("key:"+object+"?? value:"+map.get(object));
??????? }
??? }
???
}

?

?

Conf.chenyy.properties文件:


# the project path
project.path = E:/workspace/project/javacdemo
project.libpath = E:/workspace/project/javacdemo/lib

# set search -sourcepath
project.sourcepath = E:/workspace/project/javacdemo/src

#
src.testentity = /src/yan/demo/javac/test

#schema
schema0 = schema000

#schema1
schema1 = schema1111

#schema2
schema2 = schema2222

#
classes = /bin/classes

读书人网 >编程

热点推荐