Connecting Tech Pros Worldwide Forums | Help | Site Map

creating thread : error

Newbie
 
Join Date: Sep 2008
Posts: 10
#1: Oct 6 '08
Hi
Expand|Select|Wrap|Line Numbers
  1. class NewThread implements Runnable {
  2.     Thread t;
  3.  
  4.     NewThread() {
  5.         t = new Thread(this, "Demo Thread");
  6.         System.out.println("Child Thread:" + t);
  7.         t.start();
  8.     }
  9.  
  10.     public void run() {
  11.         try {
  12.             for (int i = 5; i > 0; i--) {
  13.                 System.out.println("Child Thread:" + i);
  14.                 Thread.sleep(500);
  15.             }
  16.         } catch (InterruptedException e) {
  17.             System.out.println("Child interuppted.");
  18.         }
  19.         System.out.println("Exiting child Thread");
  20.     }
  21. }
  22.  
  23. class ThreadDemo {
  24.     public static void main(String[] args) {
  25.         new NewThread();
  26.         try {
  27.             for (int i = 5; i > 0; i--) {
  28.                 System.out.println("Main Thread:" + i);
  29.                 Thread.sleep(1000);
  30.             }
  31.         } catch (InterruptedException e) {
  32.             System.out.println("Main thread interuppted.");
  33.         }
  34.         System.out.println("Main thread exiting.");
  35.     }
  36. }
  37.  
When i try to run it on command prompt i get this error.

c:\java>javac ThreadDemo.java

c:\java>java ThreadDemo
Exception in thread "main" java.lang.NoClassDefFoundError: ThreadDemo
Caused by: java.lang.ClassNotFoundException: ThreadDemo
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)

c:\java>

CAN YOU PLEASE GUIDE ME WHERE I AM WRONG.

Regards,

Neha

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Oct 6 '08

re: creating thread : error


Run it like this:

Expand|Select|Wrap|Line Numbers
  1. java -classpath . ThreadDemo
  2.  
This tells the JVM to look in the current directory '.' for classes. Also make your
ThreadDemo class a public class.

kind regards,

Jos
Reply