1

I am using JavaCompiler API to compile a string representation of an existing java class in a Maven project. Yet, I'm getting a linker error due to internal dependencies and I'm not sure how to include internal dependencies in the compilation task.

Here's the code snippet:

String importedClasses = "/Users/XXXX/Desktop/defects4j/project_repos/gson/gson/target/classes/com/google/gson/reflect/TypeToken.class";
List<String> dependencies = new ArrayList<>();
dependencies.add("com.google.gson.reflect.TypeToken");

JavaFileObject file = new JavaSourceFromString(this.className, writer.toString());
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
JavaCompiler.CompilationTask task = compiler.getTask(null, null, diagnostics, Arrays.asList("-classpath", "/Users/XXXX/Desktop/defects4j/project_repos/gson/gson/target/classes/", "-d", outputDir, "-cp", importedClasses), dependencies, compilationUnits);
boolean success = task.call();
3
  • Are you referring to Java SE JavaCompiler? Or Mavens JavaCompiler Mojo? Are you writing your a maven plugin or how do your expect to get parameters from maven? Commented Aug 25, 2021 at 23:03
  • Can you explain in detail what kind of problem you are trying to solve? Why not let Maven Compiler PLugin compile your code? Commented Aug 26, 2021 at 9:51
  • I want to make a Maven project that, programmatically, compiles the test classes of another Maven project. I wasn't aware of CompilerMojo, and even after discovering it, I'm unable to compile another project from its path. Commented Sep 2, 2021 at 16:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.