0

I'm trying to run a JUnit test case from command line
The code I followed is set to bin dir

c:/eclipse/workspace/sample/bin> java -cp C:\Ram Doc\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705.junit.jar C:\Ram Doc\eclipse\workspace\Script_Bvt\bin org.junit.runner.JUnitCore login_sanity(That's my class Name)

Error message is

C:\Ram Doc\eclipse\workspace\Script_Bvt\bin>java -cp C:\Ram Doc\eclipse\plugins\
org.junit_4.8.2.v4_8_2_v20110321-1705.junit.jar java org.junit.runner.JUnitCore
login_sanity
Exception in thread "main" java.lang.NoClassDefFoundError: Doc\eclipse\plugins\o
rg/junit_4/8/2/v4_8_2_v20110321-1705/junit/jar
Caused by: java.lang.ClassNotFoundException: Doc\eclipse\plugins\org.junit_4.8.2
.v4_8_2_v20110321-1705.junit.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Doc\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110
321-1705.junit.jar.  Program will exit.

If i keep the login_sanity at other location and execute:

C:\Selenium>javac -cp "C:\Selenium\junit4.10\junit4.10\junit-4.10.jar;." org.junit.runner.JUnitCore login_sanity

I get the following error:

Class names, 'org.junit.runner.JUnitCore,login_sanity', are only accepted if 
annotation processing is explicitly requested

The following shows my complete steps: enter image description here

pestrella
  • 9,786
  • 4
  • 39
  • 44
Riy1234
  • 39
  • 1
  • 8
  • The last screenshot suggests that you have not compiled the login_sanity class. This is due to the error you get when you compiled (i.e. executed javac). Look at my later comments where I explain why it failed to compile -- it's because you don't have the .java suffix. Once the class is compiled you will notice a login_sanity.class file in your current directory. Now you should be able to run JUnitCore with login_sanity as the argument. – pestrella Feb 14 '13 at 11:35
  • If i run this i get 25 errors : javac -cp "C:\Selenium\junit4.10\junit4.10\junit-4.10.jar;." login_sanity.java I don't feel I have to include the location even though I tried that > javac -cp "C:Selenium\junit4.10\junit4.10\junit-4.10.jar;C:Selenium\;." login_sanity.java and even without the back slash but i guess it needs"\", error message is "symbol", I have tried almost all the possible combinations – Riy1234 Feb 14 '13 at 14:04
  • If you have errors when compiling the java file then you have problems with the actual code in login_sanity. I'm afraid I can't give any further assistance unless I see the code, but I think StackOverflow is not really the place for this type of discussion. I suggest asking a colleague to take you through it. Good luck! :) – pestrella Feb 14 '13 at 14:13
  • Thank You so much. I am thankful of you for being so kind and for a lengthy conversation :) It's better the way i used to run with ant. I can't vote up :( any ways there you go for this one – Riy1234 Feb 14 '13 at 14:19

2 Answers2

1

In general running a Java class via command line is done like this (on Windows, which it appears you are using):

java -cp "jar1;jar2;dir\*" my.app.package.MainClass my_arguments

From what I can tell, you are trying to execute your test class, login_sanity, via JUnitCore. In other words you're executing the JUnitCore class with your test class as the argument; and you require the junit.jar library in order to run the JUnitCore class.

In order run this command, you would need to:

  1. put the junit.jar library in your classpath and;
  2. specify the main class you want to execute, JUnitCore, along with the arguments you want to pass to the main class, i.e. login_sanity

So it's like this:

java -cp "C:\path\to\junit.jar;C:\path\to\bin\*" org.junit.runner.JUnitCore login_sanity

The above command assumes your class login_sanity is in the default package, i.e. no package, and in the bin directory.

If your class is not in the default package, i.e. you have declared a package inside your login_sanity class, then you would need to use its fully qualified name in the command line. Here's an example --

Say your class is in the following package: my.app.login. In other words the first few lines of your java class is:

package my.app.services;

public class login_sanity {
  /* your tests go here */
}

In this case you would execute JUnitCore like so:

java -cp "C:\path\to\junit.jar;C:\path\to\bin\*" org.junit.runner.JUnitCore my.app.services.login_sanity

As an aside, typical Java convention is to name your classes in camel case, i.e. LoginSanity.

pestrella
  • 9,786
  • 4
  • 39
  • 44
  • java -cp "C:\Selenium\junit4.10\junit4.10\junit-4.10.jar;C:\Ram Doc\ eclipse\workspace\Script_Bvt\bin*" org.junit.runner.JUnitCore login_sanity === JUnit version 4.10 could not find class: login_sanity – Riy1234 Feb 13 '13 at 06:52
  • I tried with all the class where even the main method is declared. It shows the above "could not find the class" Now what should I do ? – Riy1234 Feb 13 '13 at 06:55
  • The main method you are trying to run is in JUnitCore, which takes class names as its arguments. The class name you are trying to pass to JUnitCore is login_sanity. The error "Count not find class: login_sanity" indicates that JUnitCore could not locate the login_sanity class. Two things to check: 1. that login_sanity.java is in your classpath, i.e. "C:\Ram Doc\ eclipse\workspace\Script_Bvt\bin\"; and 2. you must have the trailing backslash in your path, so change what you have to "C:\Ram Doc\ eclipse\workspace\Script_Bvt\bin\*" – pestrella Feb 13 '13 at 10:07
  • login_sanity Class is in my classpath,that was a typo error i do have "\"after bin. I am going mad on this :( – Riy1234 Feb 13 '13 at 11:38
  • Check that your login_sanity class is not in a package. If it is, then you need to specify the package in the command line. I've updated my answer to include such example. – pestrella Feb 13 '13 at 12:13
  • I went through this link(http://users.ece.utexas.edu/~miryung/teaching/EE461L-Spring2012/labs/testing.html)I really thank you a lot for being so patient to reply. I have java in my class path javac and java command works well. The funny part now is if i compile javac login_sanity.java I get error stating "Cannot find symbol" – Riy1234 Feb 13 '13 at 12:30
  • If i copy the login_sanity @ some other location say c:Selenium. then I get some other error i have updated above – Riy1234 Feb 13 '13 at 13:53
  • You are getting the compile error because you have specified the classes you want to compile without the .java suffix. And also, you don't need to compile JUnitCore as it's already compiled inside the junit jar. New to Java huh? ;) Try this: javac -cp "C:\Selenium\junit4.10\junit4.10\junit-4.10.jar;." login_sanity.java – pestrella Feb 13 '13 at 14:11
  • Yes I am new to Java. I am pretty bad at typing things, so i have a link which shows the error (http://www.4shared.com/photo/b-KML8qx/run_junit_4m_cmd1.html) – Riy1234 Feb 14 '13 at 04:35
  • That's cool, happy to help. In order to avoid extended conversation via comments, I'm going to update your question with your last screen grab and then update my answer. Hopefully it's helpful to you and others who are also starting out in Java. One moment... – pestrella Feb 14 '13 at 10:48
0

Add the junit jar to the classpath
Add the tested classes to the classpath
Run junit.textui.TestRunner testclass

java -classpath .;c:\path\name.jar;c:\path\to\bin org.junit.runner.JUnitCore nameofclass