implicit classpath works, but explicit classpaths don't?
Consider the following java program:
import computer_package.Computer;
public class HelloWorld {
public static void main(String[] args) {
Computer computer = new Computer();
System.out.println("Hello, World");
}
}
and the following directory structure:
Rebs-MacBook-Pro:jalint2 rebcabin$ find computer_package/
computer_package/
computer_package//Computer.class
computer_package//Computer.java
wherein Computer.java is as follows:
package computer_package;
public class Computer {
public Computer() {
System.out.println("Constructor of Computer class.");
}
public void computer_method() {
System.out.println("Power gone! Shut down your PC soon...");
}
}
If I compile as follows, then all works well
$ javac -g HelloWorld.java
$ java HelloWorld
Constructor of Computer class.
Hello, World
Javac and java are able to find the class file
./computer_package/Computer.class without any further hints or annotation
or classpath-hacking (the implicit case)
However, if I compile in any of the following ways:
$ javac -g -cp "./computer_package" HelloWorld.java
$ javac -g -cp "./computer_package/" HelloWorld.java
$ javac -g -cp "./computer_package/*" HelloWorld.java
$ javac -g -cp "computer_package" HelloWorld.java
$ javac -g -cp "computer_package/" HelloWorld.java
$ javac -g -cp "computer_package/*" HelloWorld.java
all hell breaks loose; the previously acceptable Computer class is now
inaccessible:
Rebs-MacBook-Pro:jalint2 rebcabin$ make
javac -g -cp "./com/sun/jna/jna-4.0.0.jar:./computer_package" HelloWorld.java
HelloWorld.java:20: error: package computer_package does not exist
import computer_package.Computer;
^
HelloWorld.java:25: error: cannot access Computer
Computer computer = new Computer();
^
bad class file: ./computer_package/Computer.class
class file contains wrong class: computer_package.Computer
Please remove or make sure it appears in the correct subdirectory of
the classpath.
2 errors
make: *** [HelloWorld.class] Error 1
I know this is elementary; I am not a java expert and I admit I am trying
to guess my way around the classpath nightmare world; I am using just
emacs and bash, avoiding the IDEs that add even more layers of opaque
abstraction.
No comments:
Post a Comment