Can't find symbol error in cmd but not in IDE

I am trying to compile the following code (one of the two files needed to do this homework), but I am getting 2 errors in cmd. This is what the cmd throws me:

CarRentalTest.java:12: error: cannot find symbol
        CarRental myCarRental = new CarRental(); //create CarRental object CarRental
        ^
  symbol:   class CarRental
  location: class CarRentalTest
CarRentalTest.java:12: error: cannot find symbol
        CarRental myCarRental = new CarRental(); //create CarRental object CarRental
                                    ^
  symbol:   class CarRental
  location: class CarRentalTest
2 errors

      

And this is the code I am trying to compile.

public class CarRentalTest {

    public static void main (String[] args)
    {
        CarRental myCarRental = new CarRental(); //create CarRental object CarRental
        myCarRental.Customers();

    } //end method main

} //end class CarRentalTest

      

What's strange is that everything works great in NetBeans. What am I doing wrong here?: 9

+3


source to share


3 answers


What am I doing wrong here?

Don't build CarRental

, or tell the compiler where to find the class if you've already compiled it. The IDE probably assumes you want to build everything, so great.

We don't know how your code is organized, but you must either pass all the appropriate filenames to the compiler at the same time:



javac -d classes src\CarRental.java test\CarRentalTest.java

      

... or put the earlier compilation output directory in the classpath for later compilation, for example

javac -d classes src\CarRental.java
javac -d testclasses -cp classes test\CarRentalTest.java

      

+6


source


If you are using the default directory layout for your project where production code and test code are in separate directory trees, then the java command line will not show the production class if your currect directory is a test directory.

To clarify: Suppose you have this structure:

src/
  main/
    java/
      mypackage/
        CarRental.java
  test/
    java/
      mypackpage/
        CarRentalTest.java

      



and you are in the 'src / test / java / mypackage /' directory, you would encounter this error when running javac

on the command line - although the production and test classes are in the same package, they are in different directories.

The IDE is aware of this directory structure, includes the test path at compile time, and therefore works fine.

+1


source


You need to import the CarRental class into CarRentalTest.

import yourpackage.CarRental into CarRentalTest. Java compiler cannot find CarRental in CarRentalTest.java.

In IDE package the whole package comes in java file

    import package.car.*;

      

This is why it works in the IDE.

0


source







All Articles