Error Cannot Find Symbol Class Solution
-
October 6th, 2011,03:46 AM #1
[RESOLVED] Cannot find symbol class
I am new to java. I am trying a simple game with three classes SimpleDotComGame, SimpleDotCom, GameHelper in three different .java files all put together in a directory dotGame. SimpleDotComGame.java has the main method. I have declared package name as in each of these files. SimpleDotCom.java and GameHelper.java compile without any errors. But when I run SimpleDotComGame.java I get the following error:
Code:
cannot find symbol symbol: class GameHelper
-
October 6th, 2011,09:03 AM #2
Re: Cannot find symbol class
SimpleDotComGame.java is looking for a class called GameHelper when the class is actually called dotGame.GameHelper which is why it can't find it.
Have you recompiled SimpleDotComGame.java after adding the package name to the other classes.
-
October 6th, 2011,09:41 AM #3
Re: Cannot find symbol class
Originally Posted by keang
Have you recompiled SimpleDotComGame.java after adding the package name to the other classes.
-
October 6th, 2011,10:38 AM #4
Re: Cannot find symbol class
Hey, I got the solution. The code started working fine when I removed
from all the three classess!But I didn't get the reason why!
-
October 6th, 2011,02:31 PM #5
Re: Cannot find symbol class
It works without the package name because classes without a package name are all put in the default package and as long as they are all in the same directory they are all visible to each other.
It will also work with the package name if you set it up correctly. Can you post all the code and/or show how you are compiling the classes and running the app and also any warning or error messages that appear for both compile and runtime.
-
October 6th, 2011,08:44 PM #6
Re: Cannot find symbol class
Originally Posted by keang
It will also work with the package name if you set it up correctly. Can you post all the code and/or show how you are compiling the classes and running the app
Code:
//code for SimpleDotCom package dotGame; public class SimpleDotCom { int [] locationCells ; int numOfHits = 0; public void setLocationCells(int [] locs) { locationCells = locs; } public String checkYourself(String stringGuess) { int guess = Integer.parseInt(stringGuess); String result = "miss"; for (int cell : locationCells) { if (guess == cell) { result = "hit"; numOfHits++; break; } } if (numOfHits == locationCells.length) { result = "kill"; } System.out.println(result); return result; } }
Code:
//Code for GameHelper.java package dotGame; import java.io.* ; public class GameHelper { public String getUserInput(String prompt) { String inputLine = null; System.out.print (prompt + " "); try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); inputLine = is.readLine(); if (inputLine.length() == 0) { return null; } } catch (IOException e) { System.out.println("IOException: " +e); } return inputLine; } }
Code:
//Code for SimpleDotComGame.java package dotGame; public class SimpleDotComGame { public static void main(String[] args) { int numOfGuesses = 0; GameHelper helper = new GameHelper(); SimpleDotCom theDotCom = new SimpleDotCom(); int randomNum = (int)(Math.random() * 5); int [] locations = {randomNum, randomNum+1, randomNum+2}; theDotCom.setLocationCells(locations); boolean isAlive = true; while (isAlive == true) { String guess = helper.getUserInput("Enter a number"); String result = theDotCom.checkYourself(guess); numOfGuesses++; if (result.equals("kill")) { isAlive = false; System.out.println("You took " + numOfGuesses + " guesses"); } } } }
Originally Posted by keang
and also any warning or error messages that appear for both compile and runtime.
Code:
E:\dotGame>javac SimpleDotCom.java E:\dotGame>javac GameHelper.java E:\dotGame>javac SimpleDotComGame.java SimpleDotComGame.java:8: cannot find symbol symbol : class GameHelper location: class dotGame.SimpleDotComGame GameHelper helper = new GameHelper(); ^ SimpleDotComGame.java:8: cannot find symbol symbol : class GameHelper location: class dotGame.SimpleDotComGame GameHelper helper = new GameHelper(); ^ SimpleDotComGame.java:9: cannot find symbol symbol : class SimpleDotCom location: class dotGame.SimpleDotComGame SimpleDotCom theDotCom = new SimpleDotCom(); ^ SimpleDotComGame.java:9: cannot find symbol symbol : class SimpleDotCom location: class dotGame.SimpleDotComGame SimpleDotCom theDotCom = new SimpleDotCom(); ^ 4 errors
-
October 7th, 2011,05:55 AM #7
Re: Cannot find symbol class
When code is in a package you need to compile it from the root of the package ie:
Code:
E:>javac dotGame\SimpleDotComGame.java
Code:
E:\SRC>javac dotGame\SimpleDotComGame.java
-
October 7th, 2011,11:18 AM #8
Re: Cannot find symbol class
It's compiling fine now after your reply @keang. How do I run it btw?
-
October 7th, 2011,11:21 AM #9
Re: Cannot find symbol class
Code:
E:>java dotGame.SimpleDotComGame
-
October 7th, 2011,11:50 AM #10
Re: Cannot find symbol class
Error Cannot Find Symbol Class Solution
Source: https://forums.codeguru.com/showthread.php?517016-RESOLVED-Cannot-find-symbol-class