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 Do I need 1 of 3 seperate .java files??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Do I need 1 of 3 seperate .java files???" Watch "Do I need 1 of 3 seperate .java files???" New topic
Author

Do I need 1 of 3 seperate .java files???

Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
I'm working my way through the book "Head First Java" and they have a code example for a number guessing game. Chapter 2, Page 39.

I have written the code down but am unsure if I should have all the code in one file or three seperate files.

If I put it in one file and compile it I get 2 errors:
GameLauncher2.java:1: class GuessGame is public, should be declared in a file named GuessGame.java
public class GuessGame {
^
GameLauncher2.java:63: class Player is public, should be declared in a file named Player.java
public class Player {

If I enter it in three seperate files and save them as three seperate files they compile fine but when I run the GameLauncher it returns an:
Exception in thread "main" java.lang.NoClass.DefFoundError: GameLauncher

What am I doing wrong?

First should I be entering all the code in one file of should they be 3 seperate .java files? I'm most confused.


Code Below
==========


[ May 07, 2005: Message edited by: Tim Dee ]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
when i first started reading Thinking in Java i was surprised by the fact that one could actually have 2 (or more) classes in a single file!

And yes you can, provided only one class is declared public (else you have to split them). And the file name should follow the name of the class that has the main method.

if you�r running your command prompt from the same dir as the one where you compiled your .java, then java should find your GameLauncher.class, like this:

YourDir>java GameLauncher


java amateur
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1010
    
    3
Any class that is declared public should be in its own file with the same name as the class. Public class GameLauncher shoudl be in GameLauncher.java, etc. Having one class per file is a practice that will serve you well when you get into bigger projects.

I think your NoClassDefFoundError is a classpath issue. Since you didn't put a package statement at the top of any of your files, all the classes are in the default no-name package. You'd think that java would be smart enough to find your classes in the current directory without being told, but you'd be wrong.

You have to put the directory that your .class files are in on the classpath. If you just want to run the program from the current directory, you can just put . in the classpath. If you want to run the Game from anywhere, put the absolute path to the file on the classpath.

How, you may ask, do you set the class ath? Two ways: A) Temporarily on the java command line or B) as an environment variable.

You could try this:
java -classpath . GameLauncher

Or this on Windows:
set CLASSPATH=.
echo %CLASSPATH%
java GameLauncher

Or this *nix:
CLASSPATH=.
echo $CLASSPATH
java GameLauncher

I hope this helps.
[ May 07, 2005: Message edited by: Ryan McGuire ]
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
Originally posted by miguel lisboa:

And yes you can, provided only one class is declared public (else you have to split them). And the file name should follow the name of the class that has the main method.

if you�r running your command prompt from the same dir as the one where you compiled your .java, then java should find your GameLauncher.class, like this:

YourDir>java GameLauncher


So will the example code I gave work in either 1 file or 3? The compiler certainly appears to prefer it as seperate files. However I thought that each class had to have a main method. Or is it just as long as the first class (or is it object) called has to have a main?

Why should only one class be declaired public? I thought that by delaring it public it just made variables available to any other object that called it.

Many thanks for the reply.
Tim
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
However I thought that each class had to have a main method.

each application has to have one main - the entry point.

Why should only one class be declaired public?

HFJ answers that one (later on)
public means your class can be seen by everybody, but these questions will appear later when you start with packages.

With your actual classes you can safelly remove all public statements and run it as well
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
So I could create a java prog that read a joystick (joystick.java) and then had another .java file that moved a cursor about the screen and it could get it's input on movement from the joystick object. Is this correct?

I'm used to writing in Basic but Java is totaly new to me so please excuse me if I'm confused.

Classpath is also new to me. I'm farmiliar with the DOS command Path and it appears simular to your classpath.
I'm currently writing/compiling & running my apps in JCreator. Do I have to set a classpath up in that prog?

Many thanks again for your help. I will understand it eventualy, I hope.


Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Tim Dee:
So I could create a java prog that read a joystick (joystick.java) and then had another .java file that moved a cursor about the screen and it could get it's input on movement from the joystick object. Is this correct?

I'm used to writing in Basic but Java is totaly new to me so please excuse me if I'm confused.

Classpath is also new to me. I'm farmiliar with the DOS command Path and it appears simular to your classpath.
I'm currently writing/compiling & running my apps in JCreator. Do I have to set a classpath up in that prog?

Many thanks again for your help. I will understand it eventualy, I hope.




For now, you shouldn't worry about the classpath. I suspect that JCreator has a default classpath that seems to be working fine for you, since you can compile your classes, albeit with some errors.

Now that I go back and read the previous messages more closely, I have a question. How are you running the program when you compile it in 3 separate files? Are you using JCreator or are you running it from the command line?

Layne
[ May 07, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
if i remember well HFJ recomends you compile and run your code using strictly command line - and that's a very good advice!

this way you'll get feedback from javac and java and eventually you'll understand what's really going on.

Later, when you feel secure about command line, then you can change to an IDE

for now, read this
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
Originally posted by Layne Lund:

How are you running the program when you compile it in 3 separate files? Are you using JCreator or are you running it from the command line?

Layne

[ May 07, 2005: Message edited by: Layne Lund ]


I'm doing it all from JCreator. You can have many .java files open at once. My example looks like this:



After compiling all 3 from JCreator I then run GameLauncher from JCreator.
Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
Many. many thanks guys, I got it working.

I used 3 seperate files all in the same directory and ran it from DOS using the command
java -classpath . GameLauncher

Works a charm.

I will do more homework on classpath.
 
 
subject: Do I need 1 of 3 seperate .java files???