• 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

Errors when compiling these classes...

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey don,

Are these different class files in the same "package". If they are not you need to "import" them. take care.
 
Don Sartain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They aren't a part of a package...I tried importing them, but that didn't work...they're all in the same directory, so I should just be able to

right???

If not, how do I do that?? I can import the standard java.* fine, but it's the custom classes I'm having issues with....
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don Sartain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Don Sartain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Files are in /home/dsartain/Desktop/java/source/net/the24hourshow/GuessGame

package = net.the24hourshow.GuessGame
classpath = /home/dsartain/Desktop/java/classes

Modified Files:


is at the beginning of GuessGame.java...

when I run javac -classpath /home/dsartain/Desktop/java/classes -d ../classes net/the24hourshow/GuessGame/GuessGame.java

I had to add the -classpath parameter to get the import to work...for some reason it still wants me to do that, even though I packaged the classes..

I get the following error:

1. ERROR in net/the24hourshow/GuessGame/GuessGame.java (at line 4)
import Player.*;
^^^^^^
The import Player cannot be resolved
----------



Any ideas where I'm messing up...this whole Head First book isn't making any sense when it comes to this...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".

Have a look at this: The Java Tutorial: Packages
 
Don Sartain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
----------

 
Don Sartain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, good news is that I got all of these classes to compile by typing


javac -d ../classes -classpath ../classes/net/the24hourshow/GuessGame net/the24hourshow/GuessGame/*.java



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 ]
 
Don Sartain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic