wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes CLasspath Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "CLasspath Problem" Watch "CLasspath Problem" New topic
Author

CLasspath Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i am confused about how to use -classpath with
java
javac


if i have a file B.java in C:\Program Files\Java\jdk1.6.0_13\bin\foo\test\xcom directory
and i compile and run using
javac B.java
java B
it complies fine,but fails to run
Exception in thread "main" java.lang.NoClassDefFoundError: B
(wrong name: xcom/B
)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: B. Program will exit.
default classpath is C:\Program Files\Java\jdk1.6.0_13\bin
how to run it.

B.java contains :



------------------------------------
on the other hand
C:\Program Files\Java\jdk1.6.0_13\bin\x\FindBaz.java

It contains the following code


i complied and run it using:
javac FindBaz.java
java FindBaz


it compiled and run fine.



how did this run and why was there error in previous file


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

B class is in a package, the fully qualified name of B class is xcom.B. java command requires you to give the fully qualified name of the class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5


i complied and run it using:
javac FindBaz.java
java FindBaz

it compiled and run fine


FindBaz does not belong to a package ? You need fully qualified names to run classes that exist in a package


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


This tells you that class B is in the xcom package. This also tells you that the root of your sources is the parent directory of xcom : C:\Program Files\Java\jdk1.6.0_13\bin\foo\test\ (by the way, what are you doing in the JDK's bin directory ?? You should keep your sources somewhere else)

To compile B.java, the compiler must find the source file. You have to tell it where the source is. There are different ways.
1. If you are in C:\Program Files\Java\jdk1.6.0_13\bin\foo\test\xcom : javac B.java
2. If you are in C:\Program Files\Java\jdk1.6.0_13\bin\foo\test : javac xcom\B.java
3. If you are somewhere else : javac C:\Program Files\Java\jdk1.6.0_13\bin\foo\test\xcom\B.java
You would use the -classpath/-cp with javac to tell the compiler where other dependencies are. In your case, only one class, no dependencies. No need to use it.

To run B.class, you have to tell where the xcom.B class is. You do this by setting the classpath. There are different ways again. Let's say that you moved the xcom package in C:\java
1. Add C:\java to the CLASSPATH environment variable, and execute from anywhere : java xcom.B
2. or, use the -classpath/-cp argument to set the classpath, and execute from anywhere : : java -cp c:\java xcom.B

[My Blog]
All roads lead to JavaRanch
 
GeeCON Prague 2014
 
subject: CLasspath Problem