Java Web部署到tomcat后,使用动态编译无法找到相关类的解决方案
一、问题
动态编译java文件时(这个java文件虽引用了第三方jar包),如果这个过程发生在java命令行程序中,则正常编译。如果发生在JavaWeb中,然后此Java部署到Tomcat之后,执行动态编译时,就会提示找不到相关类或者Jar。
二、解决方案
将所依赖到的Jar文件,复制到%JAVA_Home%\jre\lib\ext目录下。 例如:C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext 再重启Tomcat。
三、解决方法二:
编译时,添加classPath参数,即可,DEMO代码很乱,参考如下:
package servlets; import com.github.henryhuang.dynamiccompiler.ClassGenerator;
import domain.MyJavaSourceFromString;
import org.apache.commons.io.FileUtils; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try { String buildOutput = MyServlet.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File("D:\\Project\\JavaProject\\dynamicompileInWeb\\out\\artifacts\\web_war_exploded", "WEB-INF\\TestCode.java");
String code = FileUtils.readFileToString(f, "UTF-8");
resp.getWriter().write(code); // instantiate the Java compiler
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); // load the uploaded files into the compiler
List<JavaFileObject> files = new ArrayList<JavaFileObject>();
//files.add(new ByteArrayJavaFileObject(fullName, patchFile.getBytes())); files.add(new MyJavaSourceFromString("TestCode", code));
// set the classpath
List<String> options = new ArrayList<String>(); options.add("-classpath");
StringBuilder sb = new StringBuilder();
URLClassLoader urlClassLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
for (URL url : urlClassLoader.getURLs()) {
sb.append(url.getFile()).append(File.pathSeparator);
}
options.add(sb.toString());
options.add("-d");
options.add(buildOutput);
// execute the compiler
boolean isok = compiler.getTask(null, fileManager, null, options, null, files).call();
System.out.println(isok);
File root = new File(buildOutput);
if (!root.exists()) root.mkdirs();
URL[] urls = new URL[]{root.toURI().toURL()};
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
Class<?> clazz2 = Class.forName("TestCode", true, classLoader); // instantiate the class (FAILS HERE)
//Object instance = fileManager.getClassLoader(null).loadClass("TestCode").newInstance(); // close the file manager
fileManager.close(); //
//if (clazz != null) {
// resp.getWriter().write("\r\n" + clazz + ":compile success.");
//} else {
// resp.getWriter().write("\r\n" + clazz + ":compile failed.");
//} } catch (Exception ex) {
resp.getWriter().write(ex.getMessage());
} }
}
简单封装
package com.test.utils.compile; import org.apache.commons.io.FileUtils;
import com.test.utils.context.AppContext; import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List; public class ClassBuilder { public static Class<?> buildClass(String fullClassName, String codeFilePath) throws IOException, ClassNotFoundException { return buildClass(fullClassName, codeFilePath, "UTF-8", AppContext.baseDirectory());
} public static Class<?> buildClass(String fullClassName, String codeFilePath, String charsetName, String buildOutput) throws IOException, ClassNotFoundException {
try {
String code = FileUtils.readFileToString(FileUtils.getFile(codeFilePath), charsetName);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<JavaFileObject> files = new ArrayList<>();
files.add(new JavaSourceFromCodeString(fullClassName, code));
List<String> options = new ArrayList<>();
options.add("-classpath");
StringBuilder sb = new StringBuilder();
URLClassLoader urlClassLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
for (URL url : urlClassLoader.getURLs()) {
sb.append(url.getFile()).append(File.pathSeparator);
}
options.add(sb.toString());
options.add("-d");
options.add(buildOutput);
// execute the compiler
boolean isok = compiler.getTask(null, fileManager, null, options, null, files).call();
if (isok) {
File root = new File(buildOutput);
if (!root.exists()) root.mkdirs();
URL[] urls = new URL[]{root.toURI().toURL()};
ClassLoader classLoader = ClassBuilder.class.getClassLoader();
Class<?> clazz = Class.forName(fullClassName, true, classLoader);
return clazz;
}
return null;
} catch (Exception ex) {
throw ex;
}
}
}
Java Web部署到tomcat后,使用动态编译无法找到相关类的解决方案的更多相关文章
- Java Web 部署到Tomcat
1.在conf目录中,新建Catalina(注意大小写)\localhost目录,在该目录中新建一个xml文件,名字可以随意取,只要和当前文件中的文件名不重复就行了,该xml文件的内容为: <C ...
- eclipse配置tomcat,并部署一个Java web项目到tomcat上
引用链接:https://blog.csdn.net/cincoutcin/article/details/79408484 eclipse配置tomcat 1.windows——preference ...
- Java Project部署到Tomcat服务器上
所有的JAVA程序员,在编写WEB程序时,一般都通过工具如 MyEclipse,编写一个WEB Project,通过工具让这个WEB程序和Tomcat关联.其实在我们可以通过JAVA程序部署到Tomc ...
- springboot打war包后部署到tomcat后访问返回404错误
springboot打war包后部署到tomcat后访问返回404错误 1.正常情况下,修改打包方式为war <packaging>war</packaging> 2.启动类继 ...
- Java Web开发: Tomcat中部署项目的三种方法
web开发,在tomcat中部署项目的方法: 可以参考http://m.blog.csdn.net/blog/u012516903/15741727 定义$CATALINA_HOME指的是Tomcat ...
- myeclipse中的web项目导入到eclipse中注意事项,项目部署到tomcat后无法访问jsp文件
打开eclipse,点击空白处,右键可以看到import>general>existing projects into workspace>next>选择你的myeclipse ...
- Eclipse部署Java Web项目到Tomcat出错
1.今天,我打开Eclipse,准备将一个Java Web项目部署到Tomcat中, 结果弹出提示错误窗口,具体如下: (1)出错详情 Could not publish server configu ...
- 使用Maven自动部署Java Web项目到Tomcat问题小记
导读 首先说说自己为啥要用maven管理项目,一个直接的原因是:我在自己电脑上开发web项目,每次部署到服务器上时都要经历如下步骤: 首先在Eclipse里将项目打包成war包 将服务器上原来的项目文 ...
- web项目部署后动态编译无法找到依赖的jar包
很纳闷的一个问题,通过配置文件生成的java源码在本地动态编译没有问题,但是部署服务器后编译不通过,找不到依赖的jar包. 通过网上查资料,找到一个兄弟提供的方法,问题解决了:下面贴出代码以供参考: ...
随机推荐
- View Programming Guide for iOS_读书笔记
关于Window的定义的要点 Windows do not have any visible content themselves but provide a basic container for ...
- C#编程(四十八)----------列表
C#中的List C#中deList怎么样?List<T>类是ArrayList类的泛型等效类,该类使用大小可按需动态增长的数组实现List<T>泛型接口. 泛型的好处:它为使 ...
- 《嵌入式Linux开发实用教程》
<嵌入式Linux开发实用教程> 基本信息 作者: 朱兆祺 李强 袁晋蓉 出版社:人民邮电出版社 ISBN:9787115334831 上架时间:2014-2-13 出版日期: ...
- Orchard模块开发全接触2:新建 ProductPart
一:创建 Part 1:项目引用 Orchard.Framework: 2:创建 Models 文件夹: 3:在 Models 文件夹下创建类 ProductPartRecord,如下: public ...
- REQUEST_TIMEOUT 解决方案
you need to pass an npmrc file when you install the business network onto the peers. For more info s ...
- 字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“Today is Friday!”逆转为“Friday! is Today”.
字符串中单词的逆转,即将单词出现的顺序进行逆转.如将“Today is Friday!”逆转为“Friday! is Today”. #include<iostream> #include ...
- Zookeeper Tutorial 1 -- Overview
ZooKepper: 一个分布式应用的分布式协调服务(Distributed Coordination Service) 分布式服务难以管理, 他们容易造成死锁和竞争, ZooKepper的动机就是为 ...
- JUnit 入门
JUNIT了解学习 转自:关于Java单元测试,你需要知道的一切 转自:JUnit 入门教程 JUnit高级用法之@RunWith @RunWith 关于@RunWith注解,官方文档是这么描述的: ...
- NoSQL数据存储
些数据库并不是关系型的,不支持 SQL.它们用来处理庞大的数据集.支持更加灵活的 数据定义以及定制的数据操作.这些被统称为 NoSQL(not only SQL) . dbm family dbm格式 ...
- [转]HTTPS网络流量解密方法探索系列(一)
前言 分析网络流量总是绕不开HTTPS,因其广泛使用甚至是强制使用逐渐被大众熟知,在保证其安全的同时也提高了对流量进行研究的难度.目前解析HTTPS协议的文章很多,有很多不错的文章可以带着入门,老实说 ...