UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

70 lines (68 loc) 8.09 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 Java Compiler</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 Java Compiler"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-3-SECT-4"/>The Java Compiler</h1></div></div></div><p><a id="idx10115" class="indexterm"/> <a id="idx10118" class="indexterm"/> <a id="idx10120" class="indexterm"/> <a id="idx10128" class="indexterm"/> In this section, we’ll say a few words about <span class="emphasis"><em>javac</em></span>, the Java compiler in the JDK. The <span class="emphasis"><em>javac</em></span> compiler is written entirely in Java, so it’s available for any platform that supports the Java runtime system. <span class="emphasis"><em>javac</em></span> turns Java source code into a compiled class that contains Java bytecode. By convention, source files are named with a <a id="I_indexterm3_id645352" class="indexterm"/><span class="emphasis"><em>.java</em></span> extension; the resulting class files have a <a id="I_indexterm3_id645363" class="indexterm"/><span class="emphasis"><em>.class</em></span> extension. Each source code file is considered a single compilation unit. As you’ll see in <a class="xref" href="ch06.html" title="Chapter 6. Relationships Among Classes">Chapter 6</a>, classes in a given compilation unit share certain features, such as <code class="literal">package</code> and <code class="literal">import</code> statements.</p><p><span class="emphasis"><em>javac</em></span> allows one public class per file and insists that the file have the same name as the class. If the filename and class name don’t match, <span class="emphasis"><em>javac</em></span> issues a compilation error. A single file can contain multiple classes, as long as only one of the classes is public and is named for the file. Avoid packing too many classes into a single source file. Packing classes together in a <span class="emphasis"><em>.java</em></span> file only superficially associates them. In <a class="xref" href="ch06.html" title="Chapter 6. Relationships Among Classes">Chapter 6</a>, we’ll talk about inner classes, classes that contain other classes and interfaces.</p><p>As an example, place the following source code in the file <span class="emphasis"><em>BigBird.java</em></span>:</p><a id="I_3_tt87"/><pre class="programlisting"> <code class="kn">package</code> <code class="n">animals</code><code class="o">.</code><code class="na">birds</code><code class="o">;</code> <code class="kd">public</code> <code class="kd">class</code> <code class="nc">BigBird</code> <code class="kd">extends</code> <code class="n">Bird</code> <code class="o">{</code> <code class="o">...</code> <code class="o">}</code></pre><p>Next, compile it with:</p><a id="I_3_tt88"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">javac</code> <code class="n">BigBird</code><code class="o">.</code><code class="na">java</code></code></strong></pre><p>Unlike the Java interpreter, which takes just a class name as its argument, <em class="filename">javac</em> needs a filename (with the <span class="emphasis"><em>.java</em></span> extension) to process. The previous command produces the class file <span class="emphasis"><em>BigBird.class</em></span> in the same directory as the source file. While it’s nice to see the class file in the same directory as the source for this example, for most real applications, you need to store the class file in an appropriate place in the classpath.</p><p><a id="I_indexterm3_id645464" class="indexterm"/>You can use the <em class="replaceable"><code>-d</code></em> option with <span class="emphasis"><em>javac</em></span> to specify an alternative directory for storing the class files <span class="emphasis"><em>javac</em></span> generates. The specified directory is used as the root of the class hierarchy, so <span class="emphasis"><em>.class</em></span> files are placed in this directory or in a subdirectory below it, depending on whether the class is contained in a package. (The compiler creates intermediate subdirectories automatically, if necessary.) For example, we can use the following command to create the <span class="emphasis"><em>BigBird.class</em></span> file at <span class="emphasis"><em>/home/vicky/Java/classes/animals/birds/BigBird.class</em></span>:</p><a id="I_3_tt89"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">javac</code> <code class="o">-</code><code class="n">d</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="n">classes</code> <code class="n">BigBird</code><code class="o">.</code><code class="na">java</code></code></strong></pre><p>You can specify multiple <span class="emphasis"><em>.java</em></span> files in a single <span class="emphasis"><em>javac</em></span> command; the compiler creates a class file for each source file. But you don’t need to list the other classes your class references as long as they are in the classpath in either source or compiled form. During compilation, Java resolves all other class references using the classpath.</p><p>The Java compiler is more intelligent than your average compiler, replacing some of the functionality of a <a id="I_indexterm3_id645527" class="indexterm"/><span class="emphasis"><em>make</em></span> utility. For example, <span class="emphasis"><em>javac</em></span> compares the modification times of the source and class files for all classes and recompiles them as necessary. A compiled Java class remembers the source file from which it was compiled, and as long as the source file is available, <em class="filename">javac</em> can recompile it if necessary. If, in the previous example, class <code class="literal">BigBird</code> references another class, <code class="literal">animals.furry.Grover</code>, <span class="emphasis"><em>javac</em></span> looks for the source file <span class="emphasis"><em>Grover.java</em></span> in an <code class="literal">animals.furry</code> package and recompiles it if necessary to bring the <span class="emphasis"><em>Grover.class</em></span> class file up-to-date.</p><p>By default, however, <span class="emphasis"><em>javac</em></span> checks only source files that are referenced directly from other source files. This means that if you have an out-of-date class file that is referenced only by an up-to-date class file, it may not be noticed and recompiled. For that and many other reasons, most projects use a real build utility such as Apache’s Ant to manage builds, packaging, and more. We discuss Ant in <a class="xref" href="ch15.html" title="Chapter 15. Web Applications and Web Services">Chapter 15</a>.</p><p>Finally, it’s important to note that <span class="emphasis"><em>javac</em></span> can compile an application even if only the compiled (binary) versions of some of the classes are available. You don’t need source code for all your objects. Java class files contain all the data type and method signature information that source files contain, so compiling against binary class files is as typesafe (and exception safe) as compiling with Java source code.<a id="I_indexterm3_id645604" class="indexterm"/><a id="I_indexterm3_id645611" class="indexterm"/><a id="I_indexterm3_id645618" class="indexterm"/><a id="I_indexterm3_id645625" class="indexterm"/></p></div></body></html>