• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runnable problem  RSS feed

 
David A King
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'm a newbee. I'm reading the "Head First Java" book and I'm doing fairly well, I thought. I got to the chapter that is explaining threads and such. I did the first example they gave without any problem. So I can assume that my JAVA and paths are all ok. I've been running all the examples since the beginning and i'm on chapter 15 now.
The problem is I am definitely missing something in my understanding about either making a class file compiling it then referencing it from another class file that needs it, or just placing the class definition in the same file. Either as an inner class or outside of the main class. So the book shows two code snippits.

The first one is for the class that implements Runnable. It is called from the other class's "main method" when it starts a new thread. The code snippit given is;

// MyRunnable.java

The other code snippit is given for the class that contains the main method, that starts the second thread and kicks the run() method into action.
Well it would if it worked (grin).

Here is the code snippit for that class;

// ThreadTester.java


I have saved both of these code files into the same directory and when I try to run javac on either of them all I get are complaints about the Runnable class being PUBLIC and how it should be in its own class file named MyRunnable.java.
I have that file and that is the one I am trying to compile. Or that the Runnable class is not found. I might suspect a path issue if it wasn't for the fact I have another program in the same directory that uses the Runnable interface.
I tried putting the MyRunnable class into the code of ThreadTester as an inner class so they might see each other, but no go.

Can somebody tell me what I'm doing soooo wrong here?

Thanks,

*Dave
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to run your code without problems. And you have already answered your own question.
DavidA King wrote:I have saved both of these code files into the same directory and when I try to run javac on either of them all I get are complaints about the Runnable class being PUBLIC and how it should be in its own class file named MyRunnable.java.

That is exactly the solution and the answer.
Each public class must be in file named exactly as the class + .java exension.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your ThreadTester should probably be public.

I made that change, and it compiled and ran fine.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch
 
David A King
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to run the javac on the MyRunnable.java file I get thsi error message.

C:\Users\User1\javasrc\chapter15>javac MyRunnable.java
.\Runnable.java:1: class MyRunnable is public, should be declared in a file name
d MyRunnable.java
public class MyRunnable implements Runnable {
^
MyRunnable.java:2: cannot access Runnable
bad class file: .\Runnable.java
file does not contain class Runnable
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
public class MyRunnable implements Runnable {
^

** Also I added the "public" statement to the ThreadTester.java file, snippit shown below;

// ThreadTester.java

public class ThreadTester {


public static void main(String[] args) {

Runnable threadJob = new MyRunnable();
Thread myThread = new Thread(threadJob);

myThread.start();

System.out.println("back in main");
}

}//end of class

When I run the javac on it I get the following errors;

C:\Users\User1\javasrc\chapter15>javac ThreadTester.java
.\Runnable.java:1: class MyRunnable is public, should be declared in a file name
d MyRunnable.java
public class MyRunnable implements Runnable {
^
.\Runnable.java:1: interface expected here
public class MyRunnable implements Runnable {
^
ThreadTester.java:8: cannot access Runnable
bad class file: .\Runnable.java
file does not contain class Runnable
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Runnable threadJob = new MyRunnable();
^
3 errors

** Any Idea what might be wrong with my files or setup?
My understanding is that Runnable is a java.lang built in method. So if it can find the compiler it should be able to find the default class file, right?
I have been using java.net java.util java.io java.awt etc without any problems.
?
Thanks for your quick answers.

*Dave
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting two errors

the first
.\Runnable.java:1: class MyRunnable is public, should be declared in a file name
d MyRunnable.java

the second:
MyRunnable.java:2: cannot access Runnable
bad class file: .\Runnable.java
file does not contain class Runnable

ANY time you get more than one...Ignore all but the first. All the rest are meaningless. When the compiler encounters an error, it makes some guesses in an attempt to keep going. This usually leads to a whole cascade of problems.

So, ignore everything but that first error. Fix it. Then re-compile and see what happens.

So...What EXACTLY are you file(s) named?
 
David A King
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My two files are named;

ThreadTester.java

and

MyRunnable.java


*Dave
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this in a file named "ThreadTester.java" :



I have this in a file named "MyRunnable.java":

Both files are in the same directory. Both compile, and "java ThreadTester" runs.

Try deleting any .class files you have, and compile again? I'm not sure what else to suggest.
 
David A King
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange but True. I deleted the files from the directory, saved them again and tried to compile. NO GO.
There were no class files because they would not compile due to the errors shown.

I made a new directory in my current directory, saved the files into it and tried to compile them again.
It WORKED!
And the program runs.

Why? Who Knows? But the same code that would not compile in the one directory does in the other!

Thank You for all of your help!

*Dave
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David A King wrote:Why? Who Knows?

It was failing to compile because the file containing the class MyRunnable was called Runnable.java and not MyRunnable.java. It was either operator error (ie you didn't spot the file name was missing the My prefix) or possibly the windows file display name was different to the file name on the file system so what you were seeing and what the compiler were seeing were different names (I vaguely remember seeing this type of phenomenon on early windows systems but I think this was down to file names being stored in 8.3 format and I can't remember seeing it from xp onwards). It's a shame you no longer have the original files so we can investigate further.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!