File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

CLasspath Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i am confused about how to use -classpath with

if i have a file in C:\Program Files\Java\jdk1.6.0_13\bin\foo\test\xcom directory
and i compile and run using
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 Source)
at Source)
at$000(Unknown Source)
at$ Source)
at Method)
at 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. contains :

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

It contains the following code

i complied and run it using:
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

Joined: Aug 03, 2008
Posts: 9321

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

Joined: Feb 24, 2006
Posts: 6662

i complied and run it using:
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é

Joined: Nov 24, 2005
Posts: 14688

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, 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
2. If you are in C:\Program Files\Java\jdk1.6.0_13\bin\foo\test : javac xcom\
3. If you are somewhere else : javac C:\Program Files\Java\jdk1.6.0_13\bin\foo\test\xcom\
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
I agree. Here's the link:
subject: CLasspath Problem