A Java API for the Java compiler, available as javax.tools.JavaCompiler and related classes. (Use [javac] for questions about the command line compiler.)
Questions tagged [java-compiler-api]
181 questions
2
votes
1 answer
Compile another Maven project using Maven Compiler API (CompilerMojo)
I'm working on a Maven project that injects and runs some tests in another Maven projects. I already managed to inject them, however, I'm struggling to programmatically compile the other Maven project given its path.
I tried Java Compiler API and…

OBezzad
- 145
- 1
- 9
2
votes
0 answers
How to use javax.tools.JavaCompiler to create a function that calls a list of methods of child class filtered by custom annotation?
I have this unusual (I think?) situation
public abstract class Base {
public Base() {
// code
}
public final void executeAll() {
final Foo foo = new Foo();
final Bar bar = new Bar();
// now execute all method of…

Vento
- 63
- 7
2
votes
1 answer
Compile Java class in runtime with dependencies to nested jar
In a Spring Boot app I'm doing the following at runtime:
Generating a Java class
Compiling it
Accessing some static fields of the compiled class using reflection.
I've based my code on this post and got a problem compiling my generated class in…

forhas
- 11,551
- 21
- 77
- 111
2
votes
1 answer
How can I change classloader of getSystemJavaCompiler
I am dynamically compiling Java sources using the Java compiler API. My generated source files inherit from com.example.BaseClass, which is just a normal class, not dynamically generated. The generated Java sources look like this:
public class Foo…

eddy
- 63
- 6
2
votes
2 answers
How do I find the type declaration of an identifier using the Java Tree Compiler API?
I have the name of a variable/identifier, say, x, and the JCCompilationUnit and Scope. Is there a way to find the type of x?

simpatico
- 10,709
- 20
- 81
- 126
2
votes
2 answers
run() method of interface tool throws NullPointerException in JDK 9
//Following is Hello.java file.
public class Hello{
public static void main(String... s){
System.out.println("hello world");
}
}
I'm trying to compile the above class using Java Compiler API as following:
import…

Abhishek Mishra
- 128
- 1
- 2
- 11
2
votes
0 answers
Can't use javax.tools.JavaCompiler when lombok is a dependency - getting NoClassDefFoundError
I'm trying to use JavaCompiler to compile a Java source at runtime:
$ cat src/main/java/App.java
import java.util.LinkedList;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import…

Idan Arye
- 12,402
- 5
- 49
- 68
2
votes
0 answers
JavaCompiler interface usage and its default implementation
I'm trying to understand how default JavaCompiler implementation, the one you can get by invoking ToolProvider.getSystemJavaCompiler().
How do you use it with a JavaFileManager?
I understand that the compiler uses the file mananger it's given…

nadavgam
- 2,014
- 5
- 20
- 48
2
votes
4 answers
Java Compiler API with classes that depend on each other
I'm using Java Compiler API to compile in-memory classes. That is, classes are compiled to bytecode (no .classes files stored in disk) and then loaded by reconstructing the bytecode.
Sometimes, I need to compile a class that depends on another, also…

halfwarp
- 1,780
- 5
- 24
- 41
2
votes
1 answer
How to use Java Compiler API to compile java files recursively?
I am learing Java Compiler API recently and I write some code that can compile one simple java source code file into a class file, but it can't compile a source code file which refer another class in other source code file. Below is my code:
public…

kk17
- 601
- 6
- 14
2
votes
1 answer
Why JavaCompiler is slow while instantiating a Java class?
I'm using JavaCompiler to dyamically create a Java class, compile it and load in my application.
My problem is the following: the execution time with JavaCompiler is much slower than the standard way to instantiate the same class.
Here an…
user7486618
2
votes
2 answers
How to get the JavaCompiler to use a provided classLoader to find a class?
I am using this code from an old IBM blog post about how to compile and use Java classes at runtime. The code mostly works great (and is quite well written, by the way), but unfortunately for me, it won't work in one of my use cases where the class…

Renato
- 12,940
- 3
- 54
- 85
2
votes
0 answers
generate jar without dependencies
I am generating a jar inside a java program by using the jdk system-compiler.
I have some class files which I add into a jar:
JarOutputStream target = new JarOutputStream(jarStream, manifest);
addClassFilesToJar(new File(classDirectory), target);
I…

Gobliins
- 3,848
- 16
- 67
- 122
2
votes
2 answers
JavaCompiler didn't really compile the class
I want to use reflection to get all the methods of a newly created java class. Like bellow I created the java Class by copying from another file, then I use JavaCompiler to compile the newly created Java. But I don't know why the target class file…

leo
- 583
- 2
- 7
- 20
2
votes
1 answer
Classpath of classes compiled with Javassist
As the title suggests, what is the classpath of classes compiled with Javassist?
My scenario is: Class A is compiled with Javassist. Class B is compiled with Java Compiler API and references Class A. The problem is that Class A is not visible to…

halfwarp
- 1,780
- 5
- 24
- 41