UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

78 lines (77 loc) 12.2 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>The Classpath</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="The Classpath"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-3-SECT-3"/>The Classpath</h1></div></div></div><p><a id="idx10113" class="indexterm"/> <a id="idx10126" class="indexterm"/> The concept of a <a id="I_indexterm3_id644882" class="indexterm"/><span class="emphasis"><em>path</em></span> should be familiar to anyone who has worked on a DOS or Unix platform. It’s an environment variable that provides an application with a list of places to look for some resource. The most common example is a path for executable programs. In a Unix shell, the <a id="I_indexterm3_id644897" class="indexterm"/><code class="literal">PATH</code> environment variable is a colon-separated list of directories that are searched, in order, when the user types the name of a command. The Java <a id="I_indexterm3_id644909" class="indexterm"/><code class="literal">CLASSPATH</code> environment variable, similarly, is a list of locations that are searched for Java class files. Both the Java interpreter and the Java compiler use the <code class="literal">CLASSPATH</code> when searching for packages and Java classes.</p><p>An element of the classpath can be a directory or a <a id="I_indexterm3_id644930" class="indexterm"/>JAR file. Java also supports archives in the conventional ZIP format, but JAR and ZIP are really the same format. JARs are simple archives that include extra files (metadata) that describe each archive’s contents. JAR files are created with the JDK’s <a id="I_indexterm3_id644943" class="indexterm"/><span class="emphasis"><em>jar</em></span> utility; many tools for creating ZIP archives are publicly available and can be used to inspect or create JAR files as well. The archive format enables large groups of classes and their resources to be distributed in a single file; the Java runtime automatically extracts individual class files from the archive as needed.</p><p>The precise means and format for setting the classpath vary from system to system. On a Unix system (including Mac OS X), you set the <code class="literal">CLASSPATH</code> environment variable with a colon-separated list of directories and class archive files:</p><a id="I_3_tt82"/><pre class="programlisting"> <code class="o">%</code><strong class="userinput"><code><code class="n">export</code> </code></strong><strong class="userinput"><code><code class="n">CLASSPATH</code><code class="o">=/</code><code class="n">home</code><code class="o">/</code><code class="n">vicky</code><code class="o">/</code><code class="n">Java</code><code class="o">/</code><code class="nl">classes:</code><code class="o">/</code><code class="n">home</code><code class="o">/</code><code class="n">josh</code><code class="o">/</code><code class="n">lib</code><code class="o">/</code><code class="n">foo</code><code class="o">.</code><code class="na">jar</code><code class="o">:.</code></code></strong></pre><p>This example specifies a classpath with three locations: a directory in the user’s home, a JAR file in another user’s directory, and the current directory, which is always specified with a dot (<code class="literal">.</code>). The last component of the classpath, the current directory, is useful when you are tinkering with classes.</p><p>On a Windows system, the <code class="literal">CLASSPATH</code> environment variable is set with a semicolon-separated list of directories and class archive files:</p><a id="I_3_tt83"/><pre class="programlisting"> <code class="nl">C:</code><code class="err">\</code><code class="o">&gt;</code> <strong class="userinput"><code><code class="n">set</code> <code class="n">CLASSPATH</code><code class="o">=</code><code class="nl">C:</code><code class="err">\</code><code class="n">home</code><code class="err">\</code><code class="n">vicky</code><code class="err">\</code><code class="n">Java</code><code class="err">\</code><code class="n">classes</code><code class="o">;</code><code class="nl">C:</code><code class="err">\</code><code class="n">home</code><code class="err">\</code><code class="n">josh</code><code class="err">\</code><code class="n">lib</code><code class="err">\</code><code class="n">foo</code><code class="o">.</code><code class="na">jar</code><code class="o">;.</code></code></strong></pre><p>The Java launcher and the other command-line tools know how to find the core classes, which are the classes included in every Java installation. The classes in the <code class="literal">java.lang</code>, <code class="literal">java.io</code>, <code class="literal">java.net</code>, and <code class="literal">javax.swing</code> packages, for example, are all core classes so you do not need to include these classes in your classpath.</p><p><a id="I_indexterm3_id645042" class="indexterm"/> <a id="I_indexterm3_id645050" class="indexterm"/>The classpath may also include “*” wildcards that match all JAR files within a directory. For example:</p><a id="I_programlisting3_id645063"/><pre class="programlisting"><code class="n">export</code> <code class="n">CLASSPATH</code><code class="o">=/</code><code class="n">home</code><code class="o">/</code><code class="n">pat</code><code class="o">/</code><code class="n">libs</code><code class="o">/*</code></pre><p>To find other classes, the Java interpreter searches the elements of the classpath in order. The search combines the path location and the components of the fully qualified class name. For example, consider a search for the class <code class="literal">animals.birds.BigBird</code>. Searching the classpath directory <span class="emphasis"><em>/usr/lib/java</em></span> means the interpreter looks for an individual class file at <span class="emphasis"><em>/usr/lib/java/animals/birds/BigBird.class</em></span>. Searching a ZIP or JAR archive on the classpath, say <span class="emphasis"><em>/home/vicky/myutils.jar</em></span>, means that the interpreter looks for component file <span class="emphasis"><em>animals/birds/BigBird.class</em></span> within that archive.</p><p>For the Java runtime, <span class="emphasis"><em>java</em></span>, and the Java compiler, <a id="I_indexterm3_id645102" class="indexterm"/><span class="emphasis"><em>javac</em></span>, the classpath can also be specified with the <em class="replaceable"><code>-classpath</code></em> option:</p><a id="I_3_tt84"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">javac</code> <code class="o">-</code><code class="n">classpath</code> <code class="o">/</code><code class="n">home</code><code class="o">/</code><code class="n">pat</code><code class="o">/</code><code class="nl">classes:</code><code class="o">/</code><code class="n">utils</code><code class="o">/</code><code class="n">utils</code><code class="o">.</code><code class="na">jar</code><code class="o">:.</code> <code class="n">Foo</code><code class="o">.</code><code class="na">java</code></code></strong></pre><p>If you don’t specify the <a id="I_indexterm3_id645135" class="indexterm"/><code class="literal">CLASSPATH</code> environment variable or command-line option, the classpath defaults to the current directory (<code class="literal">.</code>); this means that the files in your current directory are normally available. If you change the classpath and don’t include the current directory, these files will no longer be accessible.</p><p>We suspect that about 80 percent of the problems that newcomers have when first learning Java are classpath-related. You may wish to pay particular attention to setting and checking the classpath when getting started. If you’re working inside an IDE, it may remove some or all of the burden of managing the classpath. Ultimately, however, understanding the classpath and knowing exactly what is in it when your application runs is very important to your long-term sanity. The <span class="emphasis"><em>javap</em></span> command, discussed next, can be useful in debugging classpath issues.</p><div class="sect2" title="javap"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-3-SECT-3.1"/>javap</h2></div></div></div><p><a id="idx10114" class="indexterm"/> <a id="idx10121" class="indexterm"/>A useful tool to know about is the <span class="emphasis"><em>javap</em></span> command. With <span class="emphasis"><em>javap</em></span>, you can print a description of a compiled class. You don’t need the source code, and you don’t even need to know exactly where it is, only that it is in your classpath. For example:</p><a id="I_3_tt85"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">javap</code> <code class="n">java</code><code class="o">.</code><code class="na">util</code><code class="o">.</code><code class="na">Stack</code></code></strong></pre><p>prints the information about the <code class="literal">java.util.Stack</code> class:</p><a id="I_3_tt86"/><pre class="programlisting"><code class="n">Compiled</code> <code class="n">from</code> <code class="s">"Stack.java"</code> <code class="kd">public</code> <code class="kd">class</code> <code class="nc">java</code><code class="o">.</code><code class="na">util</code><code class="o">.</code><code class="na">Stack</code><code class="o">&lt;</code><code class="n">E</code><code class="o">&gt;</code> <code class="kd">extends</code> <code class="n">java</code><code class="o">.</code><code class="na">util</code><code class="o">.</code><code class="na">Vector</code><code class="o">&lt;</code><code class="n">E</code><code class="o">&gt;</code> <code class="o">{</code> <code class="kd">public</code> <code class="n">java</code><code class="o">.</code><code class="na">util</code><code class="o">.</code><code class="na">Stack</code><code class="o">();</code> <code class="kd">public</code> <code class="n">E</code> <code class="nf">push</code><code class="o">(</code><code class="n">E</code><code class="o">);</code> <code class="kd">public</code> <code class="kd">synchronized</code> <code class="n">E</code> <code class="nf">pop</code><code class="o">();</code> <code class="kd">public</code> <code class="kd">synchronized</code> <code class="n">E</code> <code class="nf">peek</code><code class="o">();</code> <code class="kd">public</code> <code class="kt">boolean</code> <code class="nf">empty</code><code class="o">();</code> <code class="kd">public</code> <code class="kd">synchronized</code> <code class="kt">int</code> <code class="nf">search</code><code class="o">(</code><code class="n">java</code><code class="o">.</code><code class="na">lang</code><code class="o">.</code><code class="na">Object</code><code class="o">);</code> <code class="o">}</code></pre><p><a id="I_indexterm3_id645242" class="indexterm"/>This is very useful if you don’t have other documentation handy and can also be helpful in debugging classpath problems. Using <span class="emphasis"><em>javap</em></span>, you can determine whether a class is in the classpath and possibly even which version you are looking at (many classpath issues involve duplicate classes in the classpath). If you are really curious, you can try <span class="emphasis"><em>javap</em></span> with the <em class="replaceable"><code>-c</code></em> option, which causes it to also print the JVM instructions for each method in the class!<a id="I_indexterm3_id645264" class="indexterm"/><a id="I_indexterm3_id645271" class="indexterm"/><a id="I_indexterm3_id645278" class="indexterm"/></p></div></div></body></html>