• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error with execution using packages in command line

 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Bye.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

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

Hug!
 
Sérgio Lopes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can use the shortcut option -cp instead of -classpath
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah!
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.

Hug.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic