All things are lawful, but not all things are profitable.
javac docs wrote:The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file, but that provide information through inheritance.
...
When the compiler needs type information, it searches for a source file or class file that defines the type. The compiler searches for class files first in the bootstrap and extension classes, then in the user class path (which by default is the current directory). The user class path is defined by setting the CLASSPATH environment variable or by using the -classpath option
Jj Roberts wrote:It doesn't seem to be explicitly stated in the docs but javac expects the file path for the source files you are compiling. As Knute noticed, putting the source file's directory on the classpath doesn't work. Campbell's second and third examples won't work because of that. The java command expects <package>.<mainclass> as the argument, though, so we have to use -cp to run it from another directory.
Instead of
you would have to do
and if you want to use set CLASSPATH you would still have to pass javac the path of your .java file
Jj Roberts wrote:It may be worth mentioning that javac does search the classpath for other classes which your class depends on:
javac docs wrote:The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file, but that provide information through inheritance.
...
When the compiler needs type information, it searches for a source file or class file that defines the type. The compiler searches for class files first in the bootstrap and extension classes, then in the user class path (which by default is the current directory). The user class path is defined by setting the CLASSPATH environment variable or by using the -classpath option
Say I have two classes
main.java:Greeter.java:
From the '.' directory I can compile them like this (it might be better to use -sourcepath instead of -cp for clarity):
To run I have to have all of the classes on the classpath:
--class-path classpath, -classpath classpath, or -cp classpath
A semicolon (;) separated list of directories, JAR archives, and ZIP archives to search for class files.
Specifying classpath overrides any setting of the CLASSPATH environment variable. If the class path option isn’t used and classpath isn’t set, then the user class path consists of the current directory (.).
As a special convenience, a class path element that contains a base name of an asterisk (*) is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. A Java program can’t tell the difference between the two invocations. For example, if the directory mydir contains a.jar and b.JAR, then the class path element mydir/* is expanded to A.jar:b.JAR, except that the order of JAR files is unspecified. All .jar files in the specified directory, even hidden ones, are included in the list. A class path entry consisting of an asterisk (*) expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, is similarly expanded. Any class path wildcard expansion occurs before the Java VM is started. Java programs never see wildcards that aren’t expanded except by querying the environment, such as by calling System.getenv("CLASSPATH").
That's a pleasureJj Roberts wrote:. . . Thank you for the cow!
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |