unable to resolve class reference org/openqa/selenium/WebDriver - java

In the directory C:\jars> In the Jars folder I have Test folder. Inside the Test folder I have OneReports.jar ,testng.jar, TestngTestSuiteUsingXML.java and Test.XML
The OneReports.jar I have exported from eclipse and it contains a few java Projects.
The Test.xml I have created testsuite and that file is
In the command Prompt I tried to run:
C:\Jars>javac -cp "C:\Program Files\Java\selenium*.jar";"OneReports.jar";"testng
.jar" test\TestngTestSuiteUsingXML.java
C:\Jars>java -cp "C:\Program Files\Java\selenium*.jar";"OneReports.jar";"testng.
jar" test.TestngTestSuiteUsingXML
Here I got error as follows:
[[TestNGClassFinder]] Unable to read methods on class test.LoginOneReports - unable to resolve class reference org/openqa/selenium/WebDriver
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetPublicMethods(Unknown Source)
at java.lang.Class.getMethods(Unknown Source)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:59)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:153)
at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:522)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1289)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1057)
at test.TestngTestSuiteUsingXML.main(TestngTestSuiteUsingXML.java:20)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 18 more
C:\Jars>
Could any one please help me to fix the issue. I'm not using maven. Using Selenium webdriver, TestNG, Java for writing code.

Since your javac worked, you have testng.jar in the classpath correctly. Then the program has dependency on other libraries selenium etc. It will be correct to use individual jars instead of wild cards.
java -cp "C:\Program Files\Java\selenium<version>.jar;OneReports.jar;testng.jar" test.TestngTestSuiteUsingXML
This may give more classnotfound exception. Add only those to your classpath.
Note that you are referring other jars too by relative path (OneReports.jar or testng.jar). This can get pretty messy. Consider using ant/maven/gradle to do this part of the work. They would form the classpath more reliably.

Related

JNI error occurrence with TestNG classpath setup

I was looking at the TestNG documentation and I wanted to implement the DotTestListener class to override TestNG methods for logging and results (section 6.2.1 in the documentation)
https://testng.org/doc/documentation-main.html
I'm trying to use this command in cmd to invoke the listener:
java -classpath C:\TestNG\plugins\testng7.0.jar;C:\eclipse-2018\Tests org.testng.TestNG -listener org.testng.reporters.DotTestListener testng.xml
note: I renamed the TestNG library myself to "testng7.0.jar"
\Tests is my eclipse project folder
When I run the command, I'm getting this exception in the CLI:
Error: A JNI error has occurred, please check your installation and
try again Exception in thread "main" java.lang.NoClassDefFoundError:
com/beust/jcommander/ParameterException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException:
com.beust.jcommander.ParameterException
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)
... 7 more
Open cmd and cd into eclipse project directory, set the environment classpath as TestNG\plugins (all of the significant Jar libraries), path of selenium dependencies, and the path of the TestNG project bin folder containing the .class files:
java -classpath C:\eclipse-2018\Tests\bin;C:\Selenium_dependencies\*;C:\TestNG\plugins\* org.testng.TestNG -listener org.testng.reporters.DotTestListener testng.xml
the classpath may also be set this way:
1.
set classpath=C:\eclipse-2018\Tests\bin;C:\Selenium_dependencies\*;C:\TestNG\plugins\*
2.
java -classpath %classpath% org.testng.TestNG -listener org.testng.reporters.DotTestListener testng.xml
Selenium dependencies:
https://jar-download.com/artifacts/org.seleniumhq.selenium/selenium-chrome-driver/3.12.0/source-code

How to add jar files and compile using command prompt? (Error: A JNI error has occured,Please Check your Installation)

I forked one project from github and when i want to run it gives error. I added org.json jar file to jdk\lib and also set the Path variable to it. I compiled code but it got no errors but when i run it i get exception as follows : (and I am running it using command prompt)
Error: A JNI error has occurred, please check your installation and
try again Exception in thread "main" java.lang.NoClassDefFoundError:
org/json/JSONExceptio n
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException:
org.json.JSONException
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)
... 7 more
Add required jar to classpath like this:
For *nix-like systems:
export CLASSPATH=<path-to-app>/myapp.jar:<path-to-app>/lib/json.jar:.
java MyApp
For Windows:
set CLASSPATH=<path-to-app>/myapp.jar;<path-to-app>/lib/json.jar;.
java MyApp
...or via manifest:
Manifest-Version: 1.0
Main-Class: MyApp
Class-Path: /lib/json.jar

Trying to run a POI-dependent class on Command Prompt

As the title says, I'm trying to run a class in Command Prompt which uses the Apache POI library but when I run this command:
java -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs; HyperlinkWriter
I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/CreationHelper
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.CreationHelper
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 6 more
the libs folder contains the entire POI folder that's zipped up when downloaded so I'm not sure why it's throwing this error. I also tested this with IntelliJ and it ran fine. I know that the way Command Prompt and IntelliJ handles libraries is different but I'm not sure why this is happening.
I would appreciate any help! Thanks!
You need to actually compile the jar with the original .java files and the specified POI jars.
javac -cp \path_to_jars \path_to_java_files
and then
java MyProject
I found the answer from a previous answer of StackOverflow. I had to add a '*' to the end, and a '.' after the semicolon. So with my example, in terms of compiling, I had to do:
javac -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;. C:\Users\rperera\IdeaProjects\LinkingNames\src\HyperlinkWriter.java
and then to run it (Credit to #andrewdleach), I just did:
java HyperlinkWriter

Running class from JAR with multiple JAR dependencies via command prompt

I cannot understand why none of these are working. I've seen similar situations on stackoverflow and their solutions haven't worked for me.
I have a lib directory containing 15 JARs. When I run this command in command prompt from within that lib directory:
java -cp openjpa-2.2.1.jar org.apache.openjpa.enhance.PCEnhancer
I get a NoClassDefFoundError for one of it's dependencies.
Exception in thread "main" java.lang.NoClassDefFoundError: serp/bytecode/Instruction
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: serp.bytecode.Instruction
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 7 more
Now when I try to add the jar containing that dependency:
java -cp openjpa-2.2.1.jar:serp-1.13.1.jar org.apache.openjpa.enhance.PCEnhancer
I get this:
Error: Could not find or load main class org.apache.openjpa.enhance.PCEnhancer
I've also tried:
java -cp openjpa-2.2.1.jar:serp-1.13.1.jar:. org.apache.openjpa.enhance.PCEnhancer
java -cp . org.apache.openjpa.enhance.PCEnhancer
With the same effect, and when I try:
java -cp serp-1.13.1.jar -jar openjpa-2.2.1.jar org.apache.openjpa.enhance.PCEnhancer [path to .java file]
All I get is:
>OpenJPA 2.2.1
>version id: openjpa-2.2.1-r422266:1396819
>Apache svn revision: 422266:1396819
>os.name: Windows 7
>os.version: 6.1
>os.arch: amd64
>java.version: 1.8.0_25
>java.vendor: Oracle Corporation
>java.class.path:
> openjpa-2.2.1.jar
With no file output, even when I add the -d flag with a directory.
Where am I going wrong?
RealSkeptic's comment was correct. Semicolons are the proper syntax to separate JARs listed on the classpath in Windows, like so:
java -cp jar1;jar2;jar3 classToRun args

Gate setup: official TestGate.java cannot run

new to Gate, actually also not old to Java and Eclipse ;) and want to run the first test of setup.
what I have done:
download gate-7.0-build4195-ALL and unzip it to C:\
change enviroment variable CLASSPATH to ".;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;C:\gate-7.0-build4195-ALL\bin\gate.jar;C:\gate-7.0-build4195-ALL\lib"
created a new java project in eclipse, and navigate to gate.jar\gate\TestGate.class and run it.
then I get the following error:
Failed to invoke suite(): java.lang.NoClassDefFoundError: org/apache/log4j/Logger
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.getTest(JUnit3TestLoader.java:108)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.loadTests(JUnit3TestLoader.java:59)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at gate.Gate.<clinit>(Gate.java:83)
at gate.TestGate.suite(TestGate.java:126)
... 10 more
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 12 more
did I miss something?
thank you
You shouldn't set your CLASSPATH environment variable. If you want to use JAR files in an Eclipse project you should add them to your project's build path.
For GATE specifically, you need bin/gate.jar and all the JAR files from lib, but you don't need the lib directory itself on your build path.
I am using GATE API in java with netbeans and when I have integrated a test with junit I have had that error. The trick is to build the project without doint the test, once time built you can run the test as normally using GATE.
For example i have got this at the beginning in my test func:
#Test
public void test()
{
if(false)
{
assertEquals(true, true);
return;
}
...
Testing GATE
To build the jar without test, you only need to set to true overthere. It seems something about classpath of the jar used by GATE.

Categories