209 questions
0
votes
1
answer
471
views
Cannot Export/Open Package from Module jdk.compiler to Run/Pass Tests in Maven
Settings
I'm using JDK 22:
openjdk version "22" 2024-03-19
OpenJDK Runtime Environment (build 22+36-2370)
OpenJDK 64-Bit Server VM (build 22+36-2370, mixed mode, sharing)
I'm also using ...
0
votes
1
answer
138
views
How to set the location that JavaCompiler executes from?
I am working with javax.tools.JavaCompiler, using it to programmatically compile Java code from my running Java application. Everything is working great, except that I don't how to execute the ...
0
votes
0
answers
178
views
Use transitive maven dependency in an annotation processor inside the maven-compiler-plugin using the ServiceLoader
Top of the morning/noon/evening!
I have a multi project setup in which I use an annotation processor. This annotation processor is supposed to pickup Plugin instances (a custom interface) during the ...
1
vote
0
answers
608
views
Caused by: java.lang.SecurityException: Can not initialize cryptographic mechanism at javax.crypto.JceSecurity in JavaCompiler API
I am trying to connect to SSH (Unix environment) using the JSch library in JavaCompiler API. I can connect to the unix environment through a normal Java program but cannot from JavaCompiler API.
Looks ...
0
votes
0
answers
96
views
How to modify java bytecode after compilation using compiler plugins?
I want to modify some java classes bytecode during/after compilation phase, but before packaging, in order to use custom invokedynamic bootstrap methods.
In order to do so, I have written a Java ...
0
votes
0
answers
82
views
Scan a super method definition from TreeScanner.visitMethodInvocation
Given the following class hierarchy
public AClass {
String getSomething() { ... }
}
public BClass extends AClass {
@Override
@CustomAnnotation
String getSomething() { super.getSomething(); ...
0
votes
1
answer
25
views
Issues with jar exporter tool
I am attempting to write a Java project, which will compile any Java project and export a *.jar file. The program expects 3 runtime arguments, which I am specifying as:
C:\dev\Kronos\Kronos
vendor
src\...
0
votes
2
answers
192
views
How to copy a JarEntry to a directory
I have scanned the maven directory (.m2) and have filtered all the files ending with .jar.
List<File> jarFiles = scanRecursivelyForJarObjects(mavenRepository, fileManager);
I am trying the copy ...
0
votes
0
answers
647
views
Enable preview features for compiler plugin
I'm developing a compiler plugin for Javac. My codebase uses preview features, so I need to run using the flag --enable-preview. I can compile my code perfectly using maven, but when I try to do the ...
2
votes
1
answer
107
views
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 ...
-1
votes
1
answer
139
views
Project packages cannot import Java compiler API: ClassNotFoundException / NoClassDefFoundError
I use Java compiler class inside my Spring Boot application as follows:
String classStr = "\n" +
"import java.util.List;\n" +
"import java.util.Map;\n" +
...
1
vote
0
answers
155
views
How to include internal dependencies in a Maven project using JavaCompiler API?
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 ...
0
votes
2
answers
503
views
How to get parent tree object from JCTree in java?
How to get parent tree node from JCTree (com.sun.tools.javac.tree.JCTree) in Java?
For example:
public class MyTreeTranslator extends TreeTranslator {
@Override
public void visitMethodDef(...
1
vote
1
answer
94
views
Facing NoClassDefFoundError despite jar being in classpath (dynamic compilation)
In a tomcat server, at run time, I am reading java class files and compiling them dynamically using the InMemoryJavaCompiler library which internally uses JavaCompiler .
Code Sample -
...
0
votes
0
answers
215
views
JavaCompiler API in a Spring Boot app and get classpath
I'm using JavaCompiler API in Spring boot application. But currently I'm facing with an issue on getting spring boot runnable jar file classpath using System.getProperty("java.class.path") ...
3
votes
1
answer
1k
views
How to validate Java code programatically?
Given the source code and the Java version, I need to be able to validate whether or not the code will compile. If the code does not compile, I need to be able to return the errors within the source ...
1
vote
0
answers
117
views
How do you create a JavaCompiler instance for a JDK not on your path?
How do you create a JavaCompiler instance not on your system? More specifically, I would like to be able to create a JavaCompiler instance from a version number.
The only techniques I have seen for ...
1
vote
1
answer
189
views
Java 8 Compiler Plugin : How to add super() call in a constructor?
I'm writing a Java compiler plugin to add a simple class named MyClass in some classes of my project (something like lombok does). I've managed to do it by writing the code bellow (you can find the ...
3
votes
2
answers
1k
views
Cross Compilation in Java 13 and Java 8
I want to load a Java version specific JavaCompiler.
This is how I take compiler instance currently:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
My aim is to compile a "user Java ...
2
votes
0
answers
137
views
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 ...
2
votes
0
answers
320
views
Can ToolProvider.getSystemJavaCompiler() access runtime generated in memory source files when using JPMS modules?
I have developed an application that generates Java source at runtime from an external source. The generated source code implements an interface residing in say org.foo and also calls other classes ...
0
votes
1
answer
465
views
Check inheritance of Element in source level AnnotationProcessors
I'm looking for a way to detect if a class is inherited from another class/interface in annotation processor. Since the annotation processor runs on the source code not runtime, there's no way to use ...
0
votes
1
answer
122
views
JavaCompiler API: access functions/variables outside the compiled program while it's running?
Adapting this the following code takes a class and function name, a string of Java code, compiles the code and runs the function.
public class Compile {
static void compileAndRun(final String ...
0
votes
0
answers
196
views
How to active manually JIT compiler on Java?
I'm making a several performance tests of different languages. When I started test Java applications I got a problem: JIT Compiler. I noted that after several executions of a method, this method ...
1
vote
1
answer
46
views
Non-essential imports compiled by javac
Just verified that javac compiles every reference or import that it sees, such as JavaDoc @link references.
Can this be turned off with an argument?
And/or, can a less eager compile be implemented ...
0
votes
0
answers
195
views
Java compiler api with springboot | Issue running under tomcat
I'm creating a file dynamically on execution of an api,
Code for it looks like:
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, null, null);
fileManager....
1
vote
0
answers
116
views
"Symbol not found" problem while trying to generat repositories at runtime
I'm trying to generate some repositories at runtime for spring data jpa with OpenHFT to reduce boilerplate code. The problem is, when running with IDEA it goes just fine, however if i run it in ...
0
votes
1
answer
83
views
Unable to compile annotations like @Entity using JavaCompiler
I am using JavaCompiler to compile Java code at runtime. For most part, it works well. There are no exceptions during compiling, it works well. I require it to compile a class with @Entity annotation. ...
-2
votes
1
answer
204
views
Interpreter and Compiler
Can any one help me in figuring out what compilers and interpreters are? And what their difference is ? Appreciate it if explained for Java beginner as I am one.
1
vote
1
answer
281
views
Java Compiler API (Linux): cannot find symbol for custom classes
I have a rather specific problem with the Java Compiler API.
For my Usecase I have to generate, compile and load a java-class at runtime in my Web-Application (using Tomcat).
In order to do that, I ...
2
votes
1
answer
2k
views
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 ...
1
vote
1
answer
315
views
how to use remote jars in classpath for javax.tools.JavaCompiler
I'm using JavaCompiler to compile .java code.
It works for me while running the class compiled using URLClassLoader to load remote jars.
But not work while compiling.
Tried to use options like
...
1
vote
0
answers
784
views
Is it possible to replace an implementation of a method of a dependency library in Java?
We have a situation where we are using some library and we would like to customize the logic of a tiny private method used in the library. Unfortunately, the library isn't well written to be extended.
...
-1
votes
1
answer
49
views
Traverse JavaFileObjects enters into infinite loop
I'm trying to use java compiler api. First I've got "mytest.java" which runs well. Then I have this code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
...
0
votes
1
answer
152
views
Run JDT ASTVisitor on the output of Java Compiler
I am trying to write an extended Java Compiler that gives all the errors and warnings from javac plus some errors for custom unsupported java features.
Since this is going to run on a server, i would ...
16
votes
3
answers
19k
views
String too large to encode using UTF-8 written instead as 'STRING_TOO_LARGE'
Java compiler error: string too large to encode using UTF-8 written instead as 'STRING_TOO_LARGE'.
I want to note, that there I have no long string and no one vector drawable that bigger than 32kb (...
0
votes
0
answers
1k
views
Annotation processor not working with Compiler API - Java
I have a task to compile java source file from source code. So I used JavaCompiler API. My java class annotated with Lombok annotation @Data. But when I compiled class exceptions are thrown.
Name....
2
votes
1
answer
571
views
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 ...
62
votes
25
answers
109k
views
android studio with Java compiler error: string too large to encode using UTF-8 written instead as 'STRING_TOO_LARGE'
When I clean the android project in android studio, the error happens.
I have backed to previous commit or different branch, which worked fine couple days ago, but has this error now.
I have checked ...
11
votes
2
answers
8k
views
How to get a list of jar files and folders in classpath in Java 9? [duplicate]
I know you can't change the classpath in Java 9 because I read this: Add jar to classpath at runtime under java 9
I just want to list jar files and folders currently on the Classpath so that I can ...
0
votes
1
answer
464
views
How to compile and run scala code at runtime from a java program?
I already have a program that uses JavaCompiler api for compiling Java code at runtime. Can I use the same compiler for scala code? If not, what is the best way to compile scala code in a Java program?...
0
votes
1
answer
684
views
Java Compiler API: import of custom package not working
I am working on a web project where I create a custom .java-File, load it with a custom ClassLoader, compile it using the Compiler API and then execute it via Reflection. The only thing that doesn't ...
2
votes
2
answers
311
views
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 ...
1
vote
0
answers
89
views
Java code generation at runtime: prevent creation of file "classlist.mf" in the working directory by javax.tools.JavaCompiler
When I run java code similar to this:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
...
CompilationTask task = compiler.getTask(null, javaFileManager, diagnosticCollector, null, null, ...
1
vote
2
answers
196
views
How do I check if the generated Java code adheres to the Java language specification?
I am working on a school project where I need to generate Java code that adheres to the Java Language Specs. So far I have been able to generate the Java code randomly which has valid syntax that ...
8
votes
1
answer
931
views
Using JavaCompiler API in a Spring Boot app - unable to properly set the compilation classpath
We have an app that's been migrated from a traditional WAR Spring web application to what we had hoped would be a modern Spring Boot executable Jar.
One of the app modules uses the JavaCompiler API ...
2
votes
0
answers
268
views
Can't use javax.tools.JavaCompiler when lombok is a dependency - getting NoClassDefFoundError [duplicate]
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;
...
3
votes
0
answers
222
views
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 ...
2
votes
1
answer
2k
views
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 ...
0
votes
0
answers
54
views
How to use Java Compile API to compile recursively? [duplicate]
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 ...