1.环境
JDK6
2.代码
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import com.yuan.common.file.DefaultFilenameFilter;
public class CompileUtil {
private static String JAVA_SOURCE_FILE = "d:/DynamicObject.java";
private static String JAVA_CLASS_NAME = "DynamicObject";
private static List<File> sourceList = new ArrayList<File>(); //源文件列表
private static final String BASEPATH = "d:/tmp";
public static synchronized void defaultCompile(String basePath)throws Exception{
File f = new File(basePath);
String sourcePath = f.getAbsolutePath()+File.separator+"src";
String targetPath = f.getAbsolutePath()+File.separator+"bin";
String libPath = f.getAbsolutePath()+File.separator+"lib";
compilePackage(sourcePath,targetPath,libPath);
}
/**
* 编译包及其子包
* @param sourcePath String
* @param targetPath String 编译后的类文件的输出文件夹
* @param libPath String 放置第三方类库的文件夹
* @throws Exception
*/
public static synchronized void compilePackage(String sourcePath , String targetPath , String libPath)throws Exception{
sourceList.clear();
compileSourceFile(getSourceList(sourcePath),targetPath,libPath);
}
private static List<File> getSourceList(String dirPath)throws Exception{
File dir = new File(dirPath);
File[] files = dir.listFiles(new DefaultFilenameFilter("java"));
for(File f:files){
if(f.isDirectory()){//如果是文件夹则迭代
getSourceList(dirPath+File.separator+f.getName());
}else{
sourceList.add(f);
}
}
return sourceList;
}
/**
* 编译单个Java源文件
* @param fileName String
* @param targetPath String
* @param libPath String
*/
public static void compileSourceFile(final String fileName , String targetPath , String libPath){
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
try {
Iterable<? extends JavaFileObject> sourcefiles = fileManager.getJavaFileObjects(fileName);
compiler.getTask(null, fileManager, null, getCompileOptions(targetPath,libPath), null, sourcefiles).call();
fileManager.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param files List<File>
* @param targetPath String 目标文件夹
* @param libPath String 放置第三方类库的文件夹
*/
private static void compileSourceFile(List<File> files , String targetPath , String libPath){
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
try {
Iterable<? extends JavaFileObject> sourcefiles = fileManager.getJavaFileObjectsFromFiles(files);
compiler.getTask(null, fileManager, null, getCompileOptions(targetPath,libPath), null, sourcefiles).call();
fileManager.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 设置编译选项
* @param targetPath String
* @param libPath String
* @return
*/
private static Iterable<String> getCompileOptions(String targetPath , String libPath){
List<String> options = new ArrayList<String>();
//设置编译后生成的类文件的输出目录
if(targetPath != null){
options.add("-d");
options.add(targetPath);
}
//设置用到的第三方类库的目录
if(libPath != null){
options.add("-classpath");
options.add(getLibStr(libPath));
}
return options;
}
private static String getLibStr(String libPath){
StringBuffer sb = new StringBuffer();
File libDir = new File(libPath);
File[] jarFiles = libDir.listFiles(new DefaultFilenameFilter("jar"));
for(File f:jarFiles){
if(!f.isDirectory()){
sb.append(libPath+File.separator+f.getName()+";"); //多个jar文件用分号隔开
}
}
return sb.substring(0, sb.lastIndexOf(";"));
}
}
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import com.yuan.common.util.AssertUtil;
public class DefaultFilenameFilter implements FilenameFilter {
private boolean ignoreDir = false; //是否忽略文件夹,默认不忽略
private List<String> extList = new ArrayList<String>();
public DefaultFilenameFilter(final String ext){
this.extList.add("."+ext);
}
public DefaultFilenameFilter(final String... exts){
if((exts != null) && (exts.length > 0)){
for(String ext : exts){
this.extList.add("."+ext);
}
}
}
public DefaultFilenameFilter(final String ext, boolean ignoreDir){
this.ignoreDir = ignoreDir;
this.extList.add("."+ext);
}
public void addExt(final String ext){
this.extList.add("."+ext);
}
public boolean accept(File dir, String name) {
if(ignoreDir){
if(new File(dir+File.separator+name).isDirectory()){
return false;
}else{
return endsWithExt(name);
}
}else{
return endsWithExt(name)||new File(dir+File.separator+name).isDirectory();
}//if
}//accept
protected boolean endsWithExt(String name){
if(!AssertUtil.notEmpty(extList)){
return true;
}
for(String ext : extList){
if(name.endsWith(ext)){
return true;
}
}
return false;
}
}
import java.util.Collection;
import java.util.Map;
public class AssertUtil {
public static boolean notEmpty(Object[] array) {
return (array != null) && (array.length > 0);
}
public static boolean notEmpty(Collection<?> collection) {
return (collection != null) && (!collection.isEmpty());
}
public static boolean notEmpty(Map<?,?> map) {
return (map != null) && (!map.isEmpty());
}
public static void main(String args[]){
}
}
3. 测试
public static void main(String[] args) throws Exception{
defaultCompile("d:/tmp");
compilePackage("d:/tmp/src", "d:/tmp/bin","d:/tmp/lib");
}// main