Dusk
Dusk

Reputation: 2201

Compilation error while compiling class within another

I have two classes Hello1 and Hello, and I'm calling class Hello1 constructor within Hello class, but when I trying to compile the Hello class with command

javac Hello.java

I'm 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 compile the class with command:

javac Hello.java Hello1.java

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

Upvotes: 2

Views: 312

Answers (2)

Raymond Roestenburg
Raymond Roestenburg

Reputation: 4890

You need to set the classPath with -cp .

Upvotes: 1

Adam Rosenfield
Adam Rosenfield

Reputation: 400166

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 which have already been compiled won't be seen by the compiler. To do this, compile like this:

javac Hello.java -cp .

Upvotes: 1

Related Questions