I'm trying to set up robot on top of an Eclipse Maven-Selenium-TestNG java project I created, but it doesn't seem to be picking up default keywords (I haven't even tried adding my own yet).
I started by creating a maven project and adding to pom.xml the dependencies for selenium 3.4, testNG 6.8 and robot 3.0.2, then also added robot plugin 1.4.7. Finally, updated the project so maven downloads all the needed stuff.
To test selenium (without robot) I created a textNG class in src>test>java, added a system property pointing to the chromedriver.exe file in my system and added a simple test that just opens the browser and navigates to google. It worked, so now I want to use robot on top of that.
This is my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.automation</groupId>
<artifactId>automated_tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.robotframework</groupId>
<artifactId>robotframework</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.robotframework</groupId>
<artifactId>robotframework-maven-plugin</artifactId>
<version>1.4.7</version>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I created a file in src/test/robotframework/acceptance, with the following contents:
*** Settings ***
Test Set Up Start Selenium Server
Test Tear Down Stop Selenium Server
*** Test Cases ***
Visit google
Open Browser https://www.google.com chrome
Close Browser
However, when I run as maven install, I get:
Setup failed: No keyword with name 'Start Selenium Server' found.
Also teardown failed: No keyword with name 'Stop Selenium Server'
found.
So why is it that robot is not finding the keywords implementation? And how do I add implementations of my own keywords?
The reason robot isn't finding the keywords is that you aren't importing the library that contains the keywords. Start Selenium Server is part of the deprecated SeleniumLibrary. In order to use the keywords you must import them with the Library setting:
*** Settings ***
Library SeleniumLibrary
Test Set Up Start Selenium Server
Test Tear Down Stop Selenium Server
Assuming that the folder where SeleniumLibrary is installed is in your PYTHONPATH, robot will import the library and make the keywords available to you.
I actually found out I was missing a maven dependency:
<dependency>
<groupId>com.github.markusbernhardt</groupId>
<artifactId>robotframework-selenium2library-java</artifactId>
<version>1.4.0.8</version>
</dependency>
Also, I don't need to use Start Selenium Server and Stop Selenium Server because they're deprecated.
After that, I was able to run my test by creating a custom keyword to set browser path (I'm using chromedriver):
I created a .java file within src/main/java/demo and added a method that sets up the property:
package demo;
public class Setup {
public void driverPath() {
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
}
}
Then, I created src/test/robotframework/acceptance/Resource.robot file and imported my library:
*** Settings ***
Library Selenium2Library
Library demo.Setup
Also created a src/test/robotframework/acceptance/__init__.robot file and used the keyword I created (Browser Setup):
*** Settings ***
Test Setup Driver Path
Test Teardown Close All Browsers
Test Timeout 2 minute 30 seconds
In my test, I invoked Resource.robot:
*** Settings ***
Resource Resource.robot
*** Test Cases ***
Visit google
Open Browser https://www.google.com chrome
Related
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.
I'm writing Selenium Junit tests with IntelliJ. The tests run ok if I trigger from test directly. However, if I trigger tests from TestRunnerSuite with JunitCore, I encountered following weird error that I did not find a solution after researching on google. Similar questions on DriverService$builder, but not my error type.
[main] ERROR sire.responseOrg.TestIncidents - java.lang.AbstractMethodError: org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:332)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at sire.responseOrg.WebDrivers.getInstance(WebDrivers.java:15)
at sire.responseOrg.util.util1.setupChromeDriver(util1.java:51)
at sire.responseOrg.Test1.setUp(Test1.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at ......Omitted
at org.junit.runner.JUnitCore.run(JUnitCore.java:127)
at org.junit.runner.JUnitCore.runClasses(JUnitCore.java:76)
at sire.responseOrg.TestSuiteRunner.main(TestSuiteRunner.java:24)
I'm using Selenium 3.5.3 and chrome 76.---> Updated to Selenium 3.141.59,and with main scope.
Now getting error
java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:93)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:72)
at org.openqa.selenium.remote.service.DriverCommandExecutor.<init>(DriverCommandExecutor.java:63)
at org.openqa.selenium.chrome.ChromeDriverCommandExecutor.<init>(ChromeDriverCommandExecutor.java:36)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at sire.responseOrg.WebDrivers.getInstance(WebDrivers.java:15)
at sire.responseOrg.util.SeleniumUtil.setupChromeDriver(SeleniumUtil.java:62)
at sire.responseOrg.TestIncidents.setUp(TestIncidents.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at org.junit.runner.JUnitCore.run(JUnitCore.java:127)
at org.junit.runner.JUnitCore.runClasses(JUnitCore.java:76)
at sire.responseOrg.TestSuiteRunner.main(TestSuiteRunner.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.http.auth.Credentials
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 33 more
Full pom.xml dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myGroupId</groupId>
<artifactId>myArtifactId</artifactId>
<version>1.0-SNAPSHOT</version>
<description>My description</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>main</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
<scope>main</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
<scope>main</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.6</version>
<scope>main</scope>
</dependency>
<dependency>
<groupId>com.salesforce.seti</groupId>
<artifactId>selenium-dependencies</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<packaging>pom</packaging>
</project>
My project folder structure is
.src
...main
.....java
.......projectname
.........constantsFolder
.........utilFolder
...........util1.java
...........util2.java
.........Test1.java
.........TestRunnerSuite.java
.........WebDrivers.java
If I start test from Test1.java, the test runs regularly though with warnings
[main] INFO projectname.util.util1 - Set up chrome driver.
Starting ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770#{#1003}) on port 28755
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1566609934.853][WARNING]: This version of ChromeDriver has not been tested with Chrome version 76.
Aug 23, 2019 6:25:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
[main] INFO projectname.util.util1 - Navigating to https://mytest.com/
However, after adding a testSuiteRunner as below.
#RunWith(Suite.class)
#Suite.SuiteClasses({ Test1.class })
public class TestSuiteRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Test1.class);
// print erros, exit etc omitted
}
}
Now I get the weird error and cannot fire the chromedriver.
The webdriver I have is singleton
public class WebDrivers {
private static WebDriver driver = null;
public static WebDriver getInstance(){
if (driver == null) {
driver = new ChromeDriver();
}
return driver;
}
}
It's my first time to work on setting everything up from grounds. I'm not sure if it's pom dependency issue, singleton webdriver issue, or something else. Could anyone share an eyesight on this and give some clues? Much appreciated.
This error message...
java.lang.AbstractMethodError: org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;
...implies that there is some incompatibility between the version of the binaries you are using specifically with the guava dependency.
You are using chrome= 76.0
You are using the following:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
Your Selenium Client version is 3.5.3 which is more then 2 years older.
Your JDK version is unknown to us.
So there is a clear mismatch between the Selenium Client v3.5.3 and Chrome Browser v76.0
However as per the discussions in:
java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.builderWithExpectedSize
NoSuchMethodError: com.google.common.collect.ImmutableList.toImmutableList()Ljava/util/stream/Collector; after upgrade to 2.0.16
These issues crop up due to incompatibile Guava dependency.
The current guava version used within selenium-java-3.141.59 is guava-25.0-jre
Solution
Ensure that:
JDK is upgraded to current levels JDK 8u222.
Selenium is upgraded to current levels Version 3.141.59.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test as non-root user.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Update
So presumably your main question with respect to the error:
java.lang.AbstractMethodError: org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;
is solved. Congratulations.
Now, as per your question update as you are seeing the error:
java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
There are two aspects.
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. You can find a detailed discussion in Exception in thread “main” java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
http/auth: Traces of http/auth implies http client is still in use where as the CHANGELOG reflects:
The HttpClient implementation details were out of HttpCommandExecutor right from Selenium v2.45.0.
With the availability of Selenium v3.11, Selenium Grid was switched to use OkHttp rather than the Apache HttpClient.
Further with the release of Selenium v3.141.0, Apache HttpClient was removed from selenium-server-standalone which drastically
reduced the size of selenium server distribution package.
Even the apache-backed httpclient was also removed.
You can find a detailed discussion in org.openqa.selenium.remote.internal.ApacheHttpClient is deprecated in selenium 3.14.0 - What should be used instead?
Remove scope from your POM.
test
or main
Isn't needed for your tests to run
used this gauva jar with latest testng 7.3. Resolved this error also dont configure any testNG seperately. Please remove configuration if we add it in pom.xml
I was facing the same issue. The following steps helped resolve it:
Go to your POM file and comment out/ remove the following dependency:
--> org.seleniumhq.selenium
--> selenium-java
--> 2.48.2
Check what version you have.
Then copy the latest mvn dependency which is 4.1.1 as of now
Perform mvn clean install (till this point it will be sufficient)
Perform reload all maven projects
Perform download sources
Build your project again
Play/ perform mvn clean install again
FYI: as per my knowledge, this issue occurs at compile time when you execute the code. Or the time when you execute a code in a new project which was compiled with different dependencies and versions earlier.
new user here!
I know there's similar questions with answers to this, but I don't know how to apply them to my case, so sorry if it's repetitive!
So... I'm trying to make my first bot for Telegram and I've decided to use Java. I'm following this tutorial and copypasted the code from the two example classes (EchoBot and Main). The only thing I changed is the token with the token I got from the BotGodfather on Telegram.
I'm using Eclipse on Ubuntu 18.04.1 as IDE so I started by making a Java project and then configured it as a Maven project. This is the code of my pom.xml:
`
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>EchoBot</groupId>
<artifactId>EchoBot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EchoBot</name>
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
`
When I run the program I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Module
at org.telegram.telegrambots.ApiContext.getInjector(ApiContext.java:46)
at org.telegram.telegrambots.ApiContext.getInstance(ApiContext.java:25)
at org.telegram.telegrambots.bots.TelegramLongPollingBot.(TelegramLongPollingBot.java:17)
at pearlbot.EchoBot.(EchoBot.java:8)
at pearlbot.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: com.google.inject.Module
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 5 more
What could the problem be?
Keep in mind that I don't even know what Maven is, so if it's something related to it, you'll need to explain what's wrong as if you were talking to a child! ^^''
I got this error when i try to install testng for eclipse Version: 2019-12 (4.14.0) and run the program then i got this error. Finally i got fixed -
Eclipse IDE for Java Developers, Version: 2019-12 (4.14.0), Build id: 20191212-1212.
TestNG 7.1.0.r202001120626
Removing the TestNG library from the build path of the project containing the test and installing TestNG from menu Help / Install New Software did not work for me, I kept getting this error.
What worked for me was downloading guice-4.2.2.jar (from https://github.com/google/guice/wiki/Guice422), copying it into any folder, and adding it to the build path of the project as external JAR.
From Eclipse, Go to Help > Install software or You can install from market place as well (Help > Market Place). After installing TestNG from market place, it dint work, but installing the GUICE422.jar to the build path worked for me
So make sure after installing TestNG from market place, install the Guice422 Jar file as well link to install testng from eclipse market place
This worked for me --- Hope it helps
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.
I am trying to build a JAR library that can invoke R code.
I basically want this jar to be capable enough to be able to run on any machine that has support for running jar executables(No need of seperate R software).
For this I am using Maven. I am able to compile and create a jar without any errors. However, when I run it, I am unable to yield successful results.
This is my java code
package com.company.analytics.timeseries;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
public class App {
public static void main(String[] args) {
System.out.println("Creating Rengine (with arguments)");
String[] Rargs = { "--vanilla" };
Rengine re = new Rengine(Rargs, false, null);
System.out.println("Rengine created, waiting for R");
if (!re.waitForR()) {
System.out.println("Cannot load R");
return;
}
System.out.println("Done.");
}
}
This is my pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.analytics</groupId>
<artifactId>timeseries</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>timeseries</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.nuiton.thirdparty</groupId>
<artifactId>JRI</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>REngine</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
</project>
I used mvn clean and then mvn package to create the jar file.
A JAR file of 4KB is created in C:\MVN\project\analytics\timeseries\target. Thern, from the command line on Windows, when I run execute this jar file, I get the following error
C:\MVN\project\analytics\timeseries\target\classes>java com.company.analytics.timeseries.App
Creating Rengine (with arguments)
Exception in thread "main" java.lang.NoClassDefFoundError: org/rosuda/JRI/Rengine
at com.company.analytics.timeseries.App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.rosuda.JRI.Rengine
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I am trying to figure out what mistake am I committing. I tried to find answers by googling, but I couldn't fix it.
Since I've been smashing my head against this for a day now and I'll likely forget in the future and reference this page - per what Gergely Basco's suggests in an above comment, strictly speaking both R and rJava need to be installed on the machine in order to resolve the Cannot find JRI native library! issue when instantiating your org.rosuda.REngine.REngine object, and this cannot be done exclusively by way of adding the JRIEngine dependency in your pom.xml (bummer).
Steps (for how I'm doing it anyway for my later image):
Install Brew (I just happen to be using Brew for other dependencies)
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install R using brew:
brew tap homebrew/science
brew install R
Install rJava with R (takes a bit of compile time, grab a coffee)
install.packages("rJava")
add rJava/jri to java.library.path classpath, add R_HOME to environment variables (where you installed R - in my case, where Brew installed it). Note that if you're trying to run this in your IDE(I'm running IDEA16), it won't inherit the path you set in ~/.bash_profile, you need to set it in your run configuration.
-Djava.library.path="/usr/local/lib/R/3.3/site-library/rJava/jri/
R_HOME=/usr/local/Cellar/r/3.3.1_2/R.framework/Resources
Ensure maven has dependency for JRIEngine in pom.xml
<dependency>
<groupId>com.github.lucarosellini.rJava</groupId>
<artifactId>JRIEngine</artifactId>
<version>0.9-7</version>
</dependency>
Instantiate REngine (I need this version in order to pass dataframe to R from java)
String[] Args = {"--vanilla"};
REngine engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", Args, new REngineStdOutput (), false);
What you should end up with looks something like this at runtime, if you instantiate with the callback argument (new REngineStdOutput () ); otherwise if you just instantiate with the String engineForClass("org.rosuda.REngine.JRI.JRIEngine"), you'll wont get the below output from R on startup/elsewise, depending on if you want it or not:
/**R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.5.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.**/
Hope this helps someone in the future and saves them from the pain.
You need to build a jar with all your dependencies included. (aka fat jar) Since you are already using Maven, the only thing you need to do is to instruct Maven to include the dependencies by adding this plugin to your pom.xml file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You are missing the classpath argument. Your jar file contains your compiled code without any 3rd party jars. When you want to run it, you should add -cp and point to all your 3rd party jars.
You can also build a single jar with all dependencies using Maven's assembly plugin.