I'm working on an exercise out of a book, it seems fairly straight forward, but I'm getting a couple of errors that I can't figure out...I'm pasting the file then the error afterwards...I think my problem is that I don't know how to reference classes that are in a separate file, but I'm not sure....any help would be greatly appreciated...
The Player class compiles fine...
---------- 1. ERROR in GameLauncher.java (at line 9) GuessGame game = new GuessGame(); ^^^^^^^^^ GuessGame cannot be resolved to a type ---------- 2. ERROR in GameLauncher.java (at line 9) GuessGame game = new GuessGame(); ^^^^^^^^^ GuessGame cannot be resolved to a type ---------- 2 problems (2 errors)
1. ERROR in GuessGame.java (at line 5) Player p1; ^^^^^^ Player cannot be resolved to a type ---------- 2. ERROR in GuessGame.java (at line 6) Player p2; ^^^^^^ Player cannot be resolved to a type ---------- 3. ERROR in GuessGame.java (at line 7) Player p3; ^^^^^^ Player cannot be resolved to a type ----------
obviously, this last bit had more than three errors, but once I get those fixed, the rest will go away...
don, do you have a particular directory structure like com/oji/util/Hello.java. Than if you want to import Hello.java you say "import com.oji.util.Hello" or "import com.oji.util.*" I guess than the compiler would be happy. Should'nt it be "unexamined", chill brother. [ January 30, 2007: Message edited by: Abdulla Mamuwala ]
well, the directory that the java files are in is /home/dsartain/Desktop/java/code_projects/GuessGame
Yes, I'm using Linux for this...
All files are in the same folder, so would I need to import that entire line, or is there a way to edit the java.conf file to include the bulk of that in a classpath or something??
First of all, it is a little bit strange that you include the name of the class GuessGame in the package too:
Files are in /home/dsartain/Desktop/java/source/net/the24hourshow/GuessGame
package net.the24hourshow.GuessGame;
But ok, if that's what you want then you can do that. Are the other classes (Player, GameLauncher etc.) in the same directory, and do they have the same package statement? If so, then you do not need to import those classes at all.
Or are classes Player and GameLauncher not in a package at all (i.e. there is no package statement in the source files)? If that is the case, then you cannot import those classes in GuessGame. You'll have to put those classes in a package somewhere to be able to import them.
The form of the import statement is:
import <packagename>.<subpackagename>.ClassName;
to import a specific class in a package <packagename>.<subpackagename>, or
import <packagename>.<subpackagename>.*;
to import all classes in the package <packagename>.<subpackagename>. (Packages can be nested). So your import statements that look like this:
import Player.*;
are not correct. You don't have a package named "Player"; you have a class named "Player".
Yes, all the classes are part of the same package, but it still wouldn't work...I renamed the package to be more normal, so now it's just just net.the24hourshow with .GuessGame as the subpackage...I'll repaste my code again, maybe it's something else....Also, because I have the subpackage named GuessGame, I renamed the class GuessGame2...
I'm sorry it's taking me so long to get this, I just don't see what the problem is...all the classes are in the same folder, declared in the same package, so I shouldn't have to import anything, but I get the same errors either way....
Code above receives this error: ---------- 1. ERROR in net/the24hourshow/GuessGame/GameLauncher.java (at line 2) import GuessGame.*; //imports subpackage ^^^^^^^^^ The import GuessGame cannot be resolved ---------- 2. ERROR in net/the24hourshow/GuessGame/GameLauncher.java (at line 3) import GuessGame.GuessGame2; //imports subpackage.class ^^^^^^^^^ The import GuessGame cannot be resolved
1. ERROR in net/the24hourshow/GuessGame/GuessGame2.java (at line 3) import GuessGame.*; //imports subpackage ^^^^^^^^^ The import GuessGame cannot be resolved ---------- 2. ERROR in net/the24hourshow/GuessGame/GuessGame2.java (at line 4) import GuessGame.Player; //imports subpackage.class ^^^^^^^^^ The import GuessGame cannot be resolved ----------
I was able to compile them individually as well...
Problem is when I run "[dsartain@localhost classes]$ java -classpath "." net.the24hourshow.GuessGame.GameLauncher.class" it gives me the error below...Any ideas...the code hasn't changed, other than me removing the import lines....Also, I can run this fine in the NetBeans IDE, but as I will most likely not be using that the majority of the time, I'd like to get this command line thing working...
[dsartain@localhost GuessGame]$ java -classpath "." net.the24hourshow.GuessGame.GameLauncher.class Exception in thread "main" java.lang.NoClassDefFoundError: net.the24hourshow.GuessGame.GameLauncher.class at gnu.java.lang.MainThread.run(libgcj.so.7rh) Caused by: java.lang.ClassNotFoundException: net.the24hourshow.GuessGame.GameLauncher.class not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}} at java.net.URLClassLoader.findClass(libgcj.so.7rh) at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.7rh) at java.lang.ClassLoader.loadClass(libgcj.so.7rh) at java.lang.ClassLoader.loadClass(libgcj.so.7rh) at gnu.java.lang.MainThread.run(libgcj.so.7rh)
[ January 31, 2007: Message edited by: Don Sartain ]
Ok, I finally got everything working. I had to modify the Linux Classpath by
CLASSPATH=path export CLASSPATH
Then I had to go to my source directory and compile using
javac -classpath $CLASSPATH path/to/filename.java
Then I went to my classes directory and ran the file with
java net.the24hourshow.GuessGame.GameLauncher
The source code is already posted, but I did have to make sure I included the package statement in each class, without trying to import the classes that were in the same package.
Thanks to those that helped.
Post by:autobot
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, the permaculture playing cards