很纳闷的一个问题,通过配置文件生成的java源码在本地动态编译没有问题,但是部署服务器后编译不通过,找不到依赖的jar包。

通过网上查资料,找到一个兄弟提供的方法,问题解决了;下面贴出代码以供参考:

package com.songxingzhu.utils.compile;
import org.apache.commons.io.FileUtils;
import com.songxingzhu.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;
        }
    }
}
 
个人理解:部署后不像eclipse编译有.classpath指向依赖的所有的jar,需要自己组装类似classpath,指定jar包路劲。重点是设置option,不懂option的可以通过cmd命令查看javac的参数介绍。
其中,option.add("-classpath");就是类似直接运行javac -classpath 类文件

web项目部署后动态编译无法找到依赖的jar包的更多相关文章

  1. java项目部署后的文件路径获取

    //eclipse部署工程 String path = request.getServletContext().getRealPath( File.separator+ "WEB-INF&q ...

  2. Linux06 /Python web项目部署

    Linux06 /Python web项目部署 目录 Linux06 /Python web项目部署 1. 部署方式 2. 纯后端代码部署/CRM为例 1. 部署方式 2. crm项目详细部署步骤 3 ...

  3. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

  4. eclipse修改web项目部署路径 wtpwebapps webapps 的设置

    eclipse修改web项目部署路径 wtpwebapps   webapps  的设置,在添加完server------>tomcat后,到server控制台进行设置 eclipse默认的部署 ...

  5. 配置JDK、tomcat及Java Web项目部署

    一.JDK的安装 (1)下载安装JDK: 这个就不用说了,直接官网下载jdk安装即可.http://www.oracle.com/technetwork/java/javaee/downloads/i ...

  6. Eclipse导入git上的maven web项目 部署

    1 Eclipse中导入Git的maven项目 方法1: (1)首先当然是拉代码. 在Eclipse里面有个Git Repositories Exploring.就是Git仓库,clone a git ...

  7. web项目部署在不同环境中需要修改配置文件的解决方法

    web项目部署中存在的配置文件问题: web项目以war包的形式,部署在tomcat中,同时项目需要访问一些其他的东东,例如访问数据库,调用别的项目的API.在开发中,这些需要访问的外部地址通常以配置 ...

  8. Eclipse导入git上的maven web项目 部署 - lpshou

    http://www.tuicool.com/articles/fqm2Qf   推酷 文章 微博 主题 站点 活动 应用 周刊 登录   Eclipse导入git上的maven web项目 部署 - ...

  9. Eclipse复制或修改项目后,把项目部署后发现还是原来的项目名称

    Eclipse复制或修改项目后,把项目部署后发现还是原来的项目名称 解决: 到项目根目录打开.setting文件夹,找到"org.eclipse.wst.common.component&q ...

随机推荐

  1. 部署asp.net网站的小问题

    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG   web.config  修改 trust level="Full"

  2. 使用gdb调试c/c++代码

    转自 http://blog.csdn.net/haoel/article/details/2879 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较 ...

  3. 破解 Navicat Premium 12

    一.下载 若文件百度云链接失效,请发邮件给博主:1766211120@qq.com 1.安装文件下载 v12.0.11(x64)版本下载地址如下 链接:https://pan.baidu.com/s/ ...

  4. 解决Linux Kettle出现闪退问题

    linux环境, 运行sh spoon.sh打开图形化界面时经常出现闪退情况. 报错信息如下: cfgbuilder - Warning: The configuration parameter [o ...

  5. oracle Instant Client install

    Installation See the Instant Client Home Page for more information. Installation of ZIP files: 1. Do ...

  6. ACM学习历程—HDU1716 排列2(dfs && set容器)

    Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数.   Input 每组数据占一行,代表四张卡片上的数字( ...

  7. SVN版本控制详解

    1 版本控制 1.1 如果没有版本控制? Team开发必备. 一个人开发(必备). 版本控制:控制(代码)版本. 论文:版本控制? 毕业论文-4-22.doc 毕业论文-5-01.doc 毕业论文-f ...

  8. poj1734Sightseeing trip——无向图求最小环

    题目:http://poj.org/problem?id=1734 无向图求最小环,用floyd: 在每个k点更新f[i][j]之前,以k点作为直接连到i,j组成一个环的点,这样找一下最小环: 注意必 ...

  9. 关于UML图的生成

    想把一个java工程生成UML图非常简单,之前我的eclipse是4.2.0的,没有对应的GEF,所以我索性就直接把工程粘到了My Eclipse中,因为My Eclipse里面有UML自动生成的功能 ...

  10. 使用unlist将日期型数据的列表转换为向量时,出现的异常

    在使用unlist函数,将日期型的列表,转换为向量时,不会得到期望的结果,如下: > dateLst <- list(Sys.Date()) > dateLst [[1]] [1] ...