Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Exception in thread "main" java.lang.NoClassDefFoundError

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    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.
    Last edited by Zepx; April 25th, 2009 at 04:31 AM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

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

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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>

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with compilation

    The solution for most of the Java related errors can be found at this site: Java errors and solutions.