This question already has an answer here:
I've tried searching around, but I couldn't find any answers that fit my case.
I can run the file CB.java just fine when I use an IDE. This file depends on classes specified in cs2.jar. Here are the contents of its directory.
02/12/2013 03:43 PM <DIR> .
02/12/2013 03:43 PM <DIR> ..
02/12/2013 03:45 PM 2,226 CB.class
02/12/2013 01:21 PM 2,164 CB.java
02/12/2013 03:43 PM 71,128 cs2.jar
3 File(s) 75,518 bytes
2 Dir(s) 408,977,362,944 bytes free
When I run it off my IDE, CB.java works just fine. However, when I try java CB
in the command line, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: sn/visual/JRect
angle
Caused by: java.lang.ClassNotFoundException: sn.visual.JRectangle
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: CB. Program will exit.
Furthermore, I tried following suggestions to add something to the classpath using:
>java -cp C:\Users\...blah blah blah...\Software_Engineering cs2
Exception in thread "main" java.lang.NoClassDefFoundError: cs2
Caused by: java.lang.ClassNotFoundException: cs2
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: cs2. Program will exit.
How come my IDE is smart but my command line isn't?
Thank you.
The class path is set to just consider
.class
files in the given directory. You need to add the jar file to the class path:java -cp C:\somewhere\cs2.jar
I suspect in your IDE, you've included the jar file in your build path, so it then includes it when both building and running. (You haven't told us which IDE it is, so it's hard to use the exact terminology it would use)
On the command line, you need to specify the jar file when both building and running too, so you'd use:
To build:
To run: