File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes cannot find symbol error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot find symbol error" Watch "cannot find symbol error" New topic
Author

cannot find symbol error

remco Brand
Greenhorn

Joined: Jun 20, 2010
Posts: 5
I'm working on the Web Component Developer Exam and trying to work out a simple example for usage within JSP

I'm trying to create an abstract class Person and a Employee class that extends Person

Both java-files in the same package and I'm also creating the classes there. How simple can it be?
But either my java got very rusty or java is just too dumb

The person class is created, but the employee is not.
I got the error

Employee.java:23: cannot find symbol
symbol: class Person
public class Employee extends Person {

To my knowledge it's not even needed because they are in the same package (and directory)
but even when I use -classpath directory it fails to compile
When I remove the extends clause from Employee.java it compiles.

Very discouraging that even the simplest of the simplest goes wrong.
java version is 1.6.0_11

Here are the classes:

Person:

package foo;

public abstract class Person {
private String name;

/* public Person() {
name = "Initial value";
}*/

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

}

Employee class

package foo;

import foo.*;

public class Employee extends Person {
private int empID;

public Employee() {
empID = -1;
}

public int getEmpID() {
return this.empID;
}

public void setEmpID(int id) {
this.empID = id;
}

}






Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I assume you are attempting to compile from a command line? If so, please post the command you used. Also please use code tags when you post. I suspect you have a classpath issue.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

compile first Person class as

after that compile Employee class as


Life is easy because we write the source code.....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Your classpath is wrong.

Also, please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button. Thanks!
remco Brand
Greenhorn

Joined: Jun 20, 2010
Posts: 5
thanks for your quick answers:

In answer to Tom:

Yes I tried it from the command line.I'm using Windows XP

The classes and java-files are in
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes\foo

Documentation from Sun (http://java.sun.com/j2se/1.3/docs/tooldocs/win32/javac.html)
states that when you don't specifiy classpath it looks in the current directory

That didn't work, the documentation doesn't seem correct?
So I gave the full classpath (the directory where I'm currently standing in by the way)

javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes\foo" Employee.java

Same error

I tried it with

javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes\foo\" Employee.java

it gave me the following error/message

javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

I tried it with
javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes\foo";. Employee.java

(this should add the current directory to the classpath, isn't it?)
Same result

As for the suggestion of Pramod

When I first do

javac -d . Employee.java
(with Person.class and Person.java already present in the directory)
it gives me the same error

But indeed when I first do

javac -d . Person.java

and then

javac -d . Employee.java

it compiles!
This behavoir really puzzles me.

Getting these simple classes to compile did cost me about 3 hours of valuable preparation time for the exam
I have found the behavoir of the javac compile command also in past experiences very user-unfriendly and often very different from what I would expect. But I guess I'm the only one here....












David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Unless you're using Java 1.3, don't use Java 1.3 documentation.
remco Brand
Greenhorn

Joined: Jun 20, 2010
Posts: 5
ok, ok, ok, here you go:

http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html

As you would expect the behavoir stayed the same according to the documentation. if not specified, it looks in the current directory.
At least according to the documentation.

In the mean-time I found that

the command

javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes\" Employee.java

also compiles the class. So the classpath apperantly has to be given to the root of the package-structure
I still find it silly and counter-intuitive that the java-compiler doesn't look in the current directory for the classes


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In your original example the source file was in your current directory, but was in package foo. If you had just gone up a single level and used "." as the classpath (plus whatever else you need) it would have worked fine. Now it looks like you're using deployed classes, which is fine, but somewhat over-complicating things.
remco Brand
Greenhorn

Joined: Jun 20, 2010
Posts: 5
I understand now a bit better how the compiler thinks/work
in this case
java -cp .. Employee.java also works
and then going one directory higher
java foo\Employee.java also does the trick.

And I still think the way java and class files are handled by the compiler is somewhat inconsistent

When I go one directory and type

javac foo.Employee.java
it gives me an error that the file is not found
But the compiler does look for the class foo.person from this place

I still find it counter-intuitive that the java-compiler doesn't look in the current directory (C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes\foo) for the classes it needs
But I guess I have to come to terms with this behavoir

I didn't understand what you mean with deployed classes?




David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

remco Brand wrote:I still find it counter-intuitive that the java-compiler doesn't look in the current directory (C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJSP\WEB-INF\classes\foo) for the classes it needs

But Java uses a hierarchical package structure, and you've defined the class to be in package "foo"--that's just the way it is.

Are you keeping your source files in the Tomcat distribution directory?!
remco Brand
Greenhorn

Joined: Jun 20, 2010
Posts: 5
No, I don't use the distribution
Just the old handwork

Thanks for your answers.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You didn't actually answer my question--right now it looks like you're keeping your source files in the Tomcat distribution directory.

Where are your source files located?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cannot find symbol error