JNI error occurrence with TestNG classpath setup - java

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

Related

Java File runs in eclipse but on command line

I have created a jar file in eclipse using maven and am able to run a class from the jar file with eclipse. However when I run the following command from the command prompt:
c:\recomendation_engine\recomendation\target>java -classpath recomendation-0.0.1
-SNAPSHOT.jar recomendation.ManageGeneralContent test
I get the following error:
Organization Environment test
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at recomendation.ManageGeneralContent.main(ManageGeneralContent.java:52)
I believe that the issue is that the class can not find the mysql driver. However it is in the classpath:
c:\Program Files\Java\jdk1.7.0_60\lib\*;C:\Users\User\.m2\repository\com\google\
guava\guava\18.0-rc1\guava-18.0-rc1.jar;C:\Users\User\.m2\repository\org\slf4j\s
lf4j-api\1.7.7\slf4j-api-1.7.7.jar;C:\Users\User\.m2\repository\com\jolbox\bonec
p\0.8.0.RELEASE\bonecp-0.8.0.RELEASE.jar;C:\Users\User\.m2\repository\postgresql
\postgresql\9.1-901-1.jdbc4\postgresql-9.1-901-1.jdbc4.jar;c:\recomendation_engi
ne\recomendation\lib\mysql-connector-java-5.1.6.jar;C:\recomendation_engine\reco
mendation\target\recomendation-0.0.1-SNAPSHOT.jar;
Can you please tell me what I am doing wrong?
You are overriding the CLASSPATH environment variable when you specify the "-classpath" switch on the java command line.
Try running:
c:\recomendation_engine\recomendation\target>java
recomendation.ManageGeneralContent test
Check out: http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html

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

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

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.

Categories