java.lang.ClassNotFoundException
I am currently working through the book "Sams Teach yourself Java 6 in 21 days" and am on Day 6. Today's lesson is about using packages. I have created a Storefront application consisting of two classes -Storefront and Item. These classes are organized in a package, org.im.ecommerce.
I have set the CLASSPATH variable as .;c:\Program Files\java\jdk1.6.0_01\lib\tools.jar;c:\dev\java
The Storefront.class and Item.class files are in the c:\dev\java\org\im\ecommerce folder.
I have a second application called GiftShop that uses the aforementioned classes. This application compiles successfully but when I try to execute it, we get the java.lang.ClassNotFoundException
Here is a snippet of code from each file.
GiftShop.java
import org.im.ecommerce.*;
public class GiftShop {
public static void main(String[] arguments) {
Storefront store = new Storefront();
//body of program
}
Storefront.java
package org.im.ecommerce;
import java.util.*;
public class Storefront {
//body of program
}
Item.java
package org.im.ecommerce;
import java.util.*;
public class Item implements Comparable {
//body of program
}
This is direct from the console.
%PROGRAMFILES%\jEdit> java -classpath "$CLASSPATH;C:\Users\Oscar\Documents\workspace " -ms32m -mx32m GiftShop
Exception in thread "main" java.lang.NoClassDefFoundError: org/im/ecommerce/Storefront
at GiftShop.main(GiftShop.java:6)
Process java exited with code 1
What else can I be looking at? Your help is greatly appreciated.
Re: java.lang.ClassNotFoundException
If you are using Windows to access an environment variable from the terminal use %VARIABLE_NAME% not $VARIABLE_NAME. In your case the get the CLASSPATH variable you should %CLASSPATH%.
Re: java.lang.ClassNotFoundException
Re: java.lang.ClassNotFoundException
I would recommend not setting up an environment variable for the classpath, except as a local one.
System wide classpaths for Java are not a good thing.