Compilation error when compiling a class inside another

I have two classes Hello1 and Hello, and I call the Hello1 class constructor in the Hello class, but when I try to compile the Hello class with the command

javac Hello.java

I am getting compile time error:

Hello.java:6:cannot find the symbol
symbol: class Hello1
location: class Hello
Hello1=new Hello();
^
Hello.java:6:cannot find the symbol
symbol: class Hello1
location: class Hello
Hello1=new Hello();
           ^

      

But when I try to compile the class with the command:

javac Hello.java Hello1.java

it works fine, but why should I use this command to compile the class every time? Why can't the compiler use the already compiled .class file Hello1, so next time I will use the javac Hello.java command.

+2


source to share


2 answers


You need to add the current directory to your classpath so the compiler can find it. By default, the classpath does not include the current working directory, so any .class files that have already been compiled will not be considered by the compiler. To do this, do the following compilations:



javac Hello.java -cp .

      

+1


source


You need to set classPath with -cp.



+1


source







All Articles