7

Am new at Java and this is what am attempting to do;

I Have two files located on this folder on a windows machine;

d:\programs\sims\javasim\src\com\jsim\
Person.java
Building.java

On my Building.java am making use of class Person located in file Person.java i.e.

package com.jsim;
ArrayList<Person> personList = new ArrayList<Person>();

Am compiling the files from this folder

d:\programs\sims\javasim\src

But when i try to compile Building.Java, the compiler tells me

d:\programs\sims\src\javac com/jsim/Building.java

com\jsim\Building.java:10: cannot find symbol
symbol  : class Person
location: class com.jsim.Building
        private ArrayList<Person>personList = new ArrayList<Person>();
                          ^

How can i make Building.java know about class Person in file Person.java?

Gath

7
  • 4
    Both classes have the same package? (package com.jsim;) Commented Apr 21, 2011 at 15:49
  • Please provide the full source of classes Person and Building. Commented Apr 21, 2011 at 15:54
  • @MByD That's what you would assume from the file structure Gath shows, but then the compiler reports location: class com.liftsim.Building which is a different package. I don't know if it's a typo in the question or what. We'll have to wait for an update. Commented Apr 21, 2011 at 15:55
  • I have edited the compiler error. com\jsim Commented Apr 21, 2011 at 16:00
  • 1
    d:\programs\sims\src\javac com/jsim/Building.java com/jsim/Person.java Commented Apr 21, 2011 at 16:55

5 Answers 5

4

execute javac *.java or javac Person.java Building.java or javac Building.java Person.java to compile your classes.

Seems like Person.java is not compiled before compiling Building.java file.

Building needs Person's class file for compiling instead of .java file.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to get your package names and imports to be consistent. The information you posted contains two different package names, so you either need to put the classes in the same package or add an import statement.

Comments

0

You must import the class you're referencing:

import Person;

1 Comment

I disagree - it looks like he's using the same package name (com.jsim) which means you don't need to import those classes as they're in the same package.
0

I think the problem is indeed with this like:

private ArrayList<Person>personList = new ArrayList<com.liftsim.Person>();

Since the class you are writing is in the same package as the others (com.jsim) you don't need to import anything. However, you have specified a totally different class name in your initialisation it seems (I'm guessing using the Eclipse autocomplete?) -- so simply remove all the imports and re-write the above line as follows:

private ArrayList<Person>personList = new ArrayList<Person>();

5 Comments

Re-edited my question, looks exactly as you state but still cant compile.
do you compile from the command line? if so, since you are using package names, you will have to use the -d parameter to specify destination folder -- in order to generate .class files together with directory (package) structures -- like this: javac -d out/
Tried this but failed javac -d ../classes com/jsim/Building.java i have the necessary destination folder (classes) already created.
which directory is Building.java in ?
d:\programs\sims\javasim\src\com\jsim\
0

I discovered that when I am using Eclipse, I can't use -non-public- classes inside other files in the same package. Note that I am using the default package while this happens. This behavior changes when I open both files and have two windows open in the editor. In that case, I CAN use classes in other files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.