javaclassobject

ClassNotFound Error when compiling multiple object classes


I have a Store class which is an array of Person; I have a Person, Date, Student, Undergraduate and Postgraduate classes. Everything runs in Eclipse, and I have to run this from the Command Line. I've copied the src files and tried to run the main program, but it's just saying it can't find any of my classes. They are all in the same folder and in every class I have assigned the package. I've looked up importing classes and I tried:

    import oopinterface.Person;

etc for all classes.

Is there any way I can compile the main program so it will recognise the class files that are very clearly there!? I'm on Windows, but it would help if I could have the 'other' way for Linux?

This is the Compiler error:

   C:\Users\Liloka\Source\oopinterface>javac ContainerInterface.java
   ContainerInterface.java:41: cannot find symbol
   symbol  : class Store
   location: class oopinterface.ContainerInterface
            Store myList = new Store();

   //Instance of Store
                    ^
   ContainerInterface.java:688: cannot find symbol
   symbol  : class Person
   location: class oopinterface.ContainerInterface
            public Person getSupervisor()
                   ^

.. it goes on to pick at every method in other classes.. (39)


Solution

  • It looks like your classes are in the package oopinterface. Compile the source files from the base directory of the package, like this:

    C:\Users\Liloka\Source> javac oopinterface\ContainerInterface.java
    

    If you have the CLASSPATH environment variable set, then make sure that it is not set, or add the base directory of the package where the compiled class files can be found to the classpath. You can also use the -cp or -classpath option to tell javac where to find the compiled class files.

    Do all your source files have a package oopinterface; at the top of the file?

    After compiling, to run the program from the command line do something like this:

    java -cp C:\Users\Liloka\Source oopinterface.MainClass
    

    where MainClass is the class that contains the public static void main(String[] args) method. (This assumes that your compiled class files are in the same directory as the sources).