SupposedlySleeping
SupposedlySleeping

Reputation: 113

Javac does not recognize classes that are in the same directory

I am trying to compile my Java program which relies on external libraries. The program compiles without errors in the IDE. Upon compiling in a terminal, I am faced with the compiler not recognizing the code that I have written. All files are in the same directory. I am using a Makefile.

The following is the javac call and the resulting errors that arise. All Java and Jar files are in the current directory.

    $ javac -cp "./*" Driver.java
    Driver.java:22: error: cannot find symbol
            DataSetProcessor processor = new DataSetProcessor();
            ^
      symbol:   class DataSetProcessor
      location: class Driver
    Driver.java:22: error: cannot find symbol
            DataSetProcessor processor = new DataSetProcessor();
                                             ^
      symbol:   class DataSetProcessor
      location: class Driver
    Driver.java:29: error: cannot find symbol
    HashMap<Integer, Dealer> map = processor.getDealerVehicleRelationship(dataSetID, vehicleIDsList);
                     ^
      symbol:   class Dealer
      location: class Driver
    3 errors

Dealer.java & DataSetProcessor.java are in the current directory.

Can someone please explain to be what my problem is? I can't understand it.

Upvotes: 1

Views: 2050

Answers (2)

Gautam
Gautam

Reputation: 1932

You should do the following :

  • Put all your libraries in a directory called lib,
  • Put all the classes you create in the current directory.
  • For the classes you use for jars, you will need import statements
  • For the classes you create, you do not need import statements.
  • Use the following command to compile :

$ javac -cp "lib/*:." Driver.java

Upvotes: 3

SupposedlySleeping
SupposedlySleeping

Reputation: 113

My use of the -cp was wrong. I am using Mac; I'm not sure if that is the reason.

The final compiler call is now:

$ javac -cp lib/commons-io-2.6.jar:lib/json-20180813.jar:. Driver.java

and the call to run the program is:

$ java -cp lib/commons-io-2.6.jar:lib/json-20180813.jar:. Driver

Upvotes: 0

Related Questions