Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old October 6th, 2008, 11:31 AM
Newbie
 
Join Date: Sep 2008
Posts: 10
Default creating thread : error

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
Reply
  #2  
Old October 6th, 2008, 12:50 PM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,493
Default

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
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles