• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IDE or bust?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:




 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or try javac *.java from the game folder. Similar thread
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried your suggestion and receive the exact same error as above:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Nick de Waal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic