As far as I've been able to discern. The "cannot find symbol" compile error is a result of one of 2 mistakes.

1) The class name is spelled incorrectly
2) The class isn't in the same directory as the class trying to instantiate the object or the class isn't in the CLASSPATH variable.

I have a class called Item(already compiled in it's .class file), and it's in the same directory as Storefront.java. Storefront.java instantiates a new Item object, yet when I try to compile Storefront I get a "cannot find symbol error". Here's my bash commands to help illustrate the problem.

dan@dan-laptop:~/java/com/stonehambey/ecommerce$ ls
Item.class  Item.java  Storefront.java
dan@dan-laptop:~/java/com/stonehambey/ecommerce$ javac Storefront.java
Storefront.java:11: cannot find symbol
symbol  : class Item
location: class com.stonehambey.ecommerce.Storefront
		Item it = new Item(id, name, price, quant);
		^
Storefront.java:11: cannot find symbol
symbol  : class Item
location: class com.stonehambey.ecommerce.Storefront
		Item it = new Item(id, name, price, quant);
		              ^
2 errors
dan@dan-laptop:~/java/com/stonehambey/ecommerce$

Obviously I'm missing something about how packages and classpaths work, but I don't know what. Any help would be much appreciated. :)

Recommended Answers

All 10 Replies

Do you have "package" declaration of your Item class?

The Item class is in package com.stonehambey.ecommerce

Not the location of the file, but does the class Item starts with

package com.stonehambey.ecommerce

???

Sorry, yes it does :)

Ohh, little slower today.
In your class Item, did you declare constructor that will initialize this class variable with 4 provided values? Something like this?

public Item(int id, String name, double price, int quant){
    itemID = id;
    itemName = name;
    itemPrice = price;
    itemQuant = quant;
}

I see nothing wrong with it. Do you mind to post Storefront.java?

sure

http://paste.ubuntu.com/94527/

Although here is something interesting. While I've been posting I've been messing with the files in eclipse. Now eclipse builds as I go and when I navigated to the directory I see a Storefront.class sitting there. Eclipse must've managed to build it somehow. However the JDK javac is still reporting the same error. Strange (although I'm pretty new to Java so maybe I'm missing something...)

That explain a lot. If you use command line to compile it you need to do it as follows

javac com/stonehambey/ecommerce/Storefront.java

where both files have to be in respectable locations and you need to run javac from the directory that is parent to your "com" so in your case judging by pasted console output in java directory

dan@dan-laptop:~/java$ javac /com/stonehambey/ecommerce/Storefront.java

Ah, so that's what I was doing wrong. My thanks to you, sir :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.