Re: Issues with package declarations
ankur wrote:
Then I did:
C:\Java Files>javac -d . NullTest.java
For giggles I tried javac with no -d, and it put the class file directly
in the current dir, with no directory for the package name. Weird.
Brenden@Homer ~/Dev/misc/javactest
$ javac Nada.java
Brenden@Homer ~/Dev/misc/javactest
$ ls
Nada.class Nada.java
Brenden@Homer ~/Dev/misc/javactest
$ javac -d . Nada.java
Brenden@Homer ~/Dev/misc/javactest
$ ls
Nada.class Nada.java testnada
I don't know exactly what's going on, but javac can also compile .java
files if it needs too. My theory is that it's confused, because
NullTest.java in in package forpackagetest, but it's at the root of the
classpath hierarchy, not in directory forpackagetest as the compiler
seems to expect. The fix is to move the .java file to the
forpackagetest directory.
Brenden@Homer ~/Dev/misc/javactest
$ javac -d . -cp . TestNada.java
TestNada.java:5: cannot access Nada
bad class file: .\Nada.java
file does not contain class Nada
Please remove or make sure it appears in the correct subdirectory of the
classpa
th.
Nada n = new Nada();
^
1 error
Brenden@Homer ~/Dev/misc/javactest
$ mv Nada.java testnada/
Brenden@Homer ~/Dev/misc/javactest
$ javac -d . -cp . TestNada.java
Brenden@Homer ~/Dev/misc/javactest
$ java -cp . TestNada
testnada.Nada@19821f
Weird, but there it is. So I guess you have to put source files in the
correct subdir also, or javac will complain.
Consider using separate source and .class trees (-sourcepath option in
addition to -d).