通过Runtime调用dos命令实现java动态编译

最近项目需要实现自定义生成java类与jsp页面,网上搜索很多资料对java类编译这块大多数使用jdk1.6的新特性compiler API进行编译,最后因为服务器环境为weblogic10.0无法使用jdk1.6,而放弃使用,改成使用调用dos命令的方法,在此总结一下,便于以后查阅。

一,路径

1.获取服务器路径

        String filePath= Thread.currentThread().getContextClassLoader()

.getResource("").getPath();

filePath= filePath.split("classes")[0];

filePath= filePath.replaceAll("%20", " ");

2.类存放路径

String fileName = filePath+"classes/com/vingsoft/res/entity/TEST.java";

 

 

二,java类拼接

 

String source = "package com.vingsoft.res.entity;"
				+ rt
				// +"import java.util.Date;"+rt
				+ "import javax.persistence.Column;"
				+ rt
				+ "import javax.persistence.Entity;"
				+ rt
				+ "import javax.persistence.GeneratedValue;"
				+ rt
				+ "import javax.persistence.GenerationType;"
				+ rt
				+ "import javax.persistence.Id;"
				+ rt
				+ "import javax.persistence.SequenceGenerator;"
				+ rt
				+ "import javax.persistence.Table;"
				+ rt // 导入需要的架包

				
				+ "public class TEST"
				+ rt
				+ "{"
				+ rt
				+ "private Long uuid;"+ rt
				+ "public Long getUuid() {" + rt
				+ "return uuid;" + rt + "}" + rt
				+ "public void setUuid(Long uuid) {" + rt + "this.uuid = uuid;"
				+ rt + "}" + rt;
//... 省略其他动态生成

source += "}";

  三,流输入文件(UTF-8格式,防止乱码)

/**

	 * 创建UTF格式文件
	 * 
	 * @author 大龙
	 * @date 2012-11-1 下午05:35:22
	 * @param fileName生成文件路径
	 * @param source
	 *            文件内容
	 * @return
	 */
	public static boolean writeUTFFile(String fileName, String source) {
		boolean bool = false;
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			fos = new FileOutputStream(fileName);
			osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(source);
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
			bool = false;
		} finally {
			if (osw != null) {
				try {
					osw.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}

			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return bool;
	}
 

四,编译java类

1.String cp为类中需要调用的jar包路径,引用的class文件直接引用到classes即可

2.getSystemByFile方法是配置了system.properties属性,因为在linux系统下;要换成:

 

	/**
	 * 编译java类
	 * 
	 * @author 大龙
	 * @date 2012-11-1 下午05:45:56
	 * @param filePath
	 *            D:/develop/tomcat/webapps/com/WEB-INF
	 * @param fileName
	 */
	public static boolean toCreateClass(String filePath, String fileName) {
		// 得到写文件路径
		// filePath = "D:/develop/tomcat/webapps/com/WEB-INF";
		// fileName = "/controller/T_CATA_CEController";
		String path = filePath + "lib";// 架包路径
		String classPath = filePath + "classes";// 编译类路径
		
		try {
			Runtime rt = Runtime.getRuntime();
			rt.exec("javac -cp "+getSystemByFile(path,classPath)+" "+filePath + fileName);
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			return false;
		}
		
	}

	/**
	 * 获取系统环境
	 * @author 大龙
	 * @date 2012-11-21 上午09:34:08
	 * @param path
	 * @param classPath
	 * @return
	 */
	private static String getSystemByFile(String path,String classPath){
		String cp=path
		+ "/spring-beans-3.0.5.RELEASE.jar;"
		+ path
		+ "/spring-jdbc-3.0.5.RELEASE.jar;"
		+ path
		+ "/spring-context-3.0.5.RELEASE.jar;"
		+ path
		+ "/iframework-1.0.6.jar;"
		+ path
		+ "/spring-tx-3.0.5.RELEASE.jar;"
		+ path
		+ "/hibernate-jpa-2.0-api-1.0.0.Final.jar;"
		+ path
		+ "/hibernate-core-3.6.1.Final.jar;"
		// controller
		+ path
		+ "/servlet-api-2.3.jar;"
		+ path
		+ "/spring-web-3.0.5.RELEASE.jar;"
		+ path
		+ "/iframework-1.0.6.jar;"
		+ path
		+ "/commons-lang-2.6.jar;"
		// classes类
		+ classPath // 引入的类文件
		;
		
		Properties prop = new Properties();   
        try {   
        	InputStream in = new BufferedInputStream(new FileInputStream(classPath+"/system.properties")); 
            prop.load(in);   
            String sys=prop.getProperty("system").trim();   
            if(sys.equals("linux")){
        		cp=cp.replaceAll(";", ":");
            }
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        System.out.println("引用包:"+cp);
		return cp;	
	}			
 

 

大龙

2012/11/21

 

 

 

 

 

 

 

 

你可能感兴趣的:(java动态编译,jdk1.5下动态编译,流文件UTF-8格式)