TestNG command line not working (java org.testng.TestNG ) - java

i'm trying to run testng tests using command line like this :
C:\Documents and Settings\Administrateur\Bureau\automatic tests testNG>java -cp
C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\
testng-6.3.1.jar org.testng.TestNG testng.xml
but i'm getting this error :
Exception in thread "main" java.lang.NoClassDefFoundError: and
Caused by: java.lang.ClassNotFoundException: and
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)
Could not find the main class: and. Program will exit.
So How to add testNG to the classpath.
Thanks

The spaces in the filename are confusing things - Java thinks that only the c:\Documents part is in the classpath argument. Try this:
java -cp
"C:\Documents and Settings"\Administrateur\.m2\repository\org\
testng\testng\6.3.1\testng-6.3.1.jar org.testng.TestNG testng.xml
(where the whole thing is on one line and there's no break after "org" of course)

If you want to run via java on command line, you'll need to either enquote your path ("C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\") or alter slightly as follows: C:\Documents%20and%20Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\. You may also want to run via maven to avoid having to specify this classpath info.

Related

Run a batch file in Python which contains java command

It is quite frustrating when you can't execute a .bat file in python while it is executing manually.
I am attaching my code here:
directory = 'E:/'
with open(os.path.join(directory, 'output_file.bat'), 'w') as OPATH:
OPATH.writelines(['"""',"\n"'E:',"\n",
'javacCreatingUser.java',"\n",'javaCreatingUser',"\n",'"""'])
os.system("E:/output_file.bat")
The above is my python code which is creating a bat file with 2 java command
javac CreatingUser.java
java CreatingUser
I can run the .bat file manually and it is working fine but my python script is giving me the following error:
java.lang.NoClassDefFoundError: oracle/iam/identity/exception/ValidationFailedException
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: oracle.iam.identity.exception.ValidationFailedException
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
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" '"""' is not recognized as an internal or external command,
operable program or batch file.
The question is that if some classes are missing in my file then it should not execute manually as well, but manual execution is fine.
javac CreatingUser.java
java CreatingUser
This is the source of your problem: You're only compiling one class file and then calling that class without a classpath.
Java has a search path for classes comparable to Pythons PYTHONPATH called classpath.
You can give this a try:
javac CreatingUser.java
java -classpath YOUR_CLASSPATH CreatingUser
YOUR_CLASSPATH can be a colon (Linux) or semicolon (Windows) separated list of JAR files and directories containing class files. Let your classpath point to the JARs you're needing and you're fine.

Java gradle execute code

I follow this starter tutorial.
I can not run the code with this command :
$ java -cp gs-gradle-0.1.0.jar hello.HelloWorld
I have this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/LocalTime
at hello.HelloWorld.main(HelloWorld.java:11)
Caused by: java.lang.ClassNotFoundException: org.joda.time.LocalTime
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
All import classes are not found. But with gradle command it works :
$ ./gradlew run
Why with java command, it doesn't work ?
Thanks
Of course it does not work, the Tutorial also told you so.
The application plugin adds the run task (and other tasks) that put your dependency on the classpath.
If you use the distZip or distTar task you get an archive with all your dependencies and with start scripts that set the correct classpath that you can use to run your application.
Read more about the application plugin at https://docs.gradle.org/current/userguide/application_plugin.html.

NoClassDefFoundError, in the same directory why the program can't find the jar

I write a Java program that read text from excel.So I import some jar like this: poi-3.10-beta2-20130904.jar, poi-ooxml-3.10-beta2-20130904.jar, etc., I can run the program correctly in eclipse.But when I package this program with maven to the directory(C:\workspace2\change\bin),
Then I run this program in command like this :
C:\workspace2\change\bin>java GenerateVar
it occurs this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell
at GenerateVar.execute(GenerateVar.java:59)
at GenerateVar.main(GenerateVar.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Cell
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)
... 2 more
You need to specify classpath with all other jars/classes you use in your program.
E.g. Setting multiple jars in java classpath
When you run it from eclipse in the console there's a command that could be used to run the project, you could copy/paste it from the console to your command line.
In the command java GenerateVar you didn't set -cp option. With this option the command line will look like
java -cp poi-3.10-beta2-20130904.jar poi-ooxml-3.10-beta2-20130904.jar ... GenerateVar
Assumed the libraries are in the current dir.
How to use maven plugins to add dependency to your project and build jar you can find here.
See also this answer if you want to modify manifest.mf manually.
over, I change the way do what I want to do. I use the fat jar which is the eclipse plugin to package the program

Main class not found exception in jar

I am trying to execute the performance-meters.jar according to the tutorial at
http://marklogic.github.io/performance-meters/tutorial.html
using the command
java -cp performance-meters.jar:xcc.jar com.marklogic.performance.PerformanceMeters
I am getting the following error:
D:\MBS\performance-meters-master\performance-meters-master\classes>java -cp performance-meters.jar:xcc.jar com.marklogic.performance.PerformanceMeters
Exception in thread "main" java.lang.NoClassDefFoundError: com/marklogic/performance/PerformanceMeters
Caused by: java.lang.ClassNotFoundException: com.marklogic.performance.PerformanceMeters
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)
Could not find the main class: com.marklogic.performance.PerformanceMeters. Program will exit.
I have kept both the jar files in the same folder.
Links to the jar files are:
http://developer.marklogic.com/code/performance-meters
[http://developer.marklogic.com/products/xcc][3]
Any help would be appreciated.
try to use semicolon instead colon in your cp declaration.
java -cp performance-meters.jar;xcc.jar com.marklogic.performance.PerformanceMeters
You are using windows system or Unix? If windows then I think you need to use ; and not : in separating jars as follows:
java -cp performance-meters.jar ; xcc.jar com.marklogic.performance.PerformanceMeters

Why does my IDE find the JAR but my command line doesn't? [duplicate]

This question already has an answer here:
How come command line isn't recognizing this java command?
(1 answer)
Closed 10 years ago.
I've tried searching around, but I couldn't find any answers that fit my case.
I can run the file CB.java just fine when I use an IDE. This file depends on classes specified in cs2.jar. Here are the contents of its directory.
02/12/2013 03:43 PM <DIR> .
02/12/2013 03:43 PM <DIR> ..
02/12/2013 03:45 PM 2,226 CB.class
02/12/2013 01:21 PM 2,164 CB.java
02/12/2013 03:43 PM 71,128 cs2.jar
3 File(s) 75,518 bytes
2 Dir(s) 408,977,362,944 bytes free
When I run it off my IDE, CB.java works just fine. However, when I try java CB in the command line, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: sn/visual/JRect
angle
Caused by: java.lang.ClassNotFoundException: sn.visual.JRectangle
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)
Could not find the main class: CB. Program will exit.
Furthermore, I tried following suggestions to add something to the classpath using:
>java -cp C:\Users\...blah blah blah...\Software_Engineering cs2
Exception in thread "main" java.lang.NoClassDefFoundError: cs2
Caused by: java.lang.ClassNotFoundException: cs2
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)
Could not find the main class: cs2. Program will exit.
How come my IDE is smart but my command line isn't?
Thank you.
The class path is set to just consider .class files in the given directory. You need to add the jar file to the class path: java -cp C:\somewhere\cs2.jar
How come my IDE is smart but my command line isn't?
I suspect in your IDE, you've included the jar file in your build path, so it then includes it when both building and running. (You haven't told us which IDE it is, so it's hard to use the exact terminology it would use)
On the command line, you need to specify the jar file when both building and running too, so you'd use:
To build:
javac -cp cs2.jar CB
To run:
java -cp .;cs2.jar CB

Categories

Resources