Maven exec plugin running with Java 1.8 instead of Java 11 from IntelliJ

Answered

Hi,

I'm generally using Maven directly from IntelliJ, and I came accross this problem:

For some reasons I won't detail here, I need to execute the main method of a Java class during maven "generate-sources" phase.

I have a pom.xml looking like this:

<dependencies>
...
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.24</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-XshowSettings</argument>
<argument>-classpath</argument>
<classpath>
<dependency>org.yaml:snakeyaml</dependency>
</classpath>
<argument>${basedir}/src/test/java/org/foo/Foo.java</argument>
<argument>arg1</argument>
<argument>arg2</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
...
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

It works perfectly fine when I run the maven command from the command line, for instance:

mvn clean compile

However, when using Maven from IntelliJ directly, I got the following error:

Error : Cannot find or load Main class .home.user.project.basedir.src.test.java.org.foo.Foo.java
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:982)
at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:929)
at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:457)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
at org.codehaus.classworlds.Launcher.main (Launcher.java:47)

I ran mvn exec plugin with "-XshowSettings" argument to compare the differences between both runs. I found this;

- in IntelliJ, the java version used is 1.8, but I clearly specified to use JAVA_HOME everywhere in "Settings -> Build, Execution, Deployment -> Build Tools -> Maven". My JAVA_HOME links to Java 11, and it is correctly shown in CLI run's settings.

- The setting "sun.java.command" when ran from the CLI is equal to

sun.java.command = jdk.compiler/com.sun.tools.javac.launcher.Main /my/project/basedir/src/test/java/org/foo/Foo.java arg1 arg2

whereas from Intellij it does not use javac.laucnher:

sun.java.command = /my/project/basedir/src/test/java/org/foo/Foo.java arg1 arg2

I tried changing the Java version in my JAVA_HOME to 1.8, and then it fails on CLI too. So the problem seems to come from Java version, but I confugred Maven Runner to use java 11 specifically.

Do I miss something for Maven configuration ? My version of intelliJ is

IntelliJ IDEA 2020.1.2 (Community Edition)
Build #IC-201.7846.76, built on June 1, 2020
Runtime version: 11.0.7+10-b765.53 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 4.15.0-76-generic
0
4 comments
Avatar
Yaroslav Bedrov

Hello,

Could you please share idea.log ("Help | Show Log in...") after restarting IDE and reproducing the issue?

0

I had a very similar problem today and the answer was: $PATH.

The way you use the exec-maven-plugin it will start a new process for the executable "java". That means, it will search $PATH for something called "java". This has nothing to do with your IDE settings or JAVA_HOME. It depends on the way the IDE was started and which $PATH it "inherited".

In my case, using Ubuntu, I set the path to JDK 11 in .bashrc and put it at the beginning of $PATH. That way it worked perfectly in a shell (which is using Bash). The default $PATH however did not include the additional path to JDK 11 but instead a path to the default JDK 8 on my system (in /usr/bin). So when the exec-maven-plugin looked for "java" in $PATH it would find JDK 8.

The solution was to configure the path to JDK in .profile, not in .bashrc. This way it will be recognized by IntelliJ and even by Bash, if you don't have a file called .bash_profile.

1
Avatar
Permanently deleted user

For me, the Maven > Runner JRE in the Preferences was the problem:

0

Old thread but I fell into the same issue.
I finally solved it by using java goal instead of exec goal provided by  exec-maven-plugin that used the same JVM as Maven

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>my-execution</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>my.package.MyMainClass</mainClass>
<arguments>
<argument>MyArg</argument>
</arguments>
</configuration>
</plugin>
0

Please sign in to leave a comment.