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
7
votes
1 answer
Can I add a method to a class from a compile time annotation?
If I create a custom annotation (example: @SaveFuncName("saveMe") will add a method called saveMe() with some code my processor generates), can the javac compiler use my annotation processor to add a method to the class? Or can I only create a…

Don Rhummy
- 24,730
- 42
- 175
- 330
7
votes
4 answers
compiling and running user code with JavaCompiler and ClassLoader
I am writing web app for java learning. Using which users may compile their code on my serwer + run that code.
Compiling is easy with JavaCompiler:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
…

tzim
- 1,715
- 15
- 37
7
votes
1 answer
Does javax.tools depend on the JDK?
I want to use JavaCompiler to dynamically create some classes.
I found the source code of the javax.tools package, but there is no implementation; some posts on the internet say it depends on tools.jar, I am not sure tools.jar associates with…

Brodie
- 605
- 1
- 9
- 17
7
votes
1 answer
How can I use JDT compiler programmatically?
I use JDT to compile my java classes. BatchCompiler returns a string but I need an array of problems/errors with their column and row information. compiler.compile(units); prints the error to its printwriter, compiler.resolve(unit) does exactly what…

burak emre
- 1,501
- 4
- 22
- 46
6
votes
1 answer
Using Java Compiler API to compile multiple java files
Hi I have requirement to create ,compile and load java classes run time. Using FTL i am creating java source files , and able to compile the source if there is no dynamic dependency.
To elaborate with an instance, I have two java source file, one…

Satheesh Cheveri
- 3,621
- 3
- 27
- 46
6
votes
4 answers
Java Dynamic Code Generation with support for generics
Is there any tool which provides Java dynamic code generation and that also supports generics?
Javassist for example, is the kind of tool that I need, but it does not support generics.
I wrote a small lib which uses the Java 6 Compiler API, however…

halfwarp
- 1,780
- 5
- 24
- 41
6
votes
3 answers
How do I use JDK6 ToolProvider and JavaCompiler with the context classloader?
My usage case is compiling generated source files from a java program using the ToolProvider and JavaCompiler classes provided in JDK 6. The source files contain references to classes in the context classloader (it runs in a J2EE container), but…

Phil
- 909
- 1
- 10
- 17
5
votes
2 answers
Programmatically compile java with JavaCompiler?
I got this Java code from another Stack Overflow thread
import java.io.*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Main {
public static void main(String[] args) throws IOException{
String source = "…

user969931
- 96
- 5
5
votes
2 answers
Developing a java program to compile .java files
I am researching for a project that will involve my project to compile simple enough java files. From my research I have decided not to try and build my own compiler as this would just take too much time.
So does anyone know what would be the best…

Hip Hip Array
- 4,665
- 11
- 49
- 80
4
votes
1 answer
Is there a way to generate a unique variable name in a given scope?
Given a Scope, is there a function that can generate a unique variable name such that a variable declaration for the unique name could be inserted in scope and the resulting source code/CompilationUnitTree would still compile?

Daniel Trebbien
- 38,421
- 18
- 121
- 193
4
votes
1 answer
How to dynamically compile Java classes in Google App Engine
In answers to this question, I learned that it is not possible to invoke the Java Compiler in javax.tools from a GAE app.
Does this limitation still apply?
If so, what are my options for compiling Java source code into loadable class files "on the…

Tony the Pony
- 40,327
- 71
- 187
- 281
4
votes
2 answers
Compile Circular Dependency classes using javax.tools.JavaCompiler
I have been trying to compile some Java Classes in a String using java.
I have used javax.tools.JavaCompiler to compile the Classes in the Strings.
I have made instances of SimpleJavaFileObject by a Subclass that I have made of…

Bhanuka Yd
- 646
- 8
- 25
4
votes
1 answer
Java Compilation task and classpath option not working
I was trying to create a class to compile some .java that I'll create at runtime, but I can't get this to work properly.
Here's my code
File bin = new File(args[args.length-1]);
System.setProperty("java.home", "C:\\Program…

Rafael Palliarini
- 142
- 1
- 11
4
votes
2 answers
how to run code compiled by JavaCompiler?
Is there any way to run program compiled by JavaCompiler? [javax.tools.JavaCompiler]
My code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector diagnostics = new…

tzim
- 1,715
- 15
- 37
4
votes
2 answers
Compiling Java code from string results in ClassNotFoundException
I'm trying a code example that uses javax.tools to compile code that is in a string. The Class.forName results in a ClassNotFoundException. Does anybody know why? I'm using Java 7.
import java.io.IOException;
import…

Jochen
- 754
- 7
- 21