This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes IDE or bust? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "IDE or bust?" Watch "IDE or bust?" New topic
Author

IDE or bust?

Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Hi all,

Today I came to a very scary realization "I know nothing!"
or should I say without my IDE (in this case netbeans) I know nothing.

I do a general software development coarse and for programming concepts we were slightly introduced to Java, only after we were introduced to NetBeans that is,
I have now started on my elective which is the SCJP and SCJD but as soon as I started the Exam guide by Kathy Sierra and Bert Bates I knew that I needed to learn more about the language.
So currently I am learning from the book Head First Java, while I am following the examples I came to a problem that stunned me namely "I did not know how to compile more than one file using the command line"
I have spent today trying to figure out how I can compile the files for the guessing game.

I thought I would put the files in a folder called game and then insert package game; at the top of the java files. but when I try to compile
I still receive error messages about symbols that can't be found (because they are in one of the other classes)
so because in NetBeans I simply make a new project and the classes come under the same package everything works fine, but I need to know Java without relying on an IDE as a beginner I think it is fundamental.

I have read some similar posts here and tried the solutions but I still cannot get these files to compile.
Can anybody show me the way or give me a step by step guide how I go about compiling these files.

Here are the classes:




Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3617
    
  14

Well, since all files are indirectly referenced by GameLauncher, you only have to compile that file, and they will all get compiled if they changed between compilations.

What's the error you're running into?
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
the error is:

GameLauncher.java:4: error: cannot find symbol
GuessGame game = new GuessGame();
^
symbol: class GuessGame
location: class GameLauncher
GameLauncher.java:4: error: cannot find symbol
GuessGame game = new GuessGame();
^
symbol: class GuessGame
location: class GameLauncher
2 errors
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3617
    
  14

You probably haven't specified the classpath, so the compiler doesn't know where to look for game\GuessGame.java.

In the command line, go to the folder that contains the game folder, and try: "javac -cp . game\GuessGame.java"
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Or try javac *.java from the game folder. Similar thread
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3617
    
  14

No, that won't work because the compiler still needs to find classes relative to the classpath. If the game folder is part of the package, you can't compile when it's also the working directory, unless you specify the containing folder as the classpath explicitly.
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Stephan van Hulst wrote:You probably haven't specified the classpath, so the compiler doesn't know where to look for game\GuessGame.java.

In the command line, go to the folder that contains the game folder, and try: "javac -cp . game\GuessGame.java"


I tried this already having found it on another post, but unfortunately I still receive similar errors:

C:\Users\Sonny\Documents\Head1stJava\src>javac -cp . game\GuessGame.java
game\GuessGame.java:24: error: cannot find symbol
p1.guesse();// call each players guess method
^
symbol: method guesse()
location: variable p1 of type Player
game\GuessGame.java:25: error: cannot find symbol
p2.guesse();
^
symbol: method guesse()
location: variable p2 of type Player
game\GuessGame.java:26: error: cannot find symbol
p3.guesse();
^
symbol: method guesse()
location: variable p3 of type Player
game\GuessGame.java:46: error: cannot find symbol
p3isRight = true;
^
symbol: variable p3isRight
location: class GuessGame
game\GuessGame.java:49: error: cannot find symbol
if (p1isRight || p2isRight || p3isRight) {
^
symbol: variable p3isRight
location: class GuessGame
game\GuessGame.java:54: error: cannot find symbol
System.out.println("Player three got it right? " + p3isRight)
^
symbol: variable p3isRight
location: class GuessGame
6 errors


Steven Bruton
Greenhorn

Joined: Mar 07, 2011
Posts: 13
You have 6 compile errors in your code, that is why it will not compile.

Just some spelling errors;
The boolean p3isRIght should be p3isRight,
You made a call to guesse() 3 times but the method name is guess().
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Steven Bruton wrote:You have 6 compile errors in your code, that is why it will not compile.

Just some spelling errors;
The boolean p3isRIght should be p3isRight,
You made a call to guesse() 3 times but the method name is guess().


Hi Steven,

Thank you,
yes you are right and I should be banned for posting that code without checking!
but I did fix up those errors (I just have not posted the fixed code.

I actually reverted back to netbeans to test my code and it works fine:
Result:
run:
I'm thinking of a number between 0 and 9...
Number to guess is 1
I'm guessing 6
I'm guessing 3
I'm guessing 2
Player one guessed 6
Player two guessed 3
Player three guessed 2
Players will have to try again.
Number to guess is 1
I'm guessing 3
I'm guessing 0
I'm guessing 1
Player one guessed 3
Player two guessed 0
Player three guessed 1
We have a winner!
Player one got it right? false
Player two got it right? false
Player three got it right? true
Game Over!
BUILD SUCCESSFUL (total time: 1 second)


So it has to be how I am trying to compile it on the command line
I found this site http://javaworkshop.sourceforge.net/chapter3.html and have been trying to follow it by making the test folder etc.
I am still having no luck but give me a few moments and I will post the errors I am getting.
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
I will post exactly what I am doing, so this way, I will hope to find out what I am doing wrong:

Steven Bruton
Greenhorn

Joined: Mar 07, 2011
Posts: 13
You compiled it successfully then, your problem is running it.

Try this;
Navigating back to this directory: "C:\Users\Sonny\Documents\Head1stJava\test\classes\com\headfirst",
From that directory try this command "java game/GameLauncher".
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
I have tried your suggestion and receive the exact same error as above:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40


Your code ....

Nick de Waal wrote:



says that the GameLauncher class is in the game package.

And your error...

Nick de Waal wrote:
C:\Users\Sonny\Documents\Head1stJava\test\classes\com\headfirst>cd game

C:\Users\Sonny\Documents\Head1stJava\test\classes\com\headfirst\game>java GameLauncher
Exception in thread "main" java.lang.NoClassDefFoundError: GameLauncher (wrong name: com/headfirst/game/GameLauncher)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)


says that your GameLauncher class is in the com.headfirst.game package.

Something has changed that you didn't tell us.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Hi Henry,
Actually I did mention it although I was rather vague, my apologies,
so here is a copy of the code as it stands:





Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Nick de Waal wrote:Hi Henry,
Actually I did mention it although I was rather vague, my apologies,
so here is a copy of the code as it stands:


Then based on this new data.... Let's take Steven's last response....

Steven Bruton wrote:You compiled it successfully then, your problem is running it.

Try this;
Navigating back to this directory: "C:\Users\Sonny\Documents\Head1stJava\test\classes\com\headfirst",
From that directory try this command "java game/GameLauncher".


And adjust it to....

Try this;
Navigating back to this directory: "C:\Users\Sonny\Documents\Head1stJava\test\classes"
From that directory try this command: "java com.headfirst.game.GameLauncher"


Henry
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Henry that's it!!

Thank you,
if I may ask, would you be able to give me a small explanation as to what is going on? I mean, using the "." instead of what I was doing.

Or maybe point me to an explanation on the web.
It would be much appreciated
thanks again
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Nick de Waal wrote:Henry that's it!!

Thank you,
if I may ask, would you be able to give me a small explanation as to what is going on? I mean, using the "." instead of what I was doing.

Or maybe point me to an explanation on the web.
It would be much appreciated
thanks again


Basically, the java command takes the fully qualified class name as the parameter... so, if the "GameLauncher" class is in the "com.headfirst.game" package, the the fully qualified class name is "com.headfirst.game.GameLauncher". This is why I always use batch files to run java commands, as typing the fully qualified name each time is annoying.

As for how the java command finds the location of the class files, it starts from the class root, then traverses directories based on the package name. For your example, the class root directory is "C:\Users\Sonny\Documents\Head1stJava\test\classes", followed by the "com\headfirst\game" directory which represent the package name, followed by the "GameLauncher.class" file which is loaded by the JVM.... How the class root is specified is via the classpath. You can specify the classpath via an environment variable or via a parameter to the java command. Since you have done neither, the classpath is the current directory, which means that you need to be located at the class root to run your program. Of course, another good use of the batch file is to set the classpath variable.

Henry




Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Hi Henry,

Thank you for taking the time for the explanation, as with most newbies (I'm guessing) we don't use the command line enough,
I am learning a little batch programming now, so that I can be more independent.

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IDE or bust?