Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CLasspath Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic