InvocationTargetException using JCI

I try to run a simple main to compile(!) some java code. This is my pom:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a</groupId>
<artifactId>a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jci-javac</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a</groupId>
<artifactId>a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jci-javac</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
And this is my main-class:
package a;

import java.nio.charset.StandardCharsets;

import org.apache.commons.jci.compilers.*;
import org.apache.commons.jci.readers.MemoryResourceReader;
import org.apache.commons.jci.stores.MemoryResourceStore;

public class Test {
public static void main(final String[] args) {
JavaCompilerFactory cf = new JavaCompilerFactory();
JavaCompiler compiler = cf.createCompiler("javac");
MemoryResourceReader pReader = new MemoryResourceReader();
int i = 0;
pReader.add("Test.java", "class Test{}".getBytes(StandardCharsets.UTF_8));
MemoryResourceStore pStore = new MemoryResourceStore();
CompilationResult result = compiler.compile(new String[] { pReader.list()[0] }, pReader, pStore,
Test.class.getClassLoader());
System.out.println(result.getErrors()[0]);
}
}
package a;

import java.nio.charset.StandardCharsets;

import org.apache.commons.jci.compilers.*;
import org.apache.commons.jci.readers.MemoryResourceReader;
import org.apache.commons.jci.stores.MemoryResourceStore;

public class Test {
public static void main(final String[] args) {
JavaCompilerFactory cf = new JavaCompilerFactory();
JavaCompiler compiler = cf.createCompiler("javac");
MemoryResourceReader pReader = new MemoryResourceReader();
int i = 0;
pReader.add("Test.java", "class Test{}".getBytes(StandardCharsets.UTF_8));
MemoryResourceStore pStore = new MemoryResourceStore();
CompilationResult result = compiler.compile(new String[] { pReader.list()[0] }, pReader, pStore,
Test.class.getClassLoader());
System.out.println(result.getErrors()[0]);
}
}
My output is: (0:0) : Error while executing the compiler: java.lang.reflect.InvocationTargetException What am I doing wrong?
37 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @Peter Rader! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st6mo ago
Can you please show the full stack trace?
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Peter Rader
Peter RaderOP5mo ago
No stacktrace. Sorry.
dan1st
dan1st5mo ago
Is the above the full error message? Can you add a DiagnosticListener?
Peter Rader
Peter RaderOP5mo ago
I dont know what a DiagnosticListener is. I could give you a github repo. Yes
dan1st
dan1st5mo ago
JavaCompiler (Java SE 21 & JDK 21)
declaration: module: java.compiler, package: javax.tools, interface: JavaCompiler
dan1st
dan1st5mo ago
Can you use setCompilationProblemHandler() and create a custom CompilationProblemHandler? to get more information about the error
Peter Rader
Peter RaderOP5mo ago
Sure I can. tbh, the JavaCompiler gives the same problem. I set
compiler.setCompilationProblemHandler(new CompilationProblemHandler() {

@Override
public boolean handle(final CompilationProblem pProblem) {
System.out.println("dddd" + pProblem);
return false;
}
});
compiler.setCompilationProblemHandler(new CompilationProblemHandler() {

@Override
public boolean handle(final CompilationProblem pProblem) {
System.out.println("dddd" + pProblem);
return false;
}
});
but the console does not print the dddd-line. I think the handler does not fire.
dan1st
dan1st5mo ago
? Did you try updating to version 1.1? or even 2.0 What is printed with System.out.println(result.getErrors()[0].getMessage());? or actually can you use a debugger and show the content of CompilationResult? or is that one returning null? Also it seems like that library is using Apache Commons Logging for quite some stuff - can you enable debug logging there? Also are you getting any other result if you use a different compiler? e.g. cf.createCompiler("eclipse") Looking at the sources, the support for the Eclipse compiler is more complete in that library
Peter Rader
Peter RaderOP5mo ago
(0:0) : Error while executing the compiler: java.lang.reflect.InvocationTargetException
dan1st
dan1st5mo ago
Is that the message?
Peter Rader
Peter RaderOP5mo ago
Yes
dan1st
dan1st5mo ago
.
Peter Rader
Peter RaderOP5mo ago
There is no version 1.1 There is no version 2
Peter Rader
Peter RaderOP5mo ago
No description
dan1st
dan1st5mo ago
https://repo1.maven.org/maven2/org/apache/commons/commons-jci-core/ just not of commons-jci-javac Though I don't think JSR199 is supported so jkust use the Eclipse compiler with 1.1
Peter Rader
Peter RaderOP5mo ago
I locked the transitive dependency 1.1 of -core using
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jci-core</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jci-core</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</dependencyManagement>
But the result remains the same.
dan1st
dan1st5mo ago
As I said, don't use commons-cli-javac Don't use javac there that doesn't really seem supported any more
Peter Rader
Peter RaderOP5mo ago
So they left a faulty JavaCompiler API in the jdk?
dan1st
dan1st5mo ago
? The one in the JDK is perfectly fine
Peter Rader
Peter RaderOP5mo ago
No
dan1st
dan1st5mo ago
but Apache Commons JCI doesn't have that any more
Peter Rader
Peter RaderOP5mo ago
Same problem!
dan1st
dan1st5mo ago
You haven't shown me the same thing with the JDK APIs and unless you show me, it doesn't exist as far as I'm concerned
Peter Rader
Peter RaderOP5mo ago
Yes, again! JavaCompiler API gives same problem.
dan1st
dan1st5mo ago
you still haven't shown me
Peter Rader
Peter RaderOP5mo ago
I will, give me a moment.
dan1st
dan1st5mo ago
Note: I don't doubt that exception would be there but it probably has a reason and should give you more details if you properly ask for it
Peter Rader
Peter RaderOP5mo ago
package a;

public class Test {
public static void main(final String[] args) {
com.sun.tools.javac.Main.compile(new String[] { "src/main/java/a/Test.java" });
}
}
package a;

public class Test {
public static void main(final String[] args) {
com.sun.tools.javac.Main.compile(new String[] { "src/main/java/a/Test.java" });
}
}
gives (0:0) : Error while executing the compiler: java.lang.reflect.InvocationTargetException. I mean, it makes totaly sense! commons-jci-javac is instrumenting javac! commons-jci-core is a wrapper that makes exactly this: com.sun.tools.javac.Main.compile(new String[] { ... });
dan1st
dan1st5mo ago
src/main/java? Seriously? That's com.sun.tools.javac not javax.compiler If it's in com.sun, it's internal meaning you should probably not use it
Peter Rader
Peter RaderOP5mo ago
It is a public-static-void-main(string[] args) Its javac imo
dan1st
dan1st5mo ago
Don't be so sure if you don't use it correctly
dan1st
dan1st5mo ago
JavaCompiler (Java SE 21 & JDK 21)
declaration: module: java.compiler, package: javax.tools, interface: JavaCompiler
dan1st
dan1st5mo ago
neither have I seen you using JCI version 1.1 with EJC
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server