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();
OBezzad
  • 145
  • 1
  • 9
  • Are you referring to [Java SE JavaCompiler](https://www.cs.uic.edu/~mcpc/Java_Docs/api/javax/tools/JavaCompiler.html)? Or Mavens JavaCompiler Mojo? Are you writing your a maven plugin or how do your expect to get parameters from maven? – homac Aug 25 '21 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? – khmarbaise Aug 26 '21 at 09: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. – OBezzad Sep 02 '21 at 16:13

0 Answers0