More on Java can't find symbol from .class in the same directory

2.2k views Asked by At

I defined 2 java files: Phone2.java and TestPhone2.java. Phone2.java compiles fine and is placed in a package named classes. TestPhone2 is supposed to create an instance of Phone2 but I cannot get TestPhone2.java to compile. Below is my directory structure:

The 2 java files are located here --

enter image description here

Phone2.class is located inside package classes --

enter image description here

My classpath is set to c:\myJavaProject.

The java files contain some simple codes from Mala Gupta to test instance and local variables. Here are the codes:

package classes;
class Phone2 {
    String phoneNumber = "123456789";
    void setNumber () {
        String phoneNumber;
        phoneNumber = "987654321";
    }
}


package classes;
class TestPhone2 {
    public static void main (String[] args) {
        Phone2 p1 = new Phone2();
        p1.setNumber();
        System.out.println(p1.phoneNumber);
    }
}

It is not clear to me why TestPhone2.java failed to compile. I reviewed some stackoverflow posts on this type of error (here and here) but the posts seem not to be directly on point. One of the posts dealt with static method and the other dealt with error stemming from an import or package statement. I don't think there is an error in my package statement because I used a similar package statement earlier today in the post here and there was no problem.

Below is my exact keystrokes.

enter image description here

Thanks for your feedback.

1

There are 1 answers

4
Elliott Frisch On BEST ANSWER

You have a package. So you must specify the classpath. One way to do that is to use the folder that contains your top level package (c:\myJavaProject\tint31\) like

set CLASSPATH=c:\myJavaProject\tint31\
javac -d . TestPhone2.java

or you might use -classpath like

javac -d . -classpath c:\myJavaProject\tint31\ TestPhone2.java