简体   繁体   English

aws lambda java:如何在动态编译时设置 class 路径

[英]aws lambda java : How to set the class path while compiling on the fly

I am building an AWS Lambda function which compiles a class on the fly.我正在构建一个 AWS Lambda function,它可以即时编译 class。 But the lambda function throws the error as it is not able to locate the dependencies ie the import statements for the class compiled on the fly.但是 lambda function 会抛出错误,因为它无法找到依赖关系,即即时编译的 class 的导入语句。 I've given sample code for the steps being followed.我已经为所遵循的步骤提供了示例代码。

Kindly guide on how I can locate the dependencies ie set class path so that the class created on the fly compiles without any errors请指导我如何找到依赖项,即设置 class 路径,以便动态创建的 class 编译时不会出现任何错误

  1. Sample Code being compiled正在编译的示例代码

    private String constructTestCode(String methodCode) throws Exception{ StringBuffer strMethod = new StringBuffer(); strMethod.append("import org.json.simple.JSONObject;"); strMethod.append("public class TestJavaFile {"); strMethod.append("public void testSample() {"); strMethod.append("JSONObject j = new JSONObject();"); strMethod.append("}"); strMethod.append("}"); return strMethod.toString(); }
  2. JavaCompilerCode Java编译器代码

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); List<String> optionList = new ArrayList<String>(); optionList.addAll(Arrays.asList("- classpath",System.getProperty("java.class.path"))); if (compiler == null) { try { Class<?> javacTool = Class.forName("com.sun.tools.javac.api.JavacTool"); Method create = javacTool.getMethod("create"); compiler = (JavaCompiler) create.invoke(null); compiler.getTask(null, null, null, optionList, null, null); } catch (Exception e) { throw new AssertionError(e); } } try { compiler.run(null, null, null, javaFile.toFile().getAbsolutePath()); } catch (Exception e) { context.getLogger().log("Exception " + e + "\n"); } return javaFile.getParent().resolve(fileName + ".class");
  3. Gradle one fat jar Gradle 一胖 jar

     task customFatJar(type: Jar) { manifest { attributes 'Class-Path': configurations.runtime.files.collect { it.name }.join(' ') } baseName = 'all-in-one-jar' from { configurations.compile.collect { it.isDirectory()? it: zipTree(it) } } with jar }
  4. Error while executing the lambda function执行 lambda function 时出错

    /tmp/TestJavaFile.java:1: error: package org.json.simple does not exist

File to be compiled is saved in the /tmp directory.要编译的文件保存在 /tmp 目录中。 I am unable to determine the path where the dependency jar files are present and how to set it in classpath.我无法确定存在依赖 jar 文件的路径以及如何在类路径中设置它。 Kindly guide on this.请对此进行指导。

Many Thanks!非常感谢!

This was resolved by setting the classpath as这是通过将类路径设置为解决的

    try {
        StandardJavaFileManager standardJavaFileManager = 
                   compiler.getStandardFileManager(null, null, null);
        standardJavaFileManager.setLocation(StandardLocation.CLASS_PATH, 
                   listFilePaths(folder, context));
        File[] javaFiles = new File[] { javaFile.toFile(), 
        Paths.get("/tmp/JUnitTest.java").toFile() };

        Iterable<? extends JavaFileObject> compilationUnits1 = 
                 standardJavaFileManager
                .getJavaFileObjectsFromFiles(Arrays.asList(javaFiles));
        CompilationTask task = compiler.getTask(null, standardJavaFileManager, null, 
                 optionList, null,
                 compilationUnits1);
        task.call();
       } catch (Exception e) {
           context.getLogger().log("Exception " + e + "\n");
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM