Exception in thread "main" java.lang.NoClassDefFoundError
Hi,
I've installed Java Netbeans. Then I tried to compile these piece of code from a book. I got an error. I tried compiling using cmd prompt instead of netbeans. I don't understand why it's not possible to compile?
Code :
D:\java>java PriorityDemo.java
Exception in thread "main" java.lang.NoClassDefFoundError: PriorityDemo/java
Caused by: java.lang.ClassNotFoundException: PriorityDemo.java
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: PriorityDemo.java. Program will exit.
That was what i got. Below is the code that I've wrote.
Code :
// Demonstrate thread priorities.
class Priority implements Runnable {
int count;
Thread thrd;
static boolean stop = false;
static String currentName;
Priority(String name) {
thrd = new Thread(this, name);
count = 0;
currentName = name;
}
public void run() {
System.out.println(thrd.getName() + " starting.");
do {
count++;
if(currentName.compareTo(thrd.getName()) != 0) {
currentName = thrd.getName();
System.out.println("In " + currentName);
}
} while(stop == false && count < 1000000);
stop = true;
System.out.println("\n" + thrd.getName() + " terminating.");
}
}
class PriorityDemo {
public static void main(String args[]) {
Priority mt1 = new Priority("High Priority");
Priority mt2 = new Priority("Low Priority");
mt1.thrd.setPriority(Thread.NORM_PRIORITY+2);
mt2.thrd.setPriority(Thread.NORM_PRIORITY-2);
mt1.thrd.start();
mt2.thrd.start();
try {
mt1.thrd.join();
mt2.thrd.join();
}
catch(InterruptedException exc) {
System.out.println("Main thread interrupted.");
}
System.out.println("\nHigh Priority thread counted to " + mt1.count);
System.out.println("Low Priority thread counted to " + mt2.count);
}
}
Edit: however the code works fine once I use NetBeans of course.
Re: Help with compilation
Welcome to Java Programming Forums.
I hope you enjoy it here :)
Anyway to your problem. You must name your file "PriorityDemo.java" to match the name of your main class.
Also I have changed the code so that your main class is defined as public, so it should look something like this. Also note if you have placed the Java file inside a package you will need to state that within the code.
Code :
// Demonstrate thread priorities.
class Priority implements Runnable {
int count;
Thread thrd;
static boolean stop = false;
static String currentName;
Priority(String name) {
thrd = new Thread(this, name);
count = 0;
currentName = name;
}
public void run() {
System.out.println(thrd.getName() + " starting.");
do {
count++;
if(currentName.compareTo(thrd.getName()) != 0) {
currentName = thrd.getName();
System.out.println("In " + currentName);
}
} while(stop == false && count < 1000000);
stop = true;
System.out.println("\n" + thrd.getName() + " terminating.");
}
}
public class PriorityDemo {
public static void main(String args[]) {
Priority mt1 = new Priority("High Priority");
Priority mt2 = new Priority("Low Priority");
mt1.thrd.setPriority(Thread.NORM_PRIORITY+2);
mt2.thrd.setPriority(Thread.NORM_PRIORITY-2);
mt1.thrd.start();
mt2.thrd.start();
try {
mt1.thrd.join();
mt2.thrd.join();
}
catch(InterruptedException exc) {
System.out.println("Main thread interrupted.");
}
System.out.println("\nHigh Priority thread counted to " + mt1.count);
System.out.println("Low Priority thread counted to " + mt2.count);
}
}
Regards,
Chris
Re: Help with compilation
Thanks for the reply. Yep, i indeed have my file named PriorityDemo.java exactly how I typed for the class name. I'm still getting main class not found :(
my PriorityDemo.java is located in the D:\java folder.
Code :
D:\java>java "D:\java\PriorityDemo.java"
Exception in thread "main" java.lang.NoClassDefFoundError: D:\java\PriorityDemo/
java
Caused by: java.lang.ClassNotFoundException: D:\java\PriorityDemo.java
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: D:\java\PriorityDemo.java. Program will exit.
D:\java>
Re: Help with compilation
Oh.
java - is the command to run a java program. First you must use javac [file] then java [file] (but with no extension. javac will produce a .class file which is then runable. Here is an example that should work for you.
D:\java>javac "D:\java\PriorityDemo.java"
D:\java>java "D:\java\PriorityDemo"
Regards,
Chris
Re: Help with compilation
Hello Zepx,
Please try this and tell me what happens:
In Netbeans, select the .java file you are working on. Right click it and click Compile File (F9), once its compiled, right click again and click Run File (Shift+F6)
Hopefully it will work now...
Re: Help with compilation
Hi Zepx
you need to do this :-
1) D:\java>javac -cp . PriorityDemo.java
2) D:\java>java -classpath . PriorityDemo
I think that problem lies in the fact that your compiler is not able to find your class file when it is trying to compile or run the classes. That is because the current directory is not in the list of directories that the compiler searches when it is trying to compile or execute the files.
This is specified by using -cp or -classpath in the javac and java commands as shown above.
This should do the trick.
Re: Help with compilation
The solution for most of the Java related errors can be found at this site: Java errors and solutions.