I give the absolute path but java cannot a class - junit

I cannot understand why this
ioannis#ioannis-GA-MA74GM-S2H:~$ java -cp /opt/junit4.6/junit-4.6.jar org.junit.runner.JUnitCore /opt/CalculatorTest
gives the error:
Could not find class: /opt/CalculatorTest
and this work fine:
ioannis#ioannis-GA-MA74GM-S2H:~$ java -cp /opt/junit4.6/junit-4.6.jar:/opt org.junit.runner.JUnitCore CalculatorTest
JUnit version 4.6
.
Time: 0.005
OK (1 test)
in the first I give the full path for class /opt/CalculatorTest

The classname is intended to be just the class name, not a path. If it is in a package, you can have foo.Bar. If it is in the default package, you can only have Bar.
As you noticed, the classpath is the place to identify the physical directories Java should look in to find the class.

Related

How do I get log4J to work - I'm getting "package org.apache.log4j does not exist"

i know this may be a newbie qestion, but I'm having issues with setting up Log4J:
I want to run a log4j demo, and here's my code:
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class HelloLOG4j {
private static final Logger logger = Logger.getLogger(Hello.class);
public static void main(String argv[]) {
BasicConfigurator.configure();
logger.debug("Hello world.");
logger.info("What a beatiful day.");
}
}
I set my Classpath:
C:\Users\Adel\Downloads\apache-log4j-1.2.17\log4j-1.2.17.jar
in both System and User var's
But when I run my program I still get
errors found:
File: C:\Users\Adel\Desktop\various_topics\JavaProjects\HelloLOG4j.java [line: 2]
Error: package org.apache.log4j does not exist
I know that I set classpath right - if I run cmd line:
C:\Program Files\Java\jdk1.6.0_20>print %LOG4J_HOME%
C:\Users\Adel\Downloads\apache-log4j-1.2.17\log4j-1.2.17.jar is currently bein
g printed
You need to add log4j home to the classpath as the JVM needs the path to the log4j classes
if on windows, you can use
set classpath=%classpath%;%LOG4J_HOME%
On linux/ ubuntu (much better than windows for development & servers)
export classpath=$classpath:$LOG4J_HOME
then run your app after adding other paths to classpath
like
set classpath=%classpath%;c:\users\adel\....
You do not need to add log4JHOME again - as %classpath%; will add to the current classpath.
LOG4J_HOME is not known to Java. It is just used by log4j in case of auto config/default config.
On a side note try using the new log4j2 !
Can you show how you are trying to compile the code?
And also, try adding the log4j.jar to 'lib' directory and compile with the classpath referencing this jar
Just want to remind that don't capitalized Log4j keyword , unlike Logger:
import org.apache.Log4j.Logger; //typo
import org.apache.log4j.Logger; //correct
/usr/share/java/log4j-1.2-api-2.8.2.jar path can be located by issue dpkg -L liblog4j2-java(debian-based) command, then do:
$ sudo javac -cp .:xxx.jar:/usr/share/java/log4j-1.2-api-2.8.2.jar xxx.java

Unable to run junit test from command prompt

I'm trying to run a JUnit test case from command lineThe 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:
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:
put the junit.jar library in your classpath and;
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.
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

Error while running JUnit test from command line

I am trying to run a single JUnit test from the command line but I am getting an error.
I could compile the JUnit test successfully and the class file gets created in the correct location.
But when I try to run it using:
C:\Program Files\Java\jdk1.7.0_01\bin>java org.junit.runner.JUnitCore C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\com\org\tests\Nav.class
I get the error:
JUnit version 4.8.1
Could not find class: C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\com\org\tests\Nav.class
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDesc
ribbing
I don’t know why it is not able to find the class even though it exists in the said location.
You need to specify the name of the class on the command line, not the filename:
java org.junit.runner.JUnitCore com.org.tests.Nav
From the javadoc for JUnitCore:
JUnitCore is a facade for running tests. It supports running JUnit 4
tests, JUnit 3.8.x tests, and mixtures. To run tests from the command
line, run java org.junit.runner.JUnitCore TestClass1 TestClass2 ....
For one-shot test runs, use the static method runClasses(Class[]). If
you want to add special listeners, create an instance of
org.junit.runner.JUnitCore first and use it to run the tests.
and you will have to add the bin directory (note NOT the src) to the classpath of the command line as well. This may look like:
java -cp C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\bin org.junit.runner.JUnitCore com.org.tests.Nav

Running JUnit from command prompt: classpath confusion

I'm having trouble getting from a working JUnit command line invocation to something only slightly more complicated. At the outset, 'hw' is a class with no package specified. The following command is successfully executed from the dir in which hw.class lives:
java -cp /usr/share/java/hamcrest-core.jar:/usr/lib/eclipse/plugins/org.junit4_4.5.0.v20090824/junit.jar:./ org.junit.runner.JUnitCore hw
I now specify a package 'p' for hw, move hw.java into subdir 'p', and recompile. How can I modify the above command to have the class successfully tested? I've have thought that
java -cp /usr/share/java/hamcrest-core.jar:/usr/lib/eclipse/plugins/org.junit4_4.5.0.v20090824/junit.jar:p org.junit.runner.JUnitCore p/hw
would work from the 'superdir' of p, but it doesn't...
There are two problems with your command:
The classpath must include the directory that is at the root of the package hierarchy, i.e. not p but the directory containing p.
The argument to JUnitCore is the fully qualified name of the class, i.e. p.hw not p/hw
The classes are missed at the time of execution of test in classpath of the project
s
Solution : add in your pom
commons-discovery
commons-discovery
0.5
test

How to register a JDBC driver using jruby-complete.jar?

I'm trying to write a script that is executed with the jruby-complete.jar like so:
java -cp derby.jar; -Djdbc.drivers=org.apache.derby.jdbc.EmbeddedDriver -jar jruby-complete.jar -S my_script.rb
I'm using JVM 1.6.0_11 and JRuby 1.4.
In my jruby script I attempt to connect to the database like this.
connection = Java::com.sql.DriverManager.getConnection("jdbc:derby:path_to_my_DB")
This throws a java.sql.SQLException: "No suitable driver found" exception.
I've tried manually loading the driver into the class loader using Class.forName which gives me the same error.
It looks like to me that the class loader being used by the DriverManager is not the same as the current thread's. I've tried setting the current thread's class loader using:
JThread = java.lang.Thread
...
class_loader = JavaLang::URLClassLoader.new(
[JavaLang::URL.new("jar:file:/derby.jar!/")].to_java(
JavaLang::URL),JRuby.runtime.jruby_class_loader)
JThread.currentThread().setContextClassLoader(class_loader )
But this doesn't help.
Any ideas?
OK I downloaded jruby-complete.jar and had a go....
This seems to work for me:
java -classpath c:\ruby\db-derby-10.5.3.0-bin\lib\derby.jar;jruby-complete-1.4.0.jar org.jruby.Main -S derby.rb
When using the -jar switch, the -classpath option is ignored (maybe the CLASSPATH shell var is too). But on the above line, we put both required jars on the class path and pass the class name to execute (i.e. org.jruby.Main). The script being passed in is as per my other answer.
Another option (which I have not tried) would be to alter the jruby-complete.jar manifest file to specify as classpath, as described here:
Adding Classes to the JAR File's Classpath
First, make sure your driver jar is not corrupted (this made me waste a couple of days one time).
Second, read this about JRuby/Java classloade: JRuby Wiki
Third (because I haven't played with "jruby-complete") try this simple script and then see if you can adapt as you need.
require 'java'
require 'C:\ruby\db-derby-10.5.3.0-bin\lib\derby.jar' # adjust for your machine
include_class "java.sql.DriverManager"
derby = org.apache.derby.jdbc.EmbeddedDriver.new
connection = DriverManager.getConnection("jdbc:derby:derbyDB;create=true")