0% found this document useful (0 votes)
61 views7 pages

Class-Path: Classpath in 5-Minutes

The document discusses classpaths, which are lists of directories that the Java compiler (javac) and runtime (java) use to locate classes. Classpaths can be specified as environment variables or command line options. When searching for classes, javac and java will first check standard Java locations, then classpath directories from left to right. For a class in a package, its package root directory must be included. JAR files can also be included in classpaths and are searched like directory trees with the JAR name specified.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views7 pages

Class-Path: Classpath in 5-Minutes

The document discusses classpaths, which are lists of directories that the Java compiler (javac) and runtime (java) use to locate classes. Classpaths can be specified as environment variables or command line options. When searching for classes, javac and java will first check standard Java locations, then classpath directories from left to right. For a class in a package, its package root directory must be included. JAR files can also be included in classpaths and are searched like directory trees with the JAR name specified.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CLASS-PATH

classpath in 5-Minutes

javac

javac [options] [source files]

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.

java -DmyProp=myValue MyClass x 1


 DmyProp=myValue: Create a system property myProp and set its
value to myValue.
 MyClass x 1: launch the file MyClass.class and send it two String
arguments whose values are x and 1

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.

Classpaths (cp) – lists of directories in which classes might be


found. There are two places where classpaths can be declared:
 As an operating system environment variable.
 Classpaths declared as command-line options.

In order to find a class in a package, you have to have a directory in


your classpath that has the package's leftmost entry (the package's
"root" i.e. com) as a subdirectory.

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

The structural overview for javac:

javac [options] [source files]

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

The second invocation passes the compiler two options

 classpath which itself has an argument of com:. and


 g, and passes the compiler two .java files to compile.

Whenever you specify multiple options and/or files they should be


separated by spaces.

Compiling with -d [d is for destination]

By default, the compiler puts a .class file in the same directory as


the .java source file. The -d option lets you tell the compiler in
which directory to put the .class file(s) it generates.

Let's say you have the following directory structure

myProject/source/MyClass.java
myProject/classes [generated class should go here]

Assuming that MyClass does not have a package statement;

cd myProject
javac -d classes source/MyClass.java

Quick look at how packages work in relationship to the -d option.

Suppose we have the following .java file in the following directory


structure:
package com.wickedlysmart;

public class MyClass { }

myProject/source/com/wickedlysmart/MyClass.java
myProject/classes/com/wickedlysmart/ [MyClass.class goes here]

If you were in the source directory, you would compile MyClass.java


and put the resulting MyClass.class file into the classes/com
/wickedlysmart directory by invoking the following command:

Cd myProject/source
javac -d ../classes com/wickedlysmart/MyClass.java

Because MyClass.java is in a package, the compiler knew to put the


resulting .class file into the

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.

If the destination directory[i.e. classes, which is not the part of


package] doesn't exist, you'll get a compiler error.

java:5: error while writing MyClass: classes/MyClass.class (No such


file or directory)

java

The java command is used to invoke the JVM. The structure of this
command.

java [options] class [args]

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

java -DmyProp=myValue MyClass x 1

This command can be read as

 DmyProp=myValue: Create a system property myProp and set its


value to myValue.
 MyClass x 1: launch the file MyClass.class and send it two
String arguments whose values are x and 1

Searching for Other Classes

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

They are lists of directories in which classes might be found.

There are two places where classpaths can be declared:


 As an operating system environment variable. The classpath declared
here is used by default, whenever java or javac are invoked.
 As a command-line option for either java or javac.
Classpaths declared as command-line options override the
classpath declared as an environment variable.
But they persist only for the length of the invocation.

Classpaths consist of a variable number of directory locations,


separated by delimiters. For example in Unix-based operating systems:
-classpath /com/foo/acct:/com/foo
 Forward slashes are used to construct directory locations, and
 The separator is the colon (:).

Remember, that when you specify a subdirectory, you're NOT specifying


the directories above it. For instance, in the preceding example the
directory /com will NOT be searched.

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.

Packages and Searching

When you start to put classes into packages, and then start to use
classpaths to find these classes, things can get tricky.

Let's start off by reviewing packages.

package com.foo;
public class MyClass { public void hi() { } }

The fully qualified name of the class is now com.foo.MyClass. Once a


class is in a package, the package part of its fully qualified name is
atomic —it can never be divided. You can't split it up on the command
line, and you can't split it up in an import statement.

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.

Let’s take a look at how package work in conjunction with classpaths


and command lines.

When you're searching for a class using its fully qualified name, that
fully qualified name relates closely to a specific directory
structure.

For instance, relative to your current directory com.foo.MyClass would


have to be located at com/foo/MyClass.class.

An important concept

In order to find a class in a package, you have to have a directory in


your classpath that has the package's leftmost entry (the package's
"root" i.e. com) as a subdirectory.

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:

 The jar command creates the META-INF directory automatically.


 The jar command creates the MANIFEST.MF file automatically.
 The jar command won’t place any of your files in META-INF/.
 java and javac will use the JAR like a normal directory tree.

Finding a JAR file using a classpath is similar to finding a package


file in a classpath. The difference is that when you specify a path
for a JAR file, you must include the name of the JAR file at the end
of the path.

You might also like