aspose file tools*
The moose likes Beginning Java and the fly likes cannot find symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot find symbol" Watch "cannot find symbol" New topic
Author

cannot find symbol

Sami Devine
Ranch Hand

Joined: Jan 30, 2011
Posts: 44
Hi, I am working on an excercise about access control. I have the following as a single file. During compilation I get the error "cannot find symbol" method:No1.



Kyle Kroboth
Greenhorn

Joined: Apr 26, 2011
Posts: 2

The problem is in the main method in the Noose class. you creating a new object of Noose and calling the method No1() but there is no No1() method in the Noose Class. you need to create in instance of Monkey to access those methods.

So it looks like this.

Sami Devine
Ranch Hand

Joined: Jan 30, 2011
Posts: 44
Hi Kyle, thanks I had actually tried a few things. I tried by changing the code as follows:



I renamed the program as Noose.java When I do a java Noose, I get NoClassDef found error - wrong name moose/Noose. javac Noose.java compiles fine.
Deep Purohit
Greenhorn

Joined: Mar 29, 2010
Posts: 9

You seem to have caught into the regular classpath problem. Check out - http://download.oracle.com/javase/tutorial/getStarted/problems/index.html
If the problem persist then revert.
Peeyush Bhadola
Greenhorn

Joined: Jan 23, 2011
Posts: 10

Hello Sami, I think you forgot to set the CLASSPATH variable, which is the most important activity to be performed while playing with packages. Here are the steps that can avoid NoClassDefFoundError :

Step 1. Create your program. (In your case it is Noose.java)

Step 2. Now put this file in a folder with name same as your package name. (In your case, the package name is moose). So you need to create a directory or folder with folder name as moose, and then put your noose.java inside this folder.

Step 3. [Main Step] Now set the CLASSPATH. Consider you are working on Windows and your newly created folder moose is present under C:\ directory. If this is the case then you need to set CLASSPATH using command prompt as: set CLASSPATH=.;C:\;
When you set classpath, Java will search our file in C-directory and also in current directory. This is because I have used .(dot) for current directory and C:\ for C directory.

Step 4. Compile the java file
C:\moose>javac Noose.java

Step 5. Run your program
C:\moose>java moose.Noose
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Peeyush Bhadola wrote:Hello Sami, I think you forgot to set the CLASSPATH variable, which is the most important activity to be performed while playing with packages. . . .
No, setting the classpath is not at all important. You should almost certainly NOT set a system classpath. A system classpath can cause more trouble than it solves. Note the advice given in that Java™ Tutorials link was to unset the classpath.

You should be executing your application from the root of the package structure. so if you have a class called moose.Noose and the moose directory is inside the myJava folder, then you should set your command line to the myJava folder
c:\ myJava:> javac moose/Noose.java
c:\myJava:> java moose.Noose
If there is no system classpath, then the java tool will use "." as its classpath.

. . . and welcome to the Ranch
Peeyush Bhadola
Greenhorn

Joined: Jan 23, 2011
Posts: 10

@Campbell - Yes ... I was wrong. Thanks
Sami Devine
Ranch Hand

Joined: Jan 30, 2011
Posts: 44
Hi Campbell, this still does not work. I have set the command prompt to my package C:\Java\Moose>. To clarify, there is a moose folder in the Java folder. The .java file is in the moose folder.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Sorry I didn't look at your query last night.

All right, so I am not using Windows®, but it would work exactly the same way on any operating system. And I have no classpath set
[campbell@campbell1110 ~]$ cd java
[campbell@campbell1110 java]$ mkdir moose
[campbell@campbell1110 java]$ cd moose
[campbell@campbell1110 moose]$ gedit Noose.java
[campbell@campbell1110 moose]$ cd ..
[campbell@campbell1110 java]$ javac moose/Monkey.java
javac: file not found: moose/Monkey.java
Usage: javac <options> <source files>
use -help for a list of possible options
[campbell@campbell1110 java]$ javac moose/Noose.java
[campbell@campbell1110 java]$ java moose.Noose
This is monkey
[campbell@campbell1110 java]$
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cannot find symbol