动态编译java类Web项目

时间:2016-09-30 17:01:24

标签: java java-ee classpath wildfly-9

我正在开发一个Web应用程序,我可以在其中导入java代码,编译然后执行它。 这个类可能有一些来自其他库的导入,我将其添加到我的类路径中。 将它作为桌面应用程序运行,它可以工作,但是在wildfly 9.0.2下运行,它找不到我的类路径库,所以我编译代码时出错。

我是否必须更改wildfly配置的任何设置?我和maven一起尝试了我的代码。

我使用这个lib来编译我的代码,就像我说的那样,它使用像桌面这样的应用程序:https://github.com/trung/InMemoryJavaCompiler

错误:

13:44:57,686 ERROR [stderr] (default task-5) /br/com/project/webtest/service/CompileClass.java:2: error: package org.junit does not exist
13:44:57,686 ERROR [stderr] (default task-5) import static org.junit.Assert.*;
13:44:57,686 ERROR [stderr] (default task-5)                        ^
13:44:57,687 ERROR [stderr] (default task-5) /br/com/project/webtest/service/CompileClass.java:3: error: cannot find symbol
13:44:57,687 ERROR [stderr] (default task-5) import br.com.project.webtest.service.SeleniumService;
13:44:57,688 ERROR [stderr] (default task-5)                                               ^
13:44:57,688 ERROR [stderr] (default task-5)   symbol:   class SeleniumService
13:44:57,688 ERROR [stderr] (default task-5)   location: package br.com.project.webtest.service
13:44:57,689 ERROR [stderr] (default task-5) /br/com/project/webtest/service/CompileClass.java:4: error: package org.junit does not exist
13:44:57,689 ERROR [stderr] (default task-5) import org.junit.*;

错误消息将继续与所有其他导入库一起使用 然后,classformaterror:

13:44:57,751 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-5) #{testController.action()}: java.lang.ClassFormatError: Truncated class file: javax.faces.FacesException: #{testController.action()}: java.lang.ClassFormatError: Truncated class file

编辑: 我添加了使用eclipse mars,在Webcontent / web-inf / lib文件夹下,添加了我的库并右键单击,添加到构建路径。

编辑2: 负责创建代码并获得结果的类:

package br.com.test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class TestController {

    public void actionParent() {
        String javaCode = generateJavaCode();
        Class<?> compile = null;
        try {
            compile = InMemoryCompilerTest.compile(Thread.currentThread().getContextClassLoader(),
                    "br.com.project.webtest.service.CompileClass", javaCode);
            System.out.println("Worked: " + compile);
        } catch (Exception e) {
            e.printStackTrace();
        }           
    }

    public void actionClassLoader() {
        String javaCode = generateJavaCode();
        Class<?> compile = null;
        try {
            compile = InMemoryCompilerTest.compile("br.com.project.webtest.service.CompileClass", javaCode);
            System.out.println("Worked: " + compile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static String readFile(String path, Charset encoding) throws IOException {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return new String(encoded, encoding);
    }

    private static String generateJavaCode() {
        String java = "package br.com.project.webtest.service;\r\n" 
                    + "import static org.junit.Assert.*;\r\n"
                    + "public class CompileClass {\r\n" 
                    + " public CompileClass() {\r\n"
                    + "     System.out.println(\"Dynamically compiled\");\r\n"
                    + "     String text = \"Testing JUnit lib\";\r\n"
                    + "     assertEquals(\"Testing JUnit lib\", text);\r\n" 
                    + "     System.out.println(\"completed\");\r\n"
                    + " }\r\n" + "  public static void main(String[] args) {\r\n" 
                    + "     new CompileClass();\r\n"
                    + "     System.out.println(\"finish\");\r\n" 
                    + " }\r\n" 
                    + "}\r\n";

        return java;

    }

    public static void main(String[] args)  {
        TestController c = new TestController();
        c.actionClassLoader();
        c.actionParent();
    }

}

我创建了一个扩展lib并改变了使用父类加载器的类:

public class InMemoryCompilerTest extends InMemoryJavaCompiler {
    static JavaCompiler javac = ToolProvider.getSystemJavaCompiler();

    public static Class<?> compile(ClassLoader parent, String className, String sourceCodeInText) throws Exception {
        SourceCode sourceCode = new SourceCode(className, sourceCodeInText);
        CompiledCode compiledCode = new CompiledCode(className);
        Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(sourceCode);
        DynamicClassLoader cl = new DynamicClassLoader(parent);
        ExtendedStandardJavaFileManager fileManager = new ExtendedStandardTest(
                javac.getStandardFileManager(null, null, null), compiledCode, cl);
        JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, null, null, null, compilationUnits);
        boolean result = task.call();
        return cl.loadClass(className);
    }
}

public class ExtendedStandardTest extends ExtendedStandardJavaFileManager{

    protected ExtendedStandardTest(JavaFileManager fileManager, CompiledCode compiledCode, DynamicClassLoader cl) {
        super(fileManager, compiledCode, cl);
    }

}

XHTML:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">

<h:head></h:head>
<h:body>

    <h:form>

        <h:commandButton action="#{testController.actionClassLoader()}" value="ClassLoader" />
        <h:commandButton action="#{testController.actionParent()}" value="Parent" />
    </h:form>
</h:body>
</html>

我在测试时测试了传递父类加载器,但也没有工作。

第一堂课&#34; TestController&#34;从主要方法执行它。

编辑:

我添加了以下代码,我可以使用libs设置类路径:

// set the classpath
        List<String> options = new ArrayList<String>();

        options.add("-classpath");
        StringBuilder sb = new StringBuilder();
        URLClassLoader urlClassLoader = (URLClassLoader) parent;

        for (URL url : urlClassLoader.getURLs()) {
            sb.append(url.getFile()).append(File.pathSeparator);
        }
        options.add(sb.toString());

作为Java应用程序执行它返回:

[-classpath, /C:/Users/dev/Automacao/workspace/test/build/classes/;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/InMemoryJavaCompiler-1.2.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/commons-io-2.5.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/client-combined-3.0.0-beta3-nodeps.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/commons-codec-1.10.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/commons-exec-1.3.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/commons-logging-1.2.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/gson-2.3.1.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/guava-19.0.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/hamcrest-core-1.3.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/hamcrest-library-1.3.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/httpclient-4.5.2.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/httpcore-4.4.4.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/httpmime-4.5.2.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/jna-4.1.0.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/jna-platform-4.1.0.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/junit-4.12.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/netty-3.5.7.Final.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/phantomjsdriver-1.3.0.jar;/C:/Users/dev/Automacao/workspace/test/WebContent/WEB-INF/lib/workspace_libs/cglib-nodep-3.2.4.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/annotations-api.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/catalina-ant.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/catalina-ha.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/catalina-storeconfig.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/catalina-tribes.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/catalina.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/ecj-4.3.1.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/el-api.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/jasper-el.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/jasper.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/jsp-api.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/servlet-api.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-api.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-coyote.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-dbcp.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-i18n-es.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-i18n-fr.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-i18n-ja.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-jdbc.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-jni.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-spdy.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-util-scan.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-util.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/tomcat-websocket.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Apache%20Tomcat%208.0.3/lib/websocket-api.jar;/C:/Users/dev/Automacao/workspace/libraries/JSF%202.2%20(Mojarra%202.2.0)/mojarra-2.2.0-FCS/lib/javax.faces.jar;]

执行为webapplication:

[-classpath, /C:/Users/dev/Automacao/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/test/WEB-INF/classes/;/C:/Users/dev/Automacao/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/test/WEB-INF/lib/commons-io-2.5.jar;/C:/Users/dev/Automacao/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/test/WEB-INF/lib/InMemoryJavaCompiler-1.2.jar;/C:/Users/dev/Automacao/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/test/WEB-INF/lib/javax.faces.jar;]

我怎么能得到相同的结果?

3 个答案:

答案 0 :(得分:1)

让它发挥作用。

我必须进行@NicolasFilotto提到的更改,以便在使用webapp时使用父类加载器。 之后,对于JavaCompiler工作,因为我正在使用外部库,所以必须传递我上次编辑时提到的类路径。 基本上代码改为:

public static Class<?> compile(ClassLoader parent, String className, String sourceCodeInText) throws Exception {
        SourceCode sourceCode = new SourceCode(className, sourceCodeInText);
        CompiledCode compiledCode = new CompiledCode(className);
        Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(sourceCode);
        DynamicClassLoader cl = new DynamicClassLoader(parent);
        ExtendedStandardJavaFileManager fileManager = new ExtendedStandard(
                javac.getStandardFileManager(null, null, null), compiledCode, cl);

        // set the classpath
        List<String> options = new ArrayList<String>();

        options.add("-classpath");
        StringBuilder sb = new StringBuilder();
        Enumeration<URL> resources = parent.getResources("/");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            sb.append(url.getFile()).append(File.pathSeparator);
        }

        options.add(sb.toString());

        // execute the compiler
        Boolean call = javac.getTask(null, fileManager, null, options, null, compilationUnits).call();
        if (call) {
            return cl.loadClass(className);
        }
        return null;
    }

使用wildfly我必须将URLClassLoader urlClassLoader = (URLClassLoader) parent;更改为Enumeration<URL> resources = parent.getResources("/");

答案 1 :(得分:0)

@VictorBello我已经在Dynamic Loader中实现了这些功能。即使您可以在Dynamic Loader

中的运行时包含Jar
SparkConf conf = new SparkConf().setAppName("Aggregation").setMaster("local");
JavaSparkContext sc = new JavaSparkContext(conf);
SQLContext sqlContext = new SQLContext(sc);
JavaPairRDD<String, Map<String, Object>> pairRDD = JavaEsSpark.esRDD(sc, "kpi_aggregator/record");
RDD rdd = JavaPairRDD.toRDD(pairRDD);
Dataset df = sqlContext.createDataFrame(rdd, customSchema);
df.registerTempTable("data");
Dataset kpi1 = sqlContext.sql("SELECT host, SUM(bytes_uplink), SUM(bytes_downlink) FROM data GROUP BY host");
JavaEsSparkSQL.saveToEs(kpi1, "kpi_aggregator_total/record");

答案 2 :(得分:0)

感谢@ victor-bello提供了出色的解决方案。 我的解决方案如下所示:

  1. 需要将所有jar从maven存储库复制到一个文件夹中,然后使用您的应用程序分发该文件夹: lib folder
  2. 从它们生成javac的类路径(愚蠢的方式): classpath
  3. 从github复制InMemoryJavaCompiler代码源并覆盖:

    公共类InMemoryCompilerTest扩展了InMemoryJavaCompiler {

    static JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    
    public static Class<?> compile(ClassLoader parent, String className, String sourceCodeInText, String classpath ) throws Exception {
    
        SourceCode sourceCode = new SourceCode(className, sourceCodeInText);
        CompiledCode compiledCode = new CompiledCode(className);
        Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(sourceCode);
        DynamicClassLoader cl = new DynamicClassLoader(parent);
        ExtendedStandardJavaFileManager fileManager =
                new ExtendedStandard(javac.getStandardFileManager(null, null, null), compiledCode, cl);
    
        // set the classpath
        List<String> options = new ArrayList<String>();
    
        options.add("-classpath");
        options.add(classpath);
    
        // execute the compiler
        Boolean call = javac.getTask(null, fileManager, null, options, null, compilationUnits).call();
        if (call) {
            return cl.loadClass(className);
        }
        return null;
    }
    

    }

  4. 调用编译方法:

    class testClass = inMemoryCompilerTest.compile(Thread.currentThread()。getContextClassLoader(),“ com.namespace.here.ClassName”,someCode,stringBuilder.toString());