Eclipse build war success, but Maven command canot - java

I code Java 7 on Java 8 JDK
When I run
mvn clean install
it show error
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project foo: Compilation failure: Compilation failure:
[ERROR] /E:/bar/XmlUtils.java:[9,44] package com.sun.xml.internal.bind.marshaller does not exist
[ERROR] /E:/bar/XmlUtils.java:[11,34] cannot find symbol
[ERROR] symbol: class CharacterEscapeHandler
[ERROR] /E:/.../FooServiceImpl.java:[106,44] package com.sun.xml.internal.bind.marshall
This is line of code make error
CharacterEscapeHandler.class.getName()
Maven can't find
import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
Maven can't find CharacterEscapeHandle. How to fix it?
but when I build, package, run by Eclipse Neon, no problem. How to fix it?

Normally you should not use any code from package com.sun.xml.internal. And this fails intentionally. Here are more details: https://bugs.java.com/bugdatabase/view_bug.do;jsessionid=1711ee4eae3ff10565fc7a212018?bug_id=6778491

This is temporary solution
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>rt.jar</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<fork>true</fork>
<compilerArgument>-XDignore.symbol.file</compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Maven builds success.

Related

Could not find artifact jdk.tools

I am migrating my code from Java 1.8 to Java 11. I have done the below change in my pom.xml
From:
<java.version>1.8</java.version>
To:
<java.version>11</java.version>
Also changed the source and target in maven compiler as per below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
When I execute the command maven install I get the below error:
[ERROR] Failed to execute goal on project store: Could not resolve dependencies for project. Could not find artifact jdk.tools:jdk.tools:jar:1.7 at specified path C:\Program Files\Java\jdk-11.0.12/../lib/tools.jar -> [Help 1]
How do I solve this error? Please help

Why can't Idea not find sources generated by ANTLR4?

I have a project in which I want to use classes generated by ANTLR4 in a piece of Kotlin code.
In pom.xml, ANTLR4 is configured as follows.
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7.1</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
The generated classes are put into target/generated-sources/antlr4:
mvn clean package, mvn clean assembly, as well as rebuilding the project in Idea lead to the following error:
Note that the errors occur only in the Kotlin class Transpiler.kt, but not in the test.
How can I fix this (make sure that classes generated by ANTLR4 can be used in Kotlin code)?
Update 1: After moving the grammar file as suggested by #Bart Kiers and executing mvn clean antlr4:antlr4, the errors in Idea disappeared. However mvn clean antlr4:antlr4 install still results in build errors:
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.4.21:compile (compile) on project elispt: Compilation failure: Compilation failure:
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[9,21] Unresolved reference: DeplorableLexer
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[11,22] Unresolved reference: DeplorableParser
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[12,21] Unresolved reference: DeplorableParser
It should work if you do the following:
move Deplorable.g4 to src/main/antlr4/com/dpisarenko/deplorable/ (note that you placed it inside src/main/antlr4/com.dpisarenko.deplorable/!)
run mvn clean antlr4:antlr4
if not already done, mark target/generated-sources/antlr4 as the "Generated Sources Root" (right click it in your IDE and select Mark Directory as)
If not, try using the latest ANTLR4 version: 4.9.1 (not just the tool and runtime, but also for antlr4-maven-plugin).

failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.7

I'm getting this error all of a sudden in maven and i'm not sure why:
Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.7:resources (default-resources) on project
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.7:resources (default-resources) on project
I searched in intellij for 2.7 as well as maven-resources-plugin and we aren't using those. Why is maven suddenly complaining about this when I don't even use that plugin?
There seems to be a problem with the maven-filtering dependency.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</plugin>
I had .der files in my src/main/resources which now when removed it builds successfully.

Maven can't compile using rt.jar

My project uses sun.security.tools.keytool to generate certificate under JDK 1.8 and this package can be found in rt.jar. According to Introduction to the Dependency Mechanism, System Dependencies, I can add rt.jar as a dependency to my project:
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>rt.jar</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
I'm pretty sure Maven found this jar file. However when I import sun.security.tools.keytool.Main, it still generates an error. Moreover, the most strange thing is if I copy rt.jar into someplace and fill its path in pom.xml, it works! As soon as I switch back to use the original rt.jar, it fails!
Can anyone tell me how could this happen?
I created a Maven project and added the <dependency> of your question to its POM.
First I got:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[6,34]
package sun.security.tools.keytool does not exist
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[12,50]
cannot find symbol
symbol: variable Main
location: class igb.so.SO31353565
Then, according to Cannot find symbol (CertAndKeyGen) with JDK8, I added:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<fork>true</fork>
<compilerArgument>-XDignore.symbol.file</compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
to the POM and the compilation succeeded.
If you are using Gradle instead of Maven you can add this to your build:
compileJava {
options.fork = true
options.forkOptions.executable = 'javac'
options.compilerArgs << "-XDignore.symbol.file"
}
It worked for me! ;)
I solved it like this:
Add to JAVA_HOME/lib/rt.jar
Go to Eclipse Menu Windows -> Windows -> Preferences -> Java -> Installed JREs
Select jdk version. In me case jdk-11.0.4
Select Add External JARs.. and add JAVA_HOME/lib/rt.jar

Maven "class file for ... not found" compilation error

I have a Maven project. When I try to build it with Maven, I get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.
3.2:compile (default-compile) on project myProject: Compilation failure:
Compilation failure:
[ERROR] ClassA.java:[32,38] cannot access ClassB
[ERROR] class file for ClassB not found
ClassB is inside another artifact, and that artifact is in the local repository. In fact, I have no problems building this project with the m2eclipse Maven plugin. It's only when I run mvn compile that the build fails.
What do I have to do to build from the command line?
Eclipse might be bypassing your pom dependencies and 'helping' you by finding a dependency that is not in your pom. Then when you run from command line eclipse isn't there to help anymore. I would double check your pom that you explicitly state dependency. You can also try
mvn dependency:analyze
for more info.
That looks like you have defined a usual class within the src/test/java instead of the src/main/java area which is sometimes the problem cause the eclipse things behaves a little bit different.
add to pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>src\main\webapp\WEB-INF\lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>

Categories

Resources