I'm trying to use checkstyle and findbugs by doing mvn checkstyle:checkstyle but I have this error
This error
[ERROR] Failed to execute goal on project my-project: Could not resolve dependencies for project default:my-project:jar:1.1: Could not find artifact com.sun:tools:jar:0 at specified path C:\Program Files\Java\jdk-11.0.2/../lib/tools.jar -> [Help 1]
I do not have a tools.jar into my JDK (i have jdk-11.0.2).
I'm on it since 2H, please help :/
tools.jar removed from Java 9+
You're on JDK 11. No tools.jar found there.
JEP 220: Modular Run-Time Images removed both tools.jar and rt.jar from the lib folder, as of Java 9.
Removed: rt.jar and tools.jar
The class and resource files previously stored in lib/rt.jar,
lib/tools.jar, lib/dt.jar, and various other internal JAR files are
now stored in a more efficient format in implementation-specific files
in the lib directory. The format of these files is not specified and
is subject to change without notice.
This change is part of adding the Java Platform Module System, a.k.a. Project Jigsaw.
To upgrade Checkstyle, use later versions, such as:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.18</version>
</dependency>
</dependencies>
</plugin>
Notice the inner dependency block.
Consult a Maven repo for versions of Apache Maven Checkstyle Plugin and of Checkstyle.
Related
I'm learning Java. To read JSON in my application, I downloaded this JSON library; which is an automatic module.
I included that library in my module descriptor like:
module art
{
exports art.anixt;
exports art.coartl;
exports art.runeape;
requires org.json; // org.json cannot be resolved to a moduleJava(8389908)
}
My settings.json in vscode:
{
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"java.format.settings.url": "Format.xml",
"java.format.settings.profile": "style",
"java.project.referencedLibraries": [
"lib/**/*.jar" // jar file showing in Referenced library(see screenshot)
]
}
How do I include the jar file in my module and import it into my Java file?
Screenshot:
TL;DR — As this unresolved 'Cannot be resolved' errors in projects with module-info.java issue reports, vscode is brain dead when it comes to JPMS and module-info.java.
The long-winded version
From my own experience, I can personally vouch for what the reporter of the above-linked vscode issue reports…
„…I've tried both Gradle and Maven…“
…
„…I find that Gradle and Maven will automatically refresh the classpath file and remove my modifications to it, which will bring back the errors…“
…
„…there needs to be module path information set in the classpath file in order for Eclipse to be happy, but there is no good way to do with that from Gradle or Maven…“
Proof that it's a vscode issue is that the exact same project — unchanged except for the removal of your comment — compiles perfectly fine in IntelliJ…
Since your project uses neither Maven nor Gradle — opting instead to use file-based dependency mgt with the jar in the lib folder — you're in even worse shape because you've eliminated the option of applying any JPMS-enabling plugins that could resolve the issue.
For example, by adding the following pom.xml with the appropriate configuration for the maven-compiler-plugin to my experimental version of your project…
…
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
<arg>--add-modules</arg>
<arg>org.json</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
…
…Maven does its magic and processes the module-info.java successfully…
I've successfully resolved other Stackers' JPMS woes by helping them apply that mrJar plugin mentioned in that vscode bug report. So if you're open to using Gradle instead of Maven, I could likewise advise you on how to configure that plugin too.
I have added the most updated Selenium dependency in my pom.xml
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
I ran
mvn clean install
inside the directory with my pom.xml and I have also imported the correct classes in my app class as per the Selenium documentation
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
However when i try and run my main method, I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
I look in my ~/.m2/repository folder and I don't see an openqa folder but instead I see a seleniumhq folder.
Why didn't maven install the openqa folder, and why does the documentation say to import from org.openqa... when that never exist in my jar repository. I'm very confused, I just want to be able to import selenium Webdriver successfully while having it in my local repository.
Firstly, check properly if you have all important dependencies for your program.
Secondly, I had similar error while running maven project:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor
And this problem was because of inappropriate plugin, because I tested different versions of Selenium and it didn't help me.
So when I changed maven-jar-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>your_main_class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
to maven-shade-plugin plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your_main_class</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
The issue was gone.
The difference between plugins you can find here.
In addition, sometimes we upgrade our libraries even with same method name. Due this different in version, we get NoClassDefFoundError or NoSuchMethodError at runtime when one library was not compatible with such an upgrade.
Java build tools and IDEs can also produce dependency reports that tell you which libraries depend on that JAR. Mostly, identifying and upgrading the library that depends on the older JAR resolve the issue.
To summarize:
try to change versions of Selenium, if it contains all dependencies;
try to add necessary dependencies if you don't have it;
try to check folder of maven if it has or not what says specific error;
try to play with plugins if nothing helps above.
NoClassDefFoundError
NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVM will throw NoClassDefFoundError.
The error you are seeing is :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
This clearly indicates that Selenium is trying to resolve the particular class at runtime from org/openqa/selenium/WebDriver which is no more available.
As you mentioned of looking into ~/.m2/repository folder, the maven folder structure for Selenium v3.7.1 (on Windows) is as follows :
C:\Users\<user_name>\.m2\repository\org\seleniumhq\selenium\selenium-java\3.7.1
So when you see a seleniumhq folder, it is pretty much expected.
What went wrong :
From all the above mentioned points it's clear that the related Class or Methods were resolved from one source Compile Time which was not available during Run Time.
This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK / Maven / Gradle.
Solution :
Here are a few steps to solve NoClassDefFoundError :
While using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.
If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused one.
While using Maven, either use <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
Remove the unwanted other <dependency> from pom.xml
Clean you Project Workspac within your IDE periodically only to build your project with required dependencies.
Use CCleane tool to wipe away the OS chores periodically.
While you execute a Maven Project always do maven clean, maven install and then maven test.
Encountered this error in Eclipse IDE. In Eclipse go to Project properties and in Java Build Path just add selenium jars in Classpath instead of Modulepath. Then under the Project tab on the top do a Clean to remove earlier buiid and then do a Run.
Are you using an IDE or working from command line? In Eclipse for example you can force downloading all dependencies by right clicking on your project, going to Maven menu item and then selecting Update Project. Then check the "Force Update of Snapshots/Releases" checkbox.
If you are opening from command line do:
mvn clean install -U
from your project path.
This is happening because you are selecting jar files under modulepath, you should add them under class path.
org.openqa.selenium is the package in the selenium-api-{version}.jar under the seleniumhq\selenium\selenium-api folder.
org.openqa.selenium.firefox is the package in the selenium-firefox-driver-{version}.jar under the seleniumhq\selenium\selenium-firefox-driver folder.
So there is no openqa folder, it's just the package name under the seleniumhq folder, you should have a check into these jar.
It's hard to say what caused NoClassDefFoundError exception without project structure and code detail. The exception is not the same as ClassNotFoundException. Maybe this answer https://stackoverflow.com/a/5756989/5374508 would be helpful.
What worked for me was to add this dependency to pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
</dependency>
I was getting below error from past 2 days and what helped me was to remove all the selenium extra dependencies like selenium-support, selenium-chrome-driver etc and only keeping the below dependencies in POM file.
Error:-
java.lang.NoClassDefFoundError: org/openqa/selenium/HasAuthentication
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
Dependencies in the pom file after removing all other:-
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Have encountered this issue while running selenium test in eclipse IDE.
Navigate to following path:
1.Properties >> Java build path >> Libraries.
2.Add all selenium jars in Classpath instead of Modulepath.
3.Apply and close modal.
4.Now go to build path and click on "Configure Build Path".
5.Now run the selenium test.
Following import in an existing class is throwing a compilation error "import cannot be resolved" on eclipse -
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
The corresponding jar inclusion in pom.xml is -
<properties>
<fasterxml.version>0.7.0</fasterxml.version>
</properties>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>jackson-module-hibernate</artifactId>
<version>${fasterxml.version} </version>
</dependency>
I checked that the corresponding jar file is not present in .m2.
There is no such directory as jackson-module-hibernate inside .m2/fasterxml/
However, jar corresponding to another declaration is present in .m2 -
com.fasterxml.jackson.core
jackson-annotations
2.6.1
Is present in .m2/fasterxml/jackson/core/jackson-annotations/2.6.1
as jackson-annotations-2.6.1.jar
Setup details
Maven version - 3.3
Java version - 1.8.
Eclipse latest version - 2019-06.
I could not verify the absence of the first jar in the working sandbox setup, but that's how it should be, as we had taken a backup of the .m2 directory. How is it possible that the application runs in another setup without the presence of the jar.
Note - I am still a struggler when it comes to maven dependencies and the setup of this legacy project has made me pull hairs. I am trying to do the setup on eclipse. It got setup sometime back, after a lot of struggle, but before I could document all the steps/workarounds we made, I deleted the working setup by mistake.
Update
The pom declaration for jackson-annotations is in the pom of another project. That project has been included in the pom of the dependent project as -
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.dependencies</groupId>
<artifactId>dependency-project</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
You also need:
Execute mvn clean compile from Command Line, on another project
Execute mvn clean compile on your project
Right-click on Project > Maven > Update project...
I compiled openCV and OpenCV_contrib library and had the resulting build/bin/opencv-411.jar and libraries .so in build/lib/. Now I am wondering how can I add these to my Maven project?
I was using that Maven repository but the contrib modules are not available.
I tried including the jar as a dependency like here with:
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv-411</artifactId>
<version>4.1.0</version>
<scope>system</scope>
<systemPath>${resourcesfolder}/opencv-411.jar</systemPath>
</dependency>
I get no error when I launch clean javafx:compile but when I launch clean javafx:run a lot of OpenCV errors appear :
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/.../src/main/java/model/VideoModel.java:[3,23] package org.opencv.core does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[4,23] package org.opencv.core does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[5,26] package org.opencv.videoio does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[6,26] package org.opencv.videoio does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[25,17] cannot find symbol
symbol: class VideoCapture
location: class model.VideoModel
[ERROR] /home/.../src/main/java/model/VideoModel.java:[26,17] cannot find symbol
symbol: class Mat
I also had a look to that but he deployed his new jar containing the executables and libraries in a remote repository. The interesting part is the jar creation, so I tried to do the same with opencv-411.jar and lib/ folder with:
cp opencv-411.jar opencv-411-new.jar
jar -uvf opencv-411-new.jar lib/
and kept the dependency as above, but raised the same errors...
What should I do?
Thanks to the answers, I came to the solution for including OpenCV jar and libs to Maven:
install the jar in the maven local repository with:
mvn install:install-file -Dfile=/home/.../src/main/resources/opencv.jar -DgroupId=org -DartifactId=opencv -Dversion=4.1.1 -Dpackaging=jar
create the dependency in pom.xml:
<dependency>
<groupId>org</groupId>
<artifactId>opencv</artifactId>
<version>4.1.1</version>
</dependency>
Now that the jar is included, we must add the OpenCV libraries somehow.
Just load the library in your app with:
System.load("/path/to/lib/libopencv_java411.so");
For the UI tests, as the maven-surefire-plugin uses a special JVM, you have to specify the lib folder to the java.library.path:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-Djava.library.path=${project.build.outputDirectory}/lib</argLine>
</configuration>
</plugin>
Please feel free to edit if you have a better solution or corrections to add.
Maven expects the artifacts (.jar files, etc.) in $USER_HOME/.m2 since you compiled opencv yourself you also need to install the file (as 3rd party module) in the local maven repository:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Where the variables need to match those you use in you pom.xml and packaging must be set to jar.
See also: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
mvn install:install-file -Dfile=C:\Users\your jar path -DgroupId=org.opencv -DartifactId=opencv-411 -Dversion=4.1.0 -Dpackaging=jar
you can use this one may be solve your issue
I have added the most updated Selenium dependency in my pom.xml
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
I ran
mvn clean install
inside the directory with my pom.xml and I have also imported the correct classes in my app class as per the Selenium documentation
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
However when i try and run my main method, I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
I look in my ~/.m2/repository folder and I don't see an openqa folder but instead I see a seleniumhq folder.
Why didn't maven install the openqa folder, and why does the documentation say to import from org.openqa... when that never exist in my jar repository. I'm very confused, I just want to be able to import selenium Webdriver successfully while having it in my local repository.
Firstly, check properly if you have all important dependencies for your program.
Secondly, I had similar error while running maven project:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor
And this problem was because of inappropriate plugin, because I tested different versions of Selenium and it didn't help me.
So when I changed maven-jar-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>your_main_class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
to maven-shade-plugin plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your_main_class</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
The issue was gone.
The difference between plugins you can find here.
In addition, sometimes we upgrade our libraries even with same method name. Due this different in version, we get NoClassDefFoundError or NoSuchMethodError at runtime when one library was not compatible with such an upgrade.
Java build tools and IDEs can also produce dependency reports that tell you which libraries depend on that JAR. Mostly, identifying and upgrading the library that depends on the older JAR resolve the issue.
To summarize:
try to change versions of Selenium, if it contains all dependencies;
try to add necessary dependencies if you don't have it;
try to check folder of maven if it has or not what says specific error;
try to play with plugins if nothing helps above.
NoClassDefFoundError
NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVM will throw NoClassDefFoundError.
The error you are seeing is :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
This clearly indicates that Selenium is trying to resolve the particular class at runtime from org/openqa/selenium/WebDriver which is no more available.
As you mentioned of looking into ~/.m2/repository folder, the maven folder structure for Selenium v3.7.1 (on Windows) is as follows :
C:\Users\<user_name>\.m2\repository\org\seleniumhq\selenium\selenium-java\3.7.1
So when you see a seleniumhq folder, it is pretty much expected.
What went wrong :
From all the above mentioned points it's clear that the related Class or Methods were resolved from one source Compile Time which was not available during Run Time.
This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK / Maven / Gradle.
Solution :
Here are a few steps to solve NoClassDefFoundError :
While using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.
If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused one.
While using Maven, either use <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
Remove the unwanted other <dependency> from pom.xml
Clean you Project Workspac within your IDE periodically only to build your project with required dependencies.
Use CCleane tool to wipe away the OS chores periodically.
While you execute a Maven Project always do maven clean, maven install and then maven test.
Encountered this error in Eclipse IDE. In Eclipse go to Project properties and in Java Build Path just add selenium jars in Classpath instead of Modulepath. Then under the Project tab on the top do a Clean to remove earlier buiid and then do a Run.
Are you using an IDE or working from command line? In Eclipse for example you can force downloading all dependencies by right clicking on your project, going to Maven menu item and then selecting Update Project. Then check the "Force Update of Snapshots/Releases" checkbox.
If you are opening from command line do:
mvn clean install -U
from your project path.
This is happening because you are selecting jar files under modulepath, you should add them under class path.
org.openqa.selenium is the package in the selenium-api-{version}.jar under the seleniumhq\selenium\selenium-api folder.
org.openqa.selenium.firefox is the package in the selenium-firefox-driver-{version}.jar under the seleniumhq\selenium\selenium-firefox-driver folder.
So there is no openqa folder, it's just the package name under the seleniumhq folder, you should have a check into these jar.
It's hard to say what caused NoClassDefFoundError exception without project structure and code detail. The exception is not the same as ClassNotFoundException. Maybe this answer https://stackoverflow.com/a/5756989/5374508 would be helpful.
What worked for me was to add this dependency to pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
</dependency>
I was getting below error from past 2 days and what helped me was to remove all the selenium extra dependencies like selenium-support, selenium-chrome-driver etc and only keeping the below dependencies in POM file.
Error:-
java.lang.NoClassDefFoundError: org/openqa/selenium/HasAuthentication
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
Dependencies in the pom file after removing all other:-
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Have encountered this issue while running selenium test in eclipse IDE.
Navigate to following path:
1.Properties >> Java build path >> Libraries.
2.Add all selenium jars in Classpath instead of Modulepath.
3.Apply and close modal.
4.Now go to build path and click on "Configure Build Path".
5.Now run the selenium test.