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?
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.
// 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.