running selenium tests from command line junit [duplicate] - java

This question already has answers here:
How to run a java class with a jar in the classpath?
(4 answers)
Closed 7 years ago.
I am unable to execute selenium tests(JUNIT) from command line
my project folder path class file
C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash
File :LoginTest.class
my project folder path java file
C:\Users\CP042756\workspace\BLR_demo1\src\com\analytics\logindash
File:LoginTest.java
jar file folder: C:\jars\imp\selenium-2.45.0\libs
jar fiLe: junit-dep-4.11.jar
it runs properly in Eclipse
i want to run it in command line
i have tried the following commands from the command line
1)
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar:C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash org.junit.runner.JUnitCore LoginTest
Error:Could not find or load main class
2)java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar org.junit.runner.JUnitCore LoginTest
Error:Could not find class:Login test
Exception in thread main java.lang.noclassdefounderror
Please help,

You have to use semicolon as path separator in Windows. Then your first example should work.
For class files there are two different rules. check, which one applies to your situation:
For .class files in an unnamed package, the class path ends with the
directory that contains the .class files. For .class files in a named
package, the class path ends with the directory that contains the
"root" package (the first package in the full package name).
So, for the latter try this:
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar;C:\Users\CP042756\workspace\BLR_demo1\bin org.junit.runner.JUnitCore com.analytics.logindash.LoginTest

Related

run .jar file which has dependency with another .jar file in command line

I have a .jar file(insertdata.jar) which has a dependency with postgresql-42.2.23.jar.I have included main class in the Manifest file also. Currently both of these files are located inside C:\Users\yush\Desktop\java directory. I have tried to run insertdata1.jar file with following command in the command line.
java -jar C:\Users\yush\Desktop\java\insertdata1.jar
with this command I'm getting following error, which seems it is unable to access postgresql-42.2.23.jar classes.
java.lang.ClassNotFoundException: org.postgresql.Driver
My question is how do I run insertdata1.jar file in the command line with postgresql-42.2.23.jar?
2 options for you
change your invocation
java -cp <class-path-of-all-required-jars> <Main.class>
build uber/fat jar - combines all individual jars. then you can so your -jar thing

compile and run java with jar files [duplicate]

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 1 year ago.
I'm trying to compile a java file that uses multiple jar files as imports.
the command that I have used to compile my code :
javac -cp jackson-databind-2.12.1.jar:jackson-core-2.12.1.jar:jackson-annotations-2.12.1.jar TestRunner.java
as a result two .class files are created : TestRunner.class and TestRunner$1.class
then I run the command :
java TestRunner
but it throws an error that says:
Error: Unable to initialize main class TestRunner Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
I have included all the required libraries in the javac command, tested it with the IDE and it works fine.
I have tried other versions of the jackson library but I'm stuck with the same error.
You need to specify the classpath when running your code, by using the same -cp args that you used when compiling, plus the folder where your compiled class is in.
In your cas that would mean something like java -cp .:jackson-databind-2.12.1.jar:jackson-core-2.12.1.jar:jackson-annotations-2.12.1.jar TestRunner
The libs you specified are not included in the .class files generated, so Java still needs them to understand how to call the code that's not coming from your class file.

Executing a class in jar file

I have created an application that uses selenium-server-standalone-2.47.1.jar and javax.mail.jar. The code works on eclipse, but I would like to run the same from command line. So I exported the project to a runnable jar file which contains both selenium and javax.mail.jar.
My code contains RTC.java which has the Main function and another Ex.java.
Both the class files are generated in com folder.
My App1.jar file is located in C:\installers.
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError: org.openqa.selenium.WebDriver
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
What am I doing wrong?
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError:
org.openqa.selenium.WebDriver
That exception usually means that a .class file was found but it didn't contain the right class. Check how you're putting it into the JAR. It's directory and file name must match its package and class name.
It sometimes also seems to mean that a secondary class wasn't found. Normally secondary JAR files are mentioned in the class-path entry of the manifest of the main JAR file, along with the main-classname, so you can use
java -jar App1.jar
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
I'm not surprised. If the first command got as far as it did, the JAR filename doesn't start with a dot. Cannot imagine why you tried this. It is nonsense.
I found the solution. I need not create a jar file which has the reference jars. I have to mention my jar which has my code and the reference jars that I am using in the class path.
My code is in App.jar.The reference jars are
selenium-server-standalone-2.47.1.jar and
javax.mail.jar.
so I used
c:\installers> java -cp selenium-server-standalone-2.47.1.jar;javax.mail.jar;App.jar com.RTC
Thank You.

java run class using jawin

I'm trying to run a class that I wrote that is importing the jawin library.
My command line is as below:
java -cp C:\class_path ClassName
I'm getting:
Exception in thread 'main' java.lang.NoClassDefFoundError org/jawin/funcptr
What am I doing wrong?
Doesn't the JAR that I'm adding to the build path get into the binary file that I've compiled?
If not, then when I publish my class file how will it know to find the external JAR? What if it doesn't exist on the target computer?

automatically compiling multiple java packages [duplicate]

This question already has answers here:
javac option to compile all java files under a given directory recursively
(10 answers)
Closed 9 years ago.
I'm trying to compile a Java project from the command line. The project contains class files in different packages. The program compiles and run fine if I specify every java file of every package. Here's my directory structure:
toplevel/
mainFile.java
level1/ (Contains fileA.java)
Now if I do javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java, this compiles fine and I can run it with java toplevel/mainFile.
Now if a create a new folder called 'level2' in level1 and create a class 'B' inside it, the new directory structure becomes:
toplevel/
mainFile.java
level1/ (Contains fileA.java)
level2/ (Contains fileB.java)
To compile this I have to do:
javac -classpath ./toplevel toplevel/mainFile.java toplevel/level1/fileA.java toplevel/level1/level2/fileB.java which is becoming ridiculous. Is there an instruction that recursively compiles each package and the files inside it?
Have you tried -sourcepath flag. From the Oracle documentation,
-sourcepath sourcepath
Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.

Categories