Class-Path: Classpath in 5-Minutes
Class-Path: Classpath in 5-Minutes
classpath in 5-Minutes
javac
Cd myProject/source
pkg com/wickedlysmart
javac -d ../classes com/wickedlysmart/MyClass.java
compiler will put the resulting .class file into the
classes/com/wickedlysmart directory.
For any given class, the JVM will need to find exactly the same
supporting classes that the javac compiler needed to find at
compilation time.
The first place they look is in the directories that contain the
classes that come standard with J2SE.
The second place they look is in the directories defined by
classpaths.
When you specify a path for a JAR file, you must include the name of
the JAR file at the end of the path.
classpath -Details
javac
Both the [options] and the [source files] are optional parts of the
command, and both allow multiple entries. For example:
javac -help
javac -classpath com:. -g Foo.java Bar.java
myProject/source/MyClass.java
myProject/classes [generated class should go here]
cd myProject
javac -d classes source/MyClass.java
myProject/source/com/wickedlysmart/MyClass.java
myProject/classes/com/wickedlysmart/ [MyClass.class goes here]
Cd myProject/source
javac -d ../classes com/wickedlysmart/MyClass.java
classes/com/wickedlysmart directory.
Amazingly, the compiler will build two directories called com and
com/wickedlysmart [if they dont exist] in order to put the resulting
MyClass.class file into the correct package directory
(com/wickedlysmart/) which it builds within the existing .../classes
directory.
java
The java command is used to invoke the JVM. The structure of this
command.
The [options] and [args] parts of the java command are optional, and
they can both have multiple values. You must specify exactly one class
file to execute, and the java command assumes you're talking about
a .class file, so you don't specify the .class extension on the
command line.
You need to understand the -classpath (and its twin -cp) and -D
options
When we use the java and javac commands, we want these commands to
search for other classes that will be necessary to complete the
operation. The most obvious case is when classes we create use classes
that Sun provides with J2SE (now sometimes called Java SE), for
instance when we use classes in java.lang or java.util. The next
common case is when we want to compile a file or run a class that uses
other classes that have been created outside of what Sun provides, for
instance our own previously created classes.
Remember that for any given class, the java virtual machine will need
to find exactly the same supporting classes that the javac compiler
needed to find at compilation time. In other words, if javac needed
access to java.util.HashMap then the java command will need to find
java.util.HashMap as well.
Both java and javac use the same basic search algorithm:
They both have the same list of places (directories) they search,
to look for classes.
They both search through this list of directories in the same
order.
As soon as they find the class they're looking for, they stop
searching for that class. In the case that their search lists
contain two or more files with the same name, the first file
found will be the file that is used.
The first place they look is in the directories that contain the
classes that come standard with J2SE.
The second place they look is in the directories defined by
classpaths.
Classpaths – A class search paths
When searching for class files, the java and javac commands don't
search the current directory by default. You must tell them to search
there. The way to tell java or javac to search in the current
directory is to add a dot (.) to the classpath:
-classpath /com/foo/acct:/com/foo:.
Remember, we're talking about class files— when you're telling javac
which .java file to compile, javac looks in the current directory by
default.
It's also important to remember that classpaths are searched from left
to right. Therefore in a situation where classes with duplicate names
are located in several different directories in the following
classpaths, different results will occur:
-classpath /com:/foo:. is not the same as -classpath .:/foo:/com
Finally, the java command allows you to abbreviate -classpath with
-cp. The Java documentation is inconsistent about whether the javac
command allows the -cp abbreviation. On most machines it does, but
there are no guarantees.
When you start to put classes into packages, and then start to use
classpaths to find these classes, things can get tricky.
package com.foo;
public class MyClass { public void hi() { } }
Once you've defined the fully qualified name, you can use the "alias"
[for ex, through import] in your code—but the alias is referring back
to the fully qualified name.
When you're searching for a class using its fully qualified name, that
fully qualified name relates closely to a specific directory
structure.
An important concept
JAR
All of the classes in the JAR file can be accessed, via classpaths, by
java and javac, without ever unJARing the JAR file.
some rules concerning the structure of JAR files: