I created a List.java file in folder UtilityPack which contains this code
package Utilities;
public class List
{
private class node{}
public void insert(int data){}
public void print(){}
public static void main(String[] s){}
}
To compile i did
C:\UtilityPack>javac List.java
But when I try to run with
C:\UtilityPack>java -classpath . List
OR
C:\UtilityPack>java List
I get error
Exception in thread "main" java.lang.NoClassDefFoundError: List (wrong name: Uti
lities/List)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I have been trying to execute this program from last 3 hours but nothing worked..please help
You need the fully qualified name e.g.
i.e. you’re telling the JVM to look from the current direct (
-cp .) for a classUtilities.List, which it will expect in the fileUtilities\List.class.To be more consistent you should put the .java file under a
Utilitiesdirectory (yes – this is tautologous – the package specifies this, but it’s consistent practise).I would also avoid calling your class
List. At some stage you’re going to import ajava.util.Listand it’ll all get very confusing!Finally, as soon as you get more than a couple of classes, investigate ant or another build tool, and separate your
sourceandtargetdirectories.