0

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.

Community
  • 1
  • 1
sedeh
  • 7,083
  • 6
  • 48
  • 65
  • Although I respect you wanting to "Rambo" your way through development by hunting for your own food in the jungle etc (ie manually dealing with classpath and build issues), I strongly recommend using an IDE (ie use the McDonands in town) – Bohemian Nov 11 '14 at 04:27

1 Answers1

1

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
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Worked like charm! From Sotirios Delimanolis comment in my post earlier today [here](http://stackoverflow.com/questions/26852459/classpath-seems-correct-but-jvm-still-could-not-find-or-load-main-class/26852695#26852695), he had mentioned I need not put `..\mobile` in the `classpath` which in this case translates to `..\tint31`. Now I am confused as to the correct rule of thumb. – sedeh Nov 11 '14 at 04:29
  • @sedeh Sorry. But no, you changed *mobile* to *classes*. And `C:\myJavaProject` to `C:\myJavaProject\tint31`. Java packages are effectively mapped to directories (or stored as directories stored in archive files based on zip, called jars,ears and/or wars). – Elliott Frisch Nov 11 '14 at 04:33
  • Ok. I see. In this case, I've an additional level in my directory structure. So I need to include the level just before the target folder. In this case, I need to include up to `..\tint31`. Correct? – sedeh Nov 11 '14 at 04:38
  • @sedeh Unless they have changed the meaning of *worked like [a] charm*. The important thing is you understand it. – Elliott Frisch Nov 11 '14 at 04:40