Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven compilation issue with Java 9

Tags:

maven

java-9

Trying to compile a Maven project using JDK 9.0.1 I'm facing this stacktrace without much of an explanation:

Exception in thread "main" java.lang.AssertionError at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155) at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46) at jdk.compiler/com.sun.tools.javac.comp.Modules.enter(Modules.java:250) at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.readSourceFile(JavaCompiler.java:821) at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$ImplicitCompleter.complete(JavacProcessingEnvironment.java:1510) at jdk.compiler/com.sun.tools.javac.code.Symbol.complete(Symbol.java:633) at jdk.compiler/com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:1314) at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.complete(Type.java:1139) at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.getTypeArguments(Type.java:1065) at jdk.compiler/com.sun.tools.javac.code.Printer.visitClassType(Printer.java:237) at jdk.compiler/com.sun.tools.javac.code.Printer.visitClassType(Printer.java:52) at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.accept(Type.java:992) at jdk.compiler/com.sun.tools.javac.code.Printer.visit(Printer.java:136) at jdk.compiler/com.sun.tools.javac.util.AbstractDiagnosticFormatter.formatArgument(AbstractDiagnosticFormatter.java:197) at jdk.compiler/com.sun.tools.javac.util.AbstractDiagnosticFormatter.formatArguments(AbstractDiagnosticFormatter.java:165) at jdk.compiler/com.sun.tools.javac.util.BasicDiagnosticFormatter.formatMessage(BasicDiagnosticFormatter.java:111) at jdk.compiler/com.sun.tools.javac.util.BasicDiagnosticFormatter.formatMessage(BasicDiagnosticFormatter.java:67) at jdk.compiler/com.sun.tools.javac.util.AbstractDiagnosticFormatter.formatArgument(AbstractDiagnosticFormatter.java:183) at jdk.compiler/com.sun.tools.javac.util.AbstractDiagnosticFormatter.formatArguments(AbstractDiagnosticFormatter.java:165) at jdk.compiler/com.sun.tools.javac.util.BasicDiagnosticFormatter.formatMessage(BasicDiagnosticFormatter.java:111) at jdk.compiler/com.sun.tools.javac.util.BasicDiagnosticFormatter.formatMessage(BasicDiagnosticFormatter.java:67) at jdk.compiler/com.sun.tools.javac.util.JCDiagnostic.getMessage(JCDiagnostic.java:771) at jdk.compiler/com.sun.tools.javac.api.ClientCodeWrapper$DiagnosticSourceUnwrapper.getMessage(ClientCodeWrapper.java:799) at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:131) at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:174) at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:1075) at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:168) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146) 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:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:993) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345) at org.apache.maven.cli.MavenCli.main(MavenCli.java:191) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) 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) 

Not really sure what's causing this, is this a bug in the JDK?

Additional details:

  • Maven 3.5.0 with maven-compiler-plugin 3.7.0
  • I'm just executing mvn clean install
  • The source code is not open source unfortunately, so I'm not at liberty to share it
  • There are no module-info.java files yet, I'm just trying to compile a project using Java 9
  • Strangely enough if I leave the source level on 1.8, the code compiles, but it fails with the above exception if I specify it as 9
like image 356
Peter Major Avatar asked Oct 22 '17 20:10

Peter Major


People also ask

Which Java version does Maven use?

Maven Configuration The Maven tool uses JDK version 11.0.

Does Maven work with Java 11?

Chances are that it just runs fine with Java 11. Hint: You can speed up multi-module Maven projects by using parallel builds, e.g. mvn -T 4 compile compiles all modules in parallel on 4 CPU cores.

Does Maven support Java 17?

In case of Maven, we specify the compiler plugin to use Maven with Java 17. The next step is to basically upgrade the dependencies of our application.


2 Answers

Just add this

<forceJavacCompilerUse>true</forceJavacCompilerUse> 

to your maven compiler build plugin in your POM and you'll see all the javac errors! Source with more details

like image 174
Jason Avatar answered Sep 30 '22 08:09

Jason


Debugging

Step one should be to add the maven-compiler-plugin and enable <forceJavacCompilerUse>true</forceJavacCompilerUse> as the top answer suggests.

<project>   <build>     <plugins>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-compiler-plugin</artifactId>         <version>3.8.1</version>         <configuration>           <forceJavacCompilerUse>true</forceJavacCompilerUse>         </configuration>       </plugin>       ... 

This will point out the actual compilation error.

[main] INFO org.apache.maven.plugin.compiler.CompilerMojo - ------------------------------------------------------------- [main] ERROR org.apache.maven.plugin.compiler.CompilerMojo - COMPILATION ERROR :  [main] INFO org.apache.maven.plugin.compiler.CompilerMojo - ------------------------------------------------------------- [main] ERROR org.apache.maven.plugin.compiler.CompilerMojo -    last round: true /home/vsts/work/1/s/src/main/java/com/company/services/TemplateService.java:[3,61] error: cannot find symbol   symbol:   class VariableNotFoundException 

Root Cause

For me the root cause was that I made a commit and pushed it to the server which triggered CI, but did not include one class in the commit that was being used somewhere. Hence the compiler was not able to find it in the CI environment.

throws VariableNotFoundException { 

The solution is to make sure you don't have any Git staged files that you forgot to include as part of your commit.

like image 31
mvd Avatar answered Sep 30 '22 07:09

mvd