1 Star 0 Fork 22

三良/WebJava

forked from 老馬/WebJava 
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
TplCompile.java 2.18 KB
Copy Edit Raw Blame History
Ma authored 2015-06-12 09:41 +08:00 . 添加Demo
package Web;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
/**
* 动态编译Java文件
*
* @author Ma
*/
public class TplCompile {
public String TemplatePath = Options.TemplatePath;
/*
public static void main(String[] args) {
TplCompile Compiler = new TplCompile();
Compiler.setTemplatePath();
Compiler.run("dd/test.html");
}*/
public void setTemplatePath() {
this.TemplatePath = Options.TemplatePath;
}
/**
* 开始编译
*/
public Object run(String fileName) {
this.setTemplatePath();//先配置路径
TemplateCompile templateCompile = new TemplateCompile();
int r = templateCompile.start(fileName);
if (r > 0 && r == 1) {
//1表示说明文件不存在
//0表示解析正常
return null;
}
String javaFile = this.TemplatePath + templateCompile.getFilePath(fileName) + "/" + templateCompile.getfileName(fileName) + "Template.java";
String classPath = this.getClass().getResource("/").getPath();
//动态编译
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
int status = javac.run(null, null, null, "-d", classPath, javaFile);
if (status != 0) {
System.out.println("没有编译成功!");
}
//动态执行
try {
//返回与带有给定字符串名的类 或接口相关联的 Class 对象。
Class cls = Class.forName("Template." + templateCompile.getFilePath(fileName).replace("/", ".") + templateCompile.getfileName(fileName) + "Template");
BaseTemplate object = (BaseTemplate) cls.newInstance();
return object;
} catch (ClassNotFoundException ex) {
Logger.getLogger(TplCompile.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(TplCompile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(TplCompile.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/sanliang/WebJava.git
git@gitee.com:sanliang/WebJava.git
sanliang
WebJava
WebJava
master

Search