【发布时间】:2019-11-04 10:38:28
【问题描述】:
我正在构建一个动态编译类的 AWS Lambda 函数。但是 lambda 函数会抛出错误,因为它无法找到依赖项,即动态编译的类的导入语句。我已经为所遵循的步骤提供了示例代码。
请指导我如何找到依赖项,即设置类路径,以便动态创建的类编译时不会出现任何错误
-
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(); } -
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"); -
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 } -
Error while executing the lambda function
/tmp/TestJavaFile.java:1: error: package org.json.simple does not exist
要编译的文件保存在 /tmp 目录中。我无法确定存在依赖 jar 文件的路径以及如何在类路径中设置它。请对此进行指导。
非常感谢!
【问题讨论】:
-
请在问题中添加代码,而不是图片
-
现在添加代码。谢谢!
标签: java amazon-web-services aws-lambda classpath