1

This code compiles fine:

Person.java

package people;

class Person{
    private String name;
    private int age;

public Person(String name, int age){
    this.name = name;
    this.age = age;

}

public String getName(){
    return name;
}

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

public int getAge(){
    return age;
}

public void setAge(int age){
    this.age = age;
}

}

This does not:

controller.java

package people;
public class controller{
    public static void main (String[] args){

        Person mark = new Person();
        mark.setName("Mark");
        mark.setAge(24);

    }
}

I'm getting this compile error:

C:\Program Files\Java\jdk1.8.0_66\classes\controller.java:7: error: cannot find symbol
        Person mark = new Person();
        ^
  symbol:   class Person
  location: class controller
C:\Program Files\Java\jdk1.8.0_66\classes\controller.java:7: error: cannot find symbol
        Person mark = new Person();
                          ^
  symbol:   class Person
  location: class controller

I'm researched this quite a bit and can't seem to figure it out. Am I trying to instantiate the object incorrectly in my controller.java file? Thanks.

Logan
  • 33
  • 6
  • 2
    have you set classpath correct in your compile? You need to set it to include location of folder people including compiled Person.class – Jan Dec 02 '15 at 19:08

2 Answers2

0

Your class Person has a constructor that takes two parameters, a String and an int. However, you are trying to create a Person object using a constructor that takes no parameters:

Person mark = new Person();

You need to pass the parameters to the constructor:

Person mark = new Person("Mark", 24);

Ofcourse you don't need to call the set...() methods anymore after that.

The Java compiler automatically creates a constructor without parameters in a class, but only if you didn't explicitly add any constructor at all in your class.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • I tried that as well, I get the same "cannot find symbol" error. – Logan Dec 02 '15 at 19:13
  • What I described is definititely one thing you'll have to fix. If you still get the error, then there's more that's incorrect. One thing is: if you put classes in a package, they must be in a directory that matches the package structure. Your files must be in a dir named `people`. The commands you quoted show that you didn't do this. – Jesper Dec 02 '15 at 19:14
  • @Jasper is right! Fix your code first! Remove your explicit constructor from Person.java end then compile using `javac Person.java controller.java` – Milan Dec 02 '15 at 19:30
0

Run compiler from sources root. You must be in folder that contains "people" folder matching packager name.

Example. Java files in "D:\2\people", if current folder is "D:\2\people" and you run

"C:\Program Files (x86)\Java\jdk1.8.0_60\bin\javac.exe" controller.java

then you will get error that you describe.

But if you change current folder to "D:\2" and run

"C:\Program Files (x86)\Java\jdk1.8.0_60\bin\javac.exe" people\controller.java

all will be compiled. Don't forget to fix you constructors.

Rustam
  • 1,397
  • 1
  • 13
  • 17
  • I have the directory that contains the 'people' folder in my classpath, do I also need to add the 'people' folder to my classpath? – Logan Dec 03 '15 at 13:44