epubjs
Version:
Render ePub documents in the browser, across many devices
58 lines (57 loc) • 9.66 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Running Java Applications</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="Running Java Applications"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-3-SECT-2"/>Running Java Applications</h1></div></div></div><p><a id="idx10112" class="indexterm"/> A standalone Java application must have at least one class
containing a method called <a id="I_indexterm3_id644400" class="indexterm"/><code class="literal">main()</code>, which is the
first code to be executed upon startup. To run the application, start the
VM, specifying that class as an argument. You can also specify options to
the interpreter as well as arguments to be passed to the
application<span class="emphasis"><em>:</em></span></p><a id="I_3_tt74"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">java</code></code></strong> <code class="o">[</code><em class="replaceable"><code><code class="n">interpreter</code> <code class="n">options</code></code></em><code class="o">]</code> <em class="replaceable"><code><code class="n">class_name</code></code></em> <code class="o">[</code><em class="replaceable"><code><code class="n">program</code> <code class="n">arguments</code></code></em><code class="o">]</code></pre><p>The class should be specified as a fully qualified class name,
including the package name, if any. Note, however, that you don’t include
the <a id="I_indexterm3_id644443" class="indexterm"/><span class="emphasis"><em>.class</em></span> file extension. Here are a
couple of examples:</p><a id="I_3_tt75"/><pre class="programlisting"> <code class="o">%</code><strong class="userinput"><code><code class="n">java</code> <code class="n">animals</code><code class="o">.</code><code class="na">birds</code><code class="o">.</code><code class="na">BigBird</code></code></strong>
<code class="o">%</code><strong class="userinput"><code><code class="n">java</code> <code class="n">MyTest</code></code></strong></pre><p>The interpreter searches for the class in the <a id="I_indexterm3_id644475" class="indexterm"/><a id="I_indexterm3_id644480" class="indexterm"/><span class="emphasis"><em>classpath</em></span>, a list of directories and
archive files where classes are stored. We’ll discuss the classpath in
detail in the next section. The classpath can be specified either by an
<span class="emphasis"><em>environment variable</em></span> or with the command-line option
<em class="replaceable"><code>-classpath</code></em>. If both are present, the
command-line option is used.</p><p>Alternately, the <a id="I_indexterm3_id644502" class="indexterm"/><span class="emphasis"><em>java</em></span> command can be used to launch an
“executable” <a id="I_indexterm3_id644511" class="indexterm"/>Java archive (JAR)
file:</p><a id="I_3_tt76"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">java</code> <code class="o">-</code><code class="n">jar</code> <code class="n">spaceblaster</code><code class="o">.</code><code class="na">jar</code></code></strong></pre><p>In this case, the JAR file includes metadata with the name of the
startup class containing the <code class="literal">main()</code>
method, and the classpath becomes the JAR file itself.</p><p>After loading the first class and executing its <code class="literal">main()</code> method, the application can reference
other classes, start additional threads, and create its user interface or
other structures, as shown in <a class="xref" href="ch03s03.html#learnjava3-CHP-3-FIG-1" title="Figure 3-1. Starting a Java application">Figure 3-1</a>.</p><div class="figure"><a id="learnjava3-CHP-3-FIG-1"/><div class="figure-contents"><div class="mediaobject"><a id="I_3_tt77"/><img src="httpatomoreillycomsourceoreillyimages1707609.png" alt="Starting a Java application"/></div></div><p class="title">Figure 3-1. Starting a Java application</p></div><p>The <a id="I_indexterm3_id644581" class="indexterm"/><code class="literal">main()</code> method must have
the right <span class="emphasis"><em>method signature</em></span>. A method signature is the
set of information that defines the method. It includes the method’s name,
arguments, and return type, as well as type and visibility modifiers. The
<code class="literal">main()</code> method must be a <a id="I_indexterm3_id644604" class="indexterm"/><code class="literal">public</code>, <a id="I_indexterm3_id644618" class="indexterm"/><code class="literal">static</code> method that takes
an array of <code class="literal">String</code> objects as its
argument and does not return any value (<code class="literal">void</code>):</p><a id="I_3_tt78"/><pre class="programlisting"> <code class="kd">public</code> <code class="kd">static</code> <code class="kt">void</code> <code class="nf">main</code> <code class="o">(</code> <code class="n">String</code> <code class="o">[]</code> <code class="n">myArgs</code> <code class="o">)</code></pre><p>The fact that <code class="literal">main()</code> is a
<code class="literal">public</code> and <code class="literal">static</code> method simply means that it is globally
accessible and that it can be called directly by name. We’ll discuss the
implications of visibility modifiers such as <code class="literal">public</code> and the meaning of <code class="literal">static</code> in Chapters <a class="xref" href="ch04.html" title="Chapter 4. The Java Language">4</a> through
<a class="xref" href="ch06.html" title="Chapter 6. Relationships Among Classes">6</a>.</p><p>The <code class="literal">main()</code> method’s single
argument, the array of <code class="literal">String</code> objects,
holds the command-line arguments passed to the application. The name of
the parameter doesn’t matter; only the type is important. In Java, the
content of <code class="literal">myArgs</code> is an array. In Java,
arrays know how many elements they contain and can happily provide that
information:</p><a id="I_3_tt79"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">numArgs</code> <code class="o">=</code> <code class="n">myArgs</code><code class="o">.</code><code class="na">length</code><code class="o">;</code></pre><p><code class="literal">myArgs[0]</code> is the first
command-line argument, and so on.</p><p>The Java interpreter continues to run until the <code class="literal">main()</code> method of the initial class file returns
and until any threads that it has started also exit. Special threads
designated as <a id="I_indexterm3_id644745" class="indexterm"/><span class="emphasis"><em>daemon</em></span> threads are automatically
terminated when the rest of the application has completed.</p><div class="sect2" title="System Properties"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-3-SECT-2.1"/>System Properties</h2></div></div></div><p>Although it is possible to read host environment variables from
Java, it is discouraged for application configuration. Instead, Java
allows any number of <span class="emphasis"><em>system property</em></span> values to be
passed to the application when the VM is started. System properties are
simply name-value string pairs that are available to the application
through the static <a id="I_indexterm3_id644774" class="indexterm"/><code class="literal">System.getProperty()</code>
method. You can use these properties as a more structured and portable
alternative to command-line arguments and environment variables for
providing general configuration information to your application at
startup. Each system property is passed to the interpreter on the
command line using the <em class="replaceable"><code>-D</code></em> option followed by
<em class="replaceable"><code>name</code></em><a id="I_indexterm3_id644794" class="indexterm"/><a id="I_indexterm3_id644799" class="indexterm"/><code class="literal">=</code><em class="replaceable"><code>value</code></em>. For
example:</p><a id="I_3_tt80"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">java</code> <code class="o">-</code><code class="n">Dstreet</code><code class="o">=</code><code class="n">sesame</code> <code class="o">-</code><code class="n">Dscene</code><code class="o">=</code><code class="n">alley</code> <code class="n">animals</code><code class="o">.</code><code class="na">birds</code><code class="o">.</code><code class="na">BigBird</code></code></strong></pre><p>The value of the <code class="literal">street</code>
property is then accessible this way:</p><a id="I_3_tt81"/><pre class="programlisting"> <code class="n">String</code> <code class="n">street</code> <code class="o">=</code> <code class="n">System</code><code class="o">.</code><code class="na">getProperty</code><code class="o">(</code><code class="s">"street"</code><code class="o">);</code></pre><p>An application can get its configuration in myriad other ways,
including via files or network configuration at runtime.<a id="I_indexterm3_id644845" class="indexterm"/></p></div></div></body></html>