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

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>

Related

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

Good with maven 3.12.0, but 3.13.0:pmd failed: MavenReportException: /workspace/my-project/.out/reports/pmd/pmd.xml (No such file or directory)

Question about maven PMD plugin.
I used to have maven pod plugin version 3.12.0
Very happy with this plugin, it was configured that way, everything was working fine.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.12.0</version>
<configuration>
<targetDirectory>.out/reports/pmd</targetDirectory>
<outputDirectory>target/reports/pmd</outputDirectory>
</configuration>
</plugin>
However, I wanted to upgrade to the newest version 3.13.0 and I am now facing this issue.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<targetDirectory>.out/reports/pmd</targetDirectory>
<outputDirectory>target/reports/pmd</outputDirectory>
</configuration>
</plugin>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd (default-cli) on project my-project: Execution default-cli of goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd failed: org.apache.maven.reporting.MavenReportException: /workspace/my-project/.out/reports/pmd/pmd.xml (No such file or directory) -> [Help 1]
This is literally a one character change. The 3.12 version is working perfectly fine, while with 3.13.0, it is failing 100% reproducible.
May I ask what is the issue please?
This is now fixed with version 3.14.0 of the same plugin by PMD team.

Eclipse build war success, but Maven command canot

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.

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 doesn't build project with surefire plugin in pom

I have a maven project with Selenium WebDriver tests using TestNG. I would like to run tests in groups, thats why is important to have surefire plugin in pom file, thats what i added to pom file:
If i run my project after adding those words using maven(using testNG is still working), i get an error in console and WebDriver doesn't even open.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.15:test
(default-test) on project PORefactor: Execution default-test of goal
org.apache.maven.plugins:maven-surefire-plugin:2.15:test failed: There was an error in
the forked process.
and also :
[ERROR] Cannot find class in classpath: com.volnoboy.POImplementation
I tried to refresh and clean project. I moved project to a different computer and error is the same. Internet connection is good. What should i do?
This is my pom
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<!-- <groups>1</groups> -->
<suiteXmlFiles>
<suiteXmlFile>testSuites\testng1.xml</suiteXmlFile>
<suiteXmlFile>testSuites\testng1.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.15</version>
</plugin>
Try with that plugin.
And of course you need the testng and selenium dependecies in your pom.
Hope it helps.

Categories

Resources