File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes I Cannot Get My Thread Program Compiled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Cannot Get My Thread Program Compiled" Watch "I Cannot Get My Thread Program Compiled" New topic
Author

I Cannot Get My Thread Program Compiled

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have both JDK 1.2.2 and JDK 1.3 in my machine. I think that there is no path setup problem because I have completed Java projects for the Java-fundamental course I took, and I have written numeous small Java programs ant tested them out.
Nevertheless, I have problems with compiling Thread programs (all the small Thread programs that I have). Finally, I tried to copy and paste one of the example programs in the book entitled "Java How to Program" by Dietel and Dietel. I believe there is nothing wrong with that program (see below), and I was not able to compile that program either, especially the name of the program is ThreadTester. The error message was:
file C:\javaprj\Thread.java does not contain type Thread as expected. Please adjust the class path so that the file does not appear in the unnamed package.
Here is the program:
public class ThreadTester {
public static void main( String args[] )
{
PrintThread thread1, thread2, thread3, thread4;
thread1 = new PrintThread( "thread1" );
thread2 = new PrintThread( "thread2" );
thread3 = new PrintThread( "thread3" );
thread4 = new PrintThread( "thread4" );
System.err.println( "\nStarting threads" );
thread1.start();
thread2.start();
thread3.start();
thread4.start();
System.err.println( "Threads started\n" );
}
}
class PrintThread extends Thread {
private int sleepTime;
// PrintThread constructor assigns name to thread
// by calling Thread constructor
public PrintThread( String name )
{
super( name );
// sleep between 0 and 5 seconds
sleepTime = (int) ( Math.random() * 5000 );
System.err.println( "Name: " + getName() +
"; sleep: " + sleepTime );
}
// execute the thread
public void run()
{
// put thread to sleep for a random interval
try {
System.err.println( getName() + " going to sleep" );
Thread.sleep( sleepTime );
}
catch ( InterruptedException exception ) {
System.err.println( exception.toString() );
}
// print thread name
System.err.println( getName() + " done sleeping" );
}
}

Would anybody kindly give a hand.
Rosie Suayan
Greenhorn

Joined: Nov 29, 2000
Posts: 7
Your class name and filename do not match. It looks like you are trying to compile and run "Thread" when it should be "ThreadTester." Rename the file to ThreadTester.java, compile and try rerunning.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I am 100% certain that I made this kind of mistake. All of the thread programs cannot get compiled. I did type javac ThreadTester.java to compile the particular one I posted, and that is the error message I got.
Kathleen Chapman
Greenhorn

Joined: Nov 02, 2000
Posts: 2
It wouldn't matter that you typed that the file was ThreadTester.java . The name of your file and the name of your public class must be the same.

ex:
If your class was:

public class TestClass{}

Then your file must be named:

TestClass.java

So in the case of the source code posted the name of the file should be. C:\javaprj\ThreadTester.java .

Hope this helps.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have tried many times. My file name is ThreadTester.java and I typed javac ThreadTester.java to compile the program.
I even put the ThreadTester.java program in "some other directory" and tried to compile it under that directory. The error message has been consistent and the compiler seems to look at the C:\javaprj directory only:
File C:\javaprj\Thread.java does not contain type Thread as expected. Please adjust the class path so that the file does not appear in the unnamed package.
I think something else is screwed up. It is not I don't know how to name a java file or I don't know how to compile. I have written many non-thread java programs and I have had no problem with them.
Surya Bahadur
Ranch Hand

Joined: Sep 28, 2000
Posts: 88
Hi
Can you be more specific as to how you are winding up with this situation..what exactly do you have in the C:\javaprj directory,what java files does it have???Your problem has got to do something with the path and the classpath..from the command line type in
echo %path%
echo %classpath%
and make sure that it is set properly..
Surya
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks, Surya, for paying attention to my problem. At the C:\> prompt
echo %path%
returns C:\WINDOWS;\C:\WINDOWS\COMMAND;\JDK1.2.2\BIN;\C:\JAVAPRJ
echo %classpath%
returns c:\javaprj
My C:\javaprj directory is the directory I save all my java progrm files. There are about 100 Java programs. All the non-thread related programs compile without problem. The history of the particular problem that I posted at the javaranch is:
Many days ago, I tried to compile a thread related program. The file name was Thread.java in the C:\javaprj directory and I got compile error. I have never deleted this particular program from the directory (the Thread.java remains in the C:\javaprj). Ever since, I have never been able to compile any thread related program. Whenever I run a thread related program with different file name (such as the one named ThreadTester.java) or I run a thread related program in some other directory, trouble!
May be I should throw that C:\javaprj\Thread.java into the recycle bin and see what happens. I have not yet tried this option because it just doesn't make sense to me. Or I should give a try if I want to make things work. What do you think.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks to all who have responded to my post. Your effort in helping me is much appreciated.
I have figured out that I should never use Thread as a file name. Because all the thread related programs either extends Thread class or implements Runnable interface. The compiler finds that there is a program file called Thread.java and gets very confused.
The discussion helped me to think through the problem. Thanks again.
 
wood burning stoves
 
subject: I Cannot Get My Thread Program Compiled