Search code examples
javacompiler-errorspackageclasspathjavac

Cannot find symbol: javac no longer compiles my driver file when classes are placed in a package


noob here.

I have a group of class files which are used by a driver file. I originally wrote the files without any package statement and compiled the files using:

javac -d classes src/Driver.java

Driver compiled without a hitch (though I had to add the path to the src directory in my .bash_profile CLASSPATH variable first)

Next, I added a package statement at the top of all my src files. The statement had the form:

package edu.school.course.project;

Every src file got the same package statement. However the javac command now yields 62 "cannot find symbol" errors when executed. Every Driver.java reference to another class file in my src directory fails.

I tried adding this import statement to each class file that extends or implements some parent class in the package:

import edu.school.course.project.*;

Still, javac yields 62 "cannot find symbol" errors.

Do I need to modify my CLASSPATH variable to include the package? What am I doing wrong?


Solution

  • An alternative is create a directory to hold your Driver class. For instance:

    mkdir -p src/edu/school/course/project
    mv src/Driver.java src/edu/school/course/project
    

    Then compile it:

    javac -d classes src/edu/school/course/project/Driver.java
    

    You can put all your other related Java files in that same directory.

    Assuming Driver has a main() method, to run it:

    java -cp classes/ edu.school.course.project.Driver