home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / man / sman1 / javac.1 < prev    next >
Encoding:
Text File  |  2002-09-06  |  30.4 KB  |  548 lines

  1. <!DOCTYPE REFENTRY PUBLIC "-//Sun Microsystems//DTD DocBook V3.0-Based SolBook Subset V2.0//EN" [
  2. <!--ArborText, Inc., 1988-1999, v.4002-->
  3. <!ENTITY % javaents SYSTEM "javaman.ent">
  4. %javaents;
  5. ]>
  6. <?Pub UDT _bookmark _target>
  7. <?Pub EntList minus bsol>
  8. <?Pub Inc>
  9. <refentry id="javac-1">
  10. <!--@(#)javac.1 1.12 01/03/14 SMI;-->
  11. <refmeta><refentrytitle>javac</refentrytitle><manvolnum>1</manvolnum>
  12. <refmiscinfo class="date">24 Jul 2001</refmiscinfo>
  13. <refmiscinfo class="sectdesc">&man1;</refmiscinfo>
  14. <refmiscinfo class="software">&java-release;</refmiscinfo>
  15. <refmiscinfo class="arch">generic</refmiscinfo>
  16. <refmiscinfo class="copyright"> Copyright 2001 Sun Microsystems, Inc. All
  17. rights reserved.  Copyright 2001 Sun Microsystems, Inc. Tous droits réservés.
  18. </refmiscinfo>
  19. </refmeta>
  20. <refnamediv><refname>javac</refname><refpurpose>Java compiler</refpurpose>
  21. </refnamediv>
  22. <refsynopsisdiv><title>&synp-tt;</title>
  23. <cmdsynopsis><command>javac</command><arg choice="opt"><option>bootclasspath
  24. </option> <replaceable>bootclasspath</replaceable></arg><arg choice="opt"><option>
  25. classpath</option> <replaceable>classpath</replaceable></arg><arg choice="opt"><option>
  26. d</option> <replaceable>directory</replaceable></arg><arg choice="opt"><option>
  27. deprecation</option></arg><arg choice="opt"><option>encoding</option><replaceable>
  28. encoding</replaceable></arg><arg choice="opt"><option>extdirs</option> <replaceable>
  29. directories</replaceable></arg><group><arg choice="plain"><option>g</option></arg>
  30. <arg choice="plain"><option>g:none</option></arg><arg choice="plain"><option>
  31. g:</option> <replaceable>keyword-list</replaceable></arg></group><arg
  32. choice="opt"><option>J</option><replaceable>option</replaceable></arg><arg><option>
  33. help</option></arg><arg choice="opt"><option>nowarn</option></arg><arg choice="opt"><option>
  34. O</option></arg><arg><option>source</option> <replaceable>release</replaceable></arg>
  35. <arg choice="opt"><option>sourcepath</option> <replaceable>sourcepath
  36. </replaceable></arg><arg choice="opt"><option>target</option> <replaceable>
  37. version</replaceable></arg><arg choice="opt"><option>verbose</option></arg>
  38. <arg choice="opt"><option>X</option></arg><arg choice="opt"><option>Xstdout
  39. </option> <replaceable>filename</replaceable></arg><arg choice="opt"><replaceable>
  40. sourcefiles</replaceable></arg><arg choice="opt"><replaceable>@files</replaceable></arg>
  41. </cmdsynopsis></refsynopsisdiv>
  42. <refsect1><title>&parm-tt;</title>
  43. <para>Options may be in any order. For a discussion of parameters which apply
  44. to a specific option, see &opts-tt; below.</para>
  45. <variablelist termlength="narrow">
  46. <varlistentry><term remap="15"><replaceable>sourcefiles</replaceable> </term>
  47. <listitem><para>One or more source files to be compiled (such as <literal>
  48. MyClass.java</literal>).</para>
  49. </listitem></varlistentry>
  50. <varlistentry><term remap="15"><literal>@</literal><replaceable>files</replaceable></term>
  51. <listitem><para>One or more files that list source files.</para>
  52. </listitem></varlistentry>
  53. </variablelist></refsect1>
  54. <refsect1><title>&desc-tt;</title>
  55. <indexterm id="javac-1-indx-1"><primary sortas="Java compiler">Java compiler —
  56. javac</primary></indexterm><indexterm id="javac-1-indx-2"><primary sortas="javac">
  57. javac — Java compiler</primary></indexterm><para>The <command>javac
  58. </command> tool reads class and interface definitions, written in the Java™
  59. programming language, and compiles them into bytecode class files.</para>
  60. <para>There are two ways to pass source code file names to  <command>javac
  61. </command>:</para>
  62. <itemizedlist>
  63. <listitem><para>For a small number of source files, simply list the file names
  64. on the command line.</para></listitem>
  65. <listitem><para>For a large number of source files, list the the file names
  66. in a file, separated by blanks or line breaks. Then use the list file name
  67. on the <command>javac</command> command line, preceded by an @ character.
  68. </para></listitem>
  69. </itemizedlist>
  70. <para>Source code file names must have <literal>.java</literal> suffixes,
  71. class file names must have <literal>.class</literal> suffixes, and both source
  72. and class files must have root names that identify the class. For example,
  73. a class called <literal>MyClass</literal> would be written in a source file
  74. called <literal>MyClass.java</literal> and compiled into a bytecode class
  75. file called <literal>MyClass.class</literal>.</para>
  76. <para>Inner class definitions produce additional class files. These class
  77. files have names combining the inner and outer class names, such as <literal>
  78. MyClass$MyInnerClass.class</literal>.</para>
  79. <para>You should arrange source files in a directory tree that reflects their
  80. package tree. For example, if you keep all your source files in <literal>
  81. /workspace</literal>, the source code for  <literal>com.mysoft.mypack.MyClass
  82. </literal> should be in <literal>/workspace/com/mysoft/mypack/MyClass.java
  83. </literal>.</para>
  84. <para>By default, the compiler puts each class file in the same directory
  85. as its source file. You can specify a separate destination directory with <option>
  86. d</option> (see &opts-tt; below).</para>
  87. <refsect2>
  88. <title>Searching for Types</title>
  89. <indexterm id="javac-1-indx-3"><primary sortas="javac">javac — Java
  90. compiler</primary><secondary>Searching for Types</secondary></indexterm><para>
  91. When compiling a source file, the compiler often needs information about a
  92. type it does not yet recognize. The compiler needs type information for every
  93. class or interface used, extended, or implemented in the source file. This
  94. includes classes and interfaces not explicitly mentioned in the source file
  95. but which provide information through inheritance.</para>
  96. <para>For example, when you subclass <literal>java.applet.Applet</literal>,
  97. you are also using Applet's ancestor classes:  <literal>java.awt.Panel</literal>, <literal>
  98. java.awt.Container</literal>, <literal>java.awt.Component</literal>, and  <literal>
  99. java.awt.Object</literal>.</para>
  100. <para>When the compiler needs type information, it looks for a source file
  101. or class file which defines the type. The compiler searches first in the bootstrap
  102. and extension classes, then in the user class path. The user class path is
  103. defined by setting the <envar>CLASSPATH</envar> environment variable or by
  104. using the <option>classpath</option> command line option. (For details, see <citetitle>
  105. Setting the Class Path</citetitle>.) If you use the  <option>sourcepath</option>
  106. option, the compiler searches the indicated path for source files. Otherwise
  107. the compiler searches the user class path both for class files and source
  108. files. You can specify different bootstrap or extension classes with the <option>
  109. bootclasspath</option> and <option>extdirs</option> options. See <literal>
  110. Cross-Compilation Options</literal> below.</para>
  111. <para>A successful type search may produce a class file, a source file, or
  112. both. Here is how <command>javac</command> handles each situation:</para>
  113. <itemizedlist>
  114. <listitem><para>Search produces a class file but no source file: <command>
  115. javac</command> uses the class file.</para></listitem>
  116. <listitem><para>Search produces a source file but no class file: <command>
  117. javac</command> compiles the source file and uses the resulting class file.
  118. </para></listitem>
  119. <listitem><para>Search produces both a source file and a class file: <command>
  120. javac</command> determines whether the class file is out of date. If the class
  121. file is out of date, <command>javac</command> recompiles the source file and
  122. uses the updated class file. Otherwise, <command>javac</command> just uses
  123. the class file.</para>
  124. <para>By default, <command>javac</command> considers a class file out of date
  125. only if it is older than the source file.</para></listitem>
  126. </itemizedlist>
  127. <para>Notice that <command>javac</command> can silently compile source files
  128. not mentioned on the command line. Use the  <option>verbose</option> option
  129. to trace automatic compilation.</para>
  130. </refsect2>
  131. </refsect1>
  132. <refsect1><title>&opts-tt;</title>
  133. <para>The compiler has a set of standard options that are supported on the
  134. current development environment and will be supported in future releases.
  135. An additional set of non-standard options are specific to the current virtual
  136. machine implementation and are subject to change in the future. Non-standard
  137. options begin with <option>X</option>.</para>
  138. <refsect2>
  139. <title>Standard Options</title>
  140. <indexterm id="javac-1-indx-5"><primary sortas="javac">javac — Java
  141. compiler</primary><secondary>Standard Options</secondary></indexterm>
  142. <variablelist termlength="wholeline">
  143. <varlistentry><term><option>classpath</option> <replaceable>classpath</replaceable> 
  144. </term><listitem><para>Sets the user class path, overriding the user class
  145. path in the  <envar>CLASSPATH</envar> environment variable. If neither <envar>
  146. CLASSPATH</envar> or <option>classpath</option> is specified, the user class
  147. path consists of the current directory. See <citetitle>Setting the Class Path
  148. </citetitle> for more details. </para>
  149. <para>If the <option>sourcepath</option> option is not specified, the user
  150. class path is searched for source files as well as class files.</para>
  151. </listitem></varlistentry>
  152. <varlistentry><term><option>d</option> <replaceable>directory</replaceable> 
  153. </term><listitem><para>Sets the destination directory for class files. The
  154. destination directory must already exist; javac will not create the destination
  155. directory. <?Pub Caret>If a class is part of a package, <command>javac</command>
  156. puts the class file in a subdirectory reflecting the package name, creating
  157. directories as needed. For example, if you specify <option>d</option> <literal>
  158. /home/myclasses</literal> and the class is called <literal>com.mypackage.MyClass
  159. </literal>, then the class file is called <literal>/home/myclasses/com/mypackage/MyClass.class
  160. </literal>.</para>
  161. <para>If <option>d</option> is not specified, <command>javac</command> puts
  162. the class file in the same directory as the source file.</para>
  163. <para>Notice that the directory specified by <option>d</option> is not automatically
  164. added to your user class path.</para>
  165. </listitem></varlistentry>
  166. <varlistentry><term><option>deprecation</option> </term><listitem><para>Shows
  167. a description of each use or override of a deprecated member or class. Without <option>
  168. deprecation</option>, <command>javac</command> shows the names of source files
  169. that use or override deprecated members or classes.</para>
  170. </listitem></varlistentry>
  171. <varlistentry><term><option>encoding</option> </term><listitem><para>Sets
  172. the source file encoding name, such as EUCJIS/SJIS. If <option>encoding</option>
  173. is not specified, the platform default converter is used.</para>
  174. </listitem></varlistentry>
  175. <varlistentry><term><option>g</option> </term><listitem><para>Generates all
  176. debugging information, including local variables. By default, only line number
  177. and source file information is generated.</para>
  178. </listitem></varlistentry>
  179. <varlistentry><term><option>g:none</option> </term><listitem><para>Does not
  180. generate any debugging information.</para>
  181. </listitem></varlistentry>
  182. <varlistentry><term><option>g:</option><replaceable>keyword-list</replaceable> 
  183. </term><listitem><para>Generates only some kinds of debugging information,
  184. specified by a comma-separated list of keywords. Valid keywords are: <variablelist
  185. termlength="narrow">
  186. <varlistentry><term remap="10"><literal>source</literal> </term><listitem>
  187. <para>Source file debugging information.</para>
  188. </listitem></varlistentry>
  189. <varlistentry><term><literal>lines</literal> </term><listitem><para>Line number
  190. debugging information.</para>
  191. </listitem></varlistentry>
  192. <varlistentry><term><literal>vars</literal> </term><listitem><para>Local variable
  193. debugging information.</para>
  194. </listitem></varlistentry>
  195. </variablelist></para>
  196. </listitem></varlistentry>
  197. <varlistentry><term remap="7"><option>help</option> </term><listitem><para>
  198. Prints a synopsis of standard options.</para>
  199. </listitem></varlistentry>
  200. <varlistentry><term remap="7"><option>nowarn</option> </term><listitem><para>
  201. Disables warning messages.</para>
  202. </listitem></varlistentry>
  203. <varlistentry><term remap="7"><option>source</option> <replaceable>release
  204. </replaceable></term><listitem><para>Enables support for compiling source
  205. code containing assertions.</para>
  206. <para>When <replaceable>release</replaceable> is set to 1.4, the compiler
  207. accepts code containing assertions. Assertions were introduced in J2SE 1.4.
  208. </para>
  209. <para>When <replaceable>release</replaceable> is set to 1.3, the compiler
  210. does not support assertions. The compiler defaults to the behavior in 1.3
  211. if the <option>source</option> flag is not used.</para>
  212. </listitem></varlistentry>
  213. <varlistentry><term><option>sourcepath</option> <replaceable>sourcepath</replaceable> 
  214. </term><listitem><para>Specifies the source code path to search for class
  215. or interface definitions. As with the user class path, source path entries
  216. are separated by colons (<literal>:</literal>) and can be directories, <acronym
  217. remap="small">JAR</acronym> archives, or <acronym remap="small">ZIP</acronym>
  218. archives. If packages are used, the local path name within the directory or
  219. archive must reflect the package name.</para>
  220. <para>Notice that classes found through the classpath are subject to automatic
  221. recompilation if their sources are found.</para>
  222. </listitem></varlistentry>
  223. <varlistentry><term><option>verbose</option> </term><listitem><para>Verbose
  224. output. This includes information about each class loaded and each source
  225. file compiled.</para>
  226. </listitem></varlistentry>
  227. </variablelist></refsect2>
  228. <refsect2>
  229. <title>Cross-Compilation Options</title>
  230. <indexterm id="javac-1-indx-6"><primary sortas="javac">javac — Java
  231. compiler</primary><secondary>Cross-Compilation Options</secondary></indexterm>
  232. <para>By default, classes are compiled against the bootstrap and extension
  233. classes of the JDK that <command>javac</command> shipped with. But <command>
  234. javac</command> also supports cross-compiling, where classes are compiled
  235. against a bootstrap and extension classes of a different Java platform implementation.
  236. It is important to use <option>bootclasspath</option> and <option>extdirs
  237. </option> when cross-compiling. See <literal>Cross-Compilation Example</literal>
  238. below.</para>
  239. <variablelist termlength="wholeline">
  240. <varlistentry><term><option>bootclasspath</option> <replaceable>bootclasspath
  241. </replaceable> </term><listitem><para>Cross-compiles against the specified
  242. set of boot classes. As with the user class path, boot class path entries
  243. are separated by colons (<literal>:</literal>) and can be directories, <acronym
  244. remap="small">JAR</acronym> archives, or <acronym remap="small">ZIP</acronym>
  245. archives.</para>
  246. </listitem></varlistentry>
  247. <varlistentry><term><option>extdirs</option> <replaceable>directories</replaceable> 
  248. </term><listitem><para>Cross-compiles against the specified extension directories.
  249. Directories are a colon-separated list of directories. Each <acronym remap="small">
  250. JAR</acronym> archive in the specified directories is searched for class files.
  251. </para>
  252. </listitem></varlistentry>
  253. <varlistentry><term><option>target</option> <replaceable>version</replaceable> 
  254. </term><listitem><para>Generates class files that will work on <acronym remap="small">
  255. VM</acronym>s with the specified version. The default is to generate class
  256. files to be compatible with the 1.2 <acronym remap="small">VM</acronym>, with
  257. one exception. When the <command>—source 1.4</command> option is used,
  258. the default target is 1.4. The versions supported are: <variablelist termlength="xtranarrow">
  259. <varlistentry><term>1.1</term><listitem><para>Ensures that generated class
  260. files will be compatible with 1.1 and 1.2 <acronym remap="small">VM</acronym>s.
  261. </para>
  262. </listitem></varlistentry>
  263. <varlistentry><term>1.2</term><listitem><para>Generates class files that will
  264. run on 1.2 <acronym remap="small">VM</acronym>s, but will not run on 1.1 <acronym
  265. remap="small">VM</acronym>s. This is the default.</para>
  266. </listitem></varlistentry>
  267. <varlistentry><term>1.3</term><listitem><para>Generate class files that run
  268. on<acronym remap="small">VM</acronym>s in the Java 2 SDK v, 1.3 and later,
  269. but will not run on 1.1 or 1.2<acronym remap="small">VM</acronym>s.</para>
  270. </listitem></varlistentry>
  271. <varlistentry><term>1.4</term><listitem><para>Generates class files that are
  272. compatible only with 1.4 <acronym remap="small">VM</acronym>s.</para>
  273. </listitem></varlistentry>
  274. </variablelist></para>
  275. </listitem></varlistentry>
  276. <varlistentry><term>1.4</term><listitem><para>Generate class files that are
  277. compatible only with 1.4 <acronym remap="small">VM</acronym>s.</para>
  278. </listitem></varlistentry>
  279. </variablelist></refsect2>
  280. <refsect2>
  281. <title>Non-Standard Options</title>
  282. <indexterm id="javac-1-indx-7"><primary sortas="javac">javac — Java
  283. compiler</primary><secondary>Non-Standard Options</secondary></indexterm>
  284. <variablelist termlength="wholeline">
  285. <varlistentry><term><option>J</option><replaceable>option</replaceable> </term>
  286. <listitem><para>Passes <replaceable>option</replaceable> to the java launcher
  287. called by <command>javac</command>. For example, <literal>–J–Xms48m
  288. </literal> sets the startup memory to 48 megabytes. Although it does not begin
  289. with <option>X</option>, it is not a `standard option' of <command>javac</command>.
  290. It is a common convention for <option>J</option> to pass options to the underlying <acronym
  291. remap="small">VM</acronym> executing applications written in Java. </para>
  292. <para>Notice that <envar>CLASSPATH</envar>,  <option>classpath</option>, <option>
  293. bootclasspath</option>, and  <option>extdirs</option> do not specify the classes
  294. used to run <command>javac</command>. Tinkering with the implementation of
  295. the compiler in this way is usually pointless and always risky. If you do
  296. need to do this, use the  <option>J</option> option to pass through options
  297. to the underlying java launcher.</para>
  298. </listitem></varlistentry>
  299. <varlistentry><term><option>X</option> </term><listitem><para>Displays information
  300. about non-standard options and exit.</para>
  301. </listitem></varlistentry>
  302. <varlistentry><term><option>Xstdout</option> <replaceable>filename</replaceable></term>
  303. <listitem><para>Sends compiler messages to the named file. By default, compiler
  304. messages go to <literal>System.err</literal>.</para>
  305. </listitem></varlistentry>
  306. <varlistentry><term><option>Xswitchcheck</option></term><listitem><para>Checks <command>
  307. switch</command> blocks for fall-through cases and provides a warning message
  308. for any that are found. Fall-through cases are cases in a <command>switch
  309. </command> block, other than the last case in the block, whose code does not
  310. include a <command>break</command> statement, allowing code execution to “fall
  311. through” from that case to the next case. For example, the code following
  312. the <command>case 1</command> label in this <command>switch</command> block
  313. does not contain a break statement:<informalexample>
  314. <programlisting>switch (x) {
  315. case 1:
  316.      System.out.println("1");
  317.      //  No  break;  statement here.
  318. case 2:
  319.      System.out.println("2");
  320. }</programlisting>
  321. </informalexample></para>
  322. <para>If the <option>Xswitchcheck</option> flag were used when compiling this
  323. code, the compiler would emit a warning about “possible fall-through
  324. into case,” along with the line number of the case in question.</para>
  325. </listitem></varlistentry>
  326. </variablelist></refsect2>
  327. <refsect2>
  328. <title>Command Line Argument Files</title>
  329. <indexterm id="javac-1-indx-4"><primary sortas="javac">javac — Java
  330. compiler</primary><secondary>File List</secondary></indexterm><para>To shorten
  331. or simplify the <command>javac</command> command line, you can specify one
  332. or more files that themselves contain arguments to the <command>javac</command>
  333. command. This enables you to create javac commands of any length on any operating
  334. system.</para>
  335. <para>An argument file can include javac options and source filenames in any
  336.  combination.  The arguments within a file can be space-separated or  newline-separated.
  337.  Filenames within an argument file are relative to  the current directory,
  338. not the location of the argument file. Wildcards (*) are not allowed in these
  339. lists (such as for specifying <command>*.java</command>).  Use of the '@'
  340. character to recursively  interpret files is not supported.</para>
  341. <para>When executing javac, pass in the path and name of each argument  file
  342. with the '@' leading character. When javac encounters an argument beginning
  343. with  the character `@', it expands the contents of that file into the argument
  344. list.</para>
  345. <para>Example — Single Arg File</para>
  346. <para>You could use a single argument file named <filename>argfile</filename>
  347. to hold all javac arguments:<informalexample>
  348. <screen>C:> <userinput>javac @argfile</userinput></screen>
  349. </informalexample></para>
  350. <para>This argument file could contain the contents of both files shown in
  351. the next example.</para>
  352. <para>Example — Two Arg Files</para>
  353. <para>You can create two argument files -- one for the javac  options and
  354. the other for the source filenames: (Notice the following lists have no line-continuation
  355. characters.)</para>
  356. <para>Create a file named <filename>options</filename> containing:<informalexample>
  357. <screen>-d classes
  358. -g
  359. -sourcepath \java\pubs\ws\1.3\src\share\classes</screen>
  360. </informalexample></para>
  361. <para>Create a file named <filename>classes</filename> containing:<informalexample>
  362. <screen>MyClass1.java
  363. MyClass2.java
  364. MyClass3.java</screen>
  365. </informalexample></para>
  366. <para>You would then run javac with:<informalexample>
  367. <screen>C:> <userinput>javac @options @classes</userinput></screen>
  368. </informalexample></para>
  369. <para>Example — Arg FIles with Paths</para>
  370. <para>The argument files can have paths, but any filenames inside the files
  371. are relative to the current working directory (not <command>path1</command>
  372. or <command>path2</command>):<informalexample>
  373. <screen>C:> <userinput>javac @path1\options @path2\classes</userinput></screen>
  374. </informalexample></para>
  375. </refsect2>
  376. </refsect1>
  377. <refsect1><title>&exam-tt;</title>
  378. <example role="example">
  379. <title>Compiling a Simple Program</title>
  380. <para>One source file, <literal>Hello.java</literal>, defines a class called <literal>
  381. greetings.Hello</literal>. The greetings directory is the package directory
  382. both for the source file and the class file and is off the current directory.
  383. This allows you to use the default user class path. It also makes it unnecessary
  384. to specify a separate destination directory with <option>d</option>.<informalexample>
  385. <screen>   example% <userinput>ls</userinput>
  386.       <computeroutput>greetings/</computeroutput>
  387.    example% <userinput>ls greetings</userinput>
  388.       <computeroutput>Hello.java
  389.  
  390. </computeroutput>   example% <userinput>cat greetings/Hello.java</userinput>
  391.       <computeroutput>package greetings;
  392.    
  393.       public class Hello {
  394.            public static void main(String[] args) {
  395.               for (int i=0; i < args.length; i++) {
  396.                   System.out.println("Hello " + args[i]);
  397.               }
  398.            }
  399.       }</computeroutput>
  400.    example% <userinput>javac greetings/Hello.java</userinput>
  401.    example% <userinput>ls greetings</userinput>
  402.       <computeroutput>Hello.class   Hello.java</computeroutput>
  403.    example% <userinput>java greetings.Hello World Universe Everyone</userinput>
  404.       <computeroutput>Hello World
  405.       Hello Universe
  406.       Hello Everyone</computeroutput></screen>
  407. </informalexample></para>
  408. </example>
  409. <example role="example">
  410. <title>Compiling Multiple Source Files</title>
  411. <para>This example compiles all the source files in the package <literal>
  412. greetings</literal>:<informalexample>
  413. <screen>   example% <userinput>ls</userinput>
  414.       <computeroutput>greetings/</computeroutput>
  415.    example% <userinput>ls greetings</userinput>
  416.       <computeroutput>Aloha.java     GutenTag.java     Hello.java      Hi.java
  417. </computeroutput>
  418.    example% <userinput>javac greetings/*.java</userinput>
  419.    example% <userinput>ls greetings</userinput>
  420.       <computeroutput>Aloha.class    GutenTag.class    Hello.class     Hi.class
  421.       Aloha.java     GutenTag.java     Hello.java      Hi.java</computeroutput></screen>
  422. </informalexample></para>
  423. </example>
  424. <example role="example">
  425. <title>Specifying a User Class Path</title>
  426. <para>Having changed one of the source files in the previous example, we recompile
  427. it:<informalexample>
  428. <screen>   example% <userinput>pwd</userinput>
  429.       <computeroutput>/examples</computeroutput>
  430.    example% <userinput>javac greetings/Hi.java</userinput></screen>
  431. </informalexample>Since <literal>greetings.Hi</literal> refers to other classes
  432. in the greetings package, the compiler needs to find these other classes.
  433. The example above works, because the default user class path happens to be
  434. the directory containing the package directory. But suppose you want to recompile
  435. this file and not worry about which directory you are in? Then you need to
  436. add  <literal>/examples</literal> to the user class path. This can be done
  437. by setting <envar>CLASSPATH</envar>, but in this example the <option>classpath
  438. </option> option is used:<informalexample>
  439. <screen>   example% <userinput>javac –classpath \examples /examples/greetings/Hi.java
  440. </userinput></screen>
  441. </informalexample></para>
  442. <para>If you change <literal>greetings.Hi</literal> again to use a banner
  443. utility, that utility also needs to be accessible through the user class path:<informalexample>
  444. <screen>   example% <userinput>javac –classpath /examples:/lib/Banners.jar \
  445.                      /examples/greetings/Hi.java</userinput></screen>
  446. </informalexample></para>
  447. <para>To execute a class in <literal>greetings</literal>, you need access
  448. both to <literal>greetings</literal> and to the classes it uses.<informalexample>
  449. <screen>   example% <userinput>java –classpath /examples:/lib/Banners.jar greetings.Hi
  450. </userinput></screen>
  451. </informalexample></para>
  452. </example>
  453. <example role="example">
  454. <title>Separating Source Files and Class Files</title>
  455. <para>It often makes sense to keep source files and class files in separate
  456. directories, especially on large projects. Use the <option>d</option> option
  457. to indicate the separate class file destination. Since the source files are
  458. not in the user class path, use <option>sourcepath</option> to help the compiler
  459. find them:<informalexample>
  460. <screen>   example% <userinput>ls</userinput>
  461.       <computeroutput>classes/  lib/      src/</computeroutput>
  462.    example% <userinput>ls src</userinput>
  463.       <computeroutput>farewells/</computeroutput>
  464.    example% <userinput>ls src/farewells</userinput>
  465.       <computeroutput>Base.java      GoodBye.java</computeroutput>
  466.    example% <userinput>ls lib</userinput>
  467.       <computeroutput>Banners.jar</computeroutput>
  468.    example% ls classes
  469.    example% <userinput>javac –sourcepath src –classpath classes:lib/Banners.jar \
  470.       src/farewells/GoodBye.java –d classes</userinput>
  471.    example% <userinput>ls classes</userinput>
  472.       <computeroutput>farewells/</computeroutput>
  473.    example% <userinput>ls classes/farewells</userinput>
  474.       <computeroutput>Base.class      GoodBye.class</computeroutput></screen>
  475. </informalexample></para>
  476. <para>Notice that the compiler compiled <literal>src/farewells/Base.java</literal>,
  477. even though it wasn't specified on the command line. To trace automatic compiles,
  478. use the <option>verbose</option> option.</para>
  479. </example>
  480. <example role="example">
  481. <title>Cross-Compilation Example</title>
  482. <para>Here the JDK 1.2 <command>javac</command> is used to compile code that
  483. will run on a 1.1 <acronym remap="small">VM</acronym>.<informalexample>
  484. <screen>   example% <userinput>javac –target 1.1 –bootclasspath jdk1.1.7/lib/classes.zip \
  485.         –extdirs "" OldCode.java</userinput></screen>
  486. </informalexample></para>
  487. <para>The <option>target</option> <literal>1.1</literal> option ensures that
  488. the generated class files will be compatible with 1.1 <acronym remap="small">
  489. VM</acronym>s. In JDK1.2, <command>javac</command> compiles for 1.1 by default,
  490. so this option is not strictly required. However, it is good form because
  491. other compilers may have other defaults.</para>
  492. <para>The JDK 1.2 <command>javac</command> would also by default compile against
  493. its own 1.2 bootstrap classes, so it is necessary to tell <command>javac</command>
  494. to compile against JDK 1.1 bootstrap classes instead. This is done with <option>
  495. bootclasspath</option> and <option>extdirs</option>. Failing to do this might
  496. allow compilation against a 1.2 <acronym remap="small">API</acronym> that
  497. would not be present on a 1.1 <acronym remap="small">VM</acronym> and fail
  498. at runtime.</para>
  499. </example>
  500. </refsect1>
  501. <refsect1><title>&attr-tt;</title>
  502. <para>See <citerefentry><refentrytitle>attributes</refentrytitle><manvolnum>
  503. 5</manvolnum></citerefentry> for a description of the following attributes:
  504. </para>
  505. <informaltable frame="all" orient="port">
  506. <tgroup cols="2" colsep="1" rowsep="1"><colspec colnum="1" colname="col1"
  507. colwidth="1*" colsep="1" align="left"><colspec colnum="2" colname="col2" colwidth="1*"
  508. align="left">
  509. <thead>
  510. <row><entry colname="col1" rowsep="1" align="center" valign="top">ATTRIBUTE
  511. TYPE</entry><entry colname="col2" rowsep="1" align="center" valign="top">
  512. ATTRIBUTE VALUE</entry></row>
  513. </thead>
  514. <tbody>
  515. <row><entry>Availability</entry><entry>SUNWj3dev</entry></row>
  516. </tbody>
  517. </tgroup>
  518. </informaltable>
  519. </refsect1>
  520. <refsect1><title>&also-tt;</title>
  521. <para><citerefentry><refentrytitle>jar</refentrytitle><manvolnum>1</manvolnum>
  522. </citerefentry>, <citerefentry><refentrytitle>java</refentrytitle><manvolnum>
  523. 1</manvolnum></citerefentry>, <citerefentry><refentrytitle>javadoc</refentrytitle>
  524. <manvolnum>1</manvolnum></citerefentry>, <citerefentry><refentrytitle>javah
  525. </refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry><refentrytitle>
  526. javap</refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry>
  527. <refentrytitle>jdb</refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry>
  528. <refentrytitle>attributes</refentrytitle><manvolnum>5</manvolnum></citerefentry> 
  529. </para>
  530. <para>See or search the Java web site for the following:</para>
  531. <variablelist termlength="wholeline">
  532. <varlistentry><term remap="10"><citetitle>The Java Extensions Mechanism</citetitle>
  533. @</term><listitem><para><ulink url="http://java.sun.com/j2se/1.4/docs/guide/extensions/index.html">
  534. http://java.sun.com/j2se/1.4/docs/guide/extensions/index.html</ulink></para>
  535. </listitem></varlistentry>
  536. <varlistentry><term remap="10"><citetitle>Setting the Class Path</citetitle>
  537. @</term><listitem><para><ulink url="http://java.sun.com/j2se/1.4/docs/tooldocs/solaris/classpath.html">
  538. http://java.sun.com/j2se/1.4/docs/tooldocs/solaris/classpath.html</ulink></para>
  539. </listitem></varlistentry>
  540. </variablelist></refsect1>
  541. <!--+ 07/27/00 08:33:52
  542.     | tagMorph:  $Id: tagMorph-engine 1.54 1998/01/19 05:44:48 $
  543.     | tagMorph library:  $Id: libtagMorph.a 1.50 1998/01/19 22:02:51 $
  544.     | sunman-to-solbook 1.17
  545.     +-->
  546. </refentry>
  547. <?Pub *0000031134>
  548.