wood burning stoves*
The moose likes Beginning Java and the fly likes Error with execution using packages in command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error with execution using packages in command line" Watch "Error with execution using packages in command line" New topic

Error with execution using packages in command line

Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Hi folks.
I wrote something using JUnit and Eclipse and that works just fine. But now I want to execute my java program using JUnit through a command line. How can I do that without specifying JUnit in my Classpath file?

Like, I'm having some troubles... When I wrote this

I'm gettin this:
Exception in thread "main" java.lang.NoClassDefFoundError: junit/framework/TestCase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

I guess that is something wrong with JUnit (I'm using 4.5), but that should went wrong in Eclipse, right? And it didn't, in Eclipse it worked fine.

How can I include some jar package when I'm going to execute my Java application?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Anything you have to specify on the classpath for javac, you need to specify for java, too. Here your classpath should include that jar file along with the current directory where your own classes are located; so, for example:

java -classpath .;junit-4.jar App1

That "." (dot) stands for the current directory; it's a lot handier than typing out the path! That is a Windows example; If you're on a Mac or UNIX machine, use

java -classpath .:junit-4.jar App1

[Jess in Action][AskingGoodQuestions]
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95


Thanks! I can't believe is was just that ".:" that I was missing. Thanks a lot.

Sérgio Lopes

Joined: Jan 25, 2009
Posts: 7
And you can use the shortcut option -cp instead of -classpath

<a href="http://www.caelum.com.br" rel="nofollow">Caelum Curso Java</a>
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Even if I'm using IDE, there's one thing that I must learn correctly is to use Java compiler in the right and useful way.

I agree. Here's the link: http://aspose.com/file-tools
subject: Error with execution using packages in command line