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
locatio开发者_JS百科n: 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.
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 .
You need to set the classPath with -cp .
精彩评论