#InvocationTargetException using JCI

1 messages · Page 1 of 1 (latest)

indigo karma
#

I try to run a simple main to compile(!) some java code. This is my pom:

    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]);
    }
}

My output is: (0:0) : Error while executing the compiler: java.lang.reflect.InvocationTargetException
What am I doing wrong?

glad acornBOT
#

This post has been reserved for your question.

Hey @indigo karma! 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.

late cargo
#

Can you please show the full stack trace?

glad acornBOT
#

💤 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.

indigo karma
#

No stacktrace. Sorry.

late cargo
#

Is the above the full error message?

#

Can you add a DiagnosticListener?

indigo karma
#

I dont know what a DiagnosticListener is. I could give you a github repo.

indigo karma
late cargo
#

Can you use setCompilationProblemHandler() and create a custom CompilationProblemHandler?

#

to get more information about the error

indigo karma
#

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;
        }
    });``` but the console does not print the dddd-line. I think the handler does not fire.
late cargo
#

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?

late cargo
#

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

indigo karma
indigo karma
indigo karma
indigo karma
late cargo
#

just not of commons-jci-javac

#

Though I don't think JSR199 is supported

#

so jkust use the Eclipse compiler with 1.1

indigo karma
late cargo
#

Don't use javac there

#

that doesn't really seem supported any more

indigo karma
#

So they left a faulty JavaCompiler API in the jdk?

late cargo
#

The one in the JDK is perfectly fine

indigo karma
#

No

late cargo
#

but Apache Commons JCI doesn't have that any more

indigo karma
#

Same problem!

late cargo
#

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

indigo karma
late cargo
indigo karma
#

I will, give me a moment.

late cargo
#

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

indigo karma
#

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[] { ... });

late cargo
#

not javax.compiler

#

If it's in com.sun, it's internal meaning you should probably not use it

indigo karma
#

It is a public-static-void-main(string[] args)

#

Its javac imo

late cargo
#

Don't be so sure if you don't use it correctly

#

neither have I seen you using JCI version 1.1 with EJC