    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Building Java Applications</title>
        <link>https://members.accu.org/index.php/journals/396</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #49 - Jun 2002 + Project Management</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c196/">49</a>
                    (8)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c66/">Management</a>
                    (95)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c196-66/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c196+66/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Building Java Applications</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 June 2002 17:46:10 +01:00 or Wed, 26 June 2002 17:46:10 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>This is an article about the boring process of running
<span class="application">javac</span> on Java source files to
obtain Java class files. It explains that it's not so simple (when
you come down to it, it's actually pretty complicated), what has
been done to make it seem simple and what, in my opinion, should be
done instead.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e25" id="d0e25"></a>Dependency
management</h2>
</div>
<p>So, what's so complicated about running <span class=
"application">javac</span>? Well, making class files may be the
most obvious part of a Java build, but it's rarely the only one. In
my experience (which is admittedly limited, but I did work on more
than one Java project), Java projects are typically multi-language:
more often than not, there's some legacy library which must be
accessed through native code, a CORBA IDL interface to be
implemented, an XML DTD to conform to and so on.There are many code
generators producing Java, and therefore much code to be generated
before compilation; and when the class files are compiled, they
must be packaged into <tt class="filename">jar</tt>s (which may
need to be signed), and then there's no limit to how complicated
you want your automated <span class=
"emphasis"><em>testing</em></span> to be. I subscribe to the adage
that &quot;the program that has not been tested does not work&quot;, and even
if you don't, reflect on the dynamic nature of Java (runtime checks
for null pointers, casts and so on) and ask yourself whether you
really want your users to be the first running your code... And if
you want to test what is delivered (as opposed to some other, quite
similar code), you need version control, and that should also mesh
with your build. Builds are hard, and Java builds are harder than
average.</p>
<p>Of course, if a build does a lot of things, it shouldn't do them
every time. Not only do I not want to run integration tests every
time I change a line - I don't even want to package the <tt class=
"filename">jar</tt>s. I want to run the unit tests early and often
and they must use class files corresponding to my source files.
Therefore the build must figure out what needs to be updated
(whether it's one class file, or every class using a changed
constant) and build that and no more. In other words, a serious
build for Java applications must understand Java dependencies. That
is the problem; let's look at some existing solutions.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e44" id="d0e44"></a>Current
solutions</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e47" id="d0e47"></a>IDEs</h3>
</div>
<p>The most common answer I get when asking about a project build
is, I'm sorry to admit, &quot;Why don't you just use JBuilder?&quot; - so my
answer is practiced by now: JBuilder doesn't scale. I believe (I
didn't try) that it may be useful for a home-based individual
developer, but an environment which does not handle multiple
languages, multiple users and variant (at least unit test vs.
delivery) builds is simply not good enough for production work.
That should not be taken as criticism of JBuilder (in fact, I'm
told that some of its more irritating limitations have been fixed
in latest versions), but rather as a criticism of &quot;visual&quot;
environments in general. Large-scale software development requires
<span class="emphasis"><em>programmable</em></span> tools, going
beyond &quot;what you see is all you get&quot;.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e55" id="d0e55"></a>make</h3>
</div>
<p>And indeed there are programmable build tools. The problem of
dependency management has been recognized (and solved) long ago,
and its solution is now a de-facto standard build tool: make. From
a popular manual [<a href="#make">make</a>]: &quot;The <tt class=
"literal">make</tt> utility automatically determines which pieces
of a large program needs to be recompiled, and issues commands to
recompile them.&quot;</p>
<p>It would be perfect, if only it were true... Standard make does
not automatically handle dependencies (not beyond &quot;$NAME.o always
depends on $NAME.c&quot;), and in consequence, projects using <tt class=
"literal">make</tt> either generate most of the content of their
makefiles by various add-ons (either make extensions, or external
programs), or just ignore the problem and build from scratch every
once in a while to flush the bugs out. A good overview of
<tt class="literal">make</tt> problems can be found (perhaps
unsurprisingly) in the documentation of an alternative build tool
[<a href="#CONS">CONS</a>].</p>
<p>And if the general problems of make weren't enough to look for
alternatives, there are also Java-specific ones: <tt class=
"literal">make</tt>'s build model is based on C compilation (source
files are compiled to object files, which are linked to make an
executable), but Java doesn't work that way. Multiple class files
can be generated from a single source, circular dependencies are
quite common and the generation of <tt class="filename">jar</tt>s
is strictly optional.</p>
<p>Considering that the purpose of dependency analysis is to
minimize compilation time, it is worth noting that <span class=
"application">javac</span> is very slow to start up. For a few
simple classes, it may not pay to check dependencies at all - the
startup time (or perhaps it is time <span class=
"application">javac</span> itself spends checking dependencies -
see below) dwarfs time actually spent compiling, and so it saves
practically no time to compile just part of the project.</p>
<p>On the other hand, there clearly are projects big enough that
<span class="application">javac</span> should not compile all their
sources, every time - there's no justification for build times
going up linearly with the size of the project. In theory,
<span class="application">javac</span> could handle dependency
management internally. In practice, that functionality (the -depend
flag of <span class="application">javac</span>) existed in early
versions of Sun's JDK, but it has been dropped in Java 2, and
<span class="application">javac</span> now recompiles only direct
out-of-date dependencies, when their sources happen to be found on
<tt class="literal">sourcepath</tt> or <tt class=
"literal">classpath</tt>. <tt class="literal">Sourcepath</tt> at
least is an explicit command-line argument, but the recompilation
of source files on <tt class="literal">classpath</tt> cannot even
be turned off... <tt class="literal">Make</tt> has a particular
problem with this &quot;feature&quot;, because make builds normally create
derived files in the same directory where the source is, and since
<span class="application">javac</span> must be able to find the
class files (to compile other sources depending on them), it also
checks the sources even when it shouldn't.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e125" id="d0e125"></a>A model for a
Java build</h2>
</div>
<p>So, if the current solutions are unsatisfactory, what should be
done instead?</p>
<p>I believe that a Java-aware build tool should have
languagespecific support (not only for Java, but for multiple
languages - at least C/C++) and that the support should be
comprehensive.</p>
<p>The tool should know which class files are generated from which
sources (this is important for further derivation, i.e. making
<tt class="filename">jar</tt>s), and which class files are required
to compile each source file.</p>
<p>The tool should handle circular dependencies, and even when
there aren't any, it should compile more than one updated source
file at once (by default probably all sources in one
directory).</p>
<p>Overall, it's a lot of &quot;should&quot; - are these requirements
realistic?</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e141" id="d0e141"></a>Enumerating
derived files</h3>
</div>
<p>In the simplest case, a Java source file defines one class; if
that class is public, it must have the same name as the file. But
not all classes are public, and non-public (top-level) classes can
be defined in any source file declaring classes of a given package.
Java also has inner classes, defined inside a top level class and
not necessarily having any name at all. As the JVM spec [<a href=
"#JVM">JVM</a>] says, &quot;Typically, a class or interface will be
represented using a file in a hierarchical file system. The name of
the class or interface will usually be encoded in the pathname of
the file.&quot; - but there are no guarantees, and certainly no
published algorithm to derive the class file name. Sun's &quot;anywhere&quot;
(as in &quot;run anywhere&quot;) just doesn't seem to include Java
environments from other providers...</p>
<p>But at least the package part is clear: a package corresponds to
a relative directory. The absolute, top-level directories prepended
to the relative ones are listed (together with <tt class=
"filename">jar</tt>s - in this context, a <tt class=
"filename">jar</tt> is just a directory abstraction) in <tt class=
"literal">classpath</tt>, which is specified by a command-line
argument (for command-line tools) or a user-settable option (for
GUI applications), an environment variable or some hardcoded
default (good only for the system classes). Pretty much everybody
handles <tt class="literal">classpath</tt> the same way.</p>
<p>For the name part, let's get empirical: what class file names
are actually created by Sun's <span class=
"application">javac</span>? It appears that all inner classes have
names constructed from multiple segments, separated by '$', where
the first segment is the name of their enclosing class. Named inner
classes (i.e. the only inner classes which can be referenced from
files other than the one defining them) simply concatenate all
names of their enclosing classes - for example, a class whose Java
name is:</p>
<pre class="screen">
pkg.name.TopLevel.Inner.NestedInner.
                        DeeplyNestedInner
</pre>
<p>is compiled into a file</p>
<pre class="programlisting">
TopLevel$Inner$NestedInner$
                  DeeplyNestedInner.class
</pre>
<p>(in the <tt class="literal">pkg/name</tt> directory). Anonymous
classes use two or three segments only. Unnamed classes have names
constructed from two segments, while local classes (i.e. named
classes declared in some method) need three segments (because local
classes with the same name may be declared in different blocks).
When there's no good name for a segment, a number is used instead;
the numbers start from 1 and go up (when necessary to keep the
whole name of every defined class unique) as the source is being
parsed.</p>
<p>Overall, it seems possible to pry open the black box and get the
class file names generated from a given source - provided we have a
Java parser. Fortunately, Antlr is Java-based and (like any
selfrespecting parser) parses its own programming language, so a
utility for this task is within reach.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e181" id="d0e181"></a>Java source
dependencies</h3>
</div>
<p>When parsing Java sources, it is naturally also possible to
notice the used class names and construct a dependency graph. The
hard part is determining what is a class name (as opposed to, say,
field name - see Section 6.5 of the Java spec [<a href=
"#JLS">JLS</a>]). Basically, a dependency analyzer must know about
all classes (on the <tt class="literal">classpath</tt>,
<span class="emphasis"><em>and</em></span> classes that may not
exist yet, but are defined in the project's sources) and use that
information to determine the meaning of a name. Admittedly, the
most complicated scenarios should be rare - perhaps it would pay to
cheat a bit... Also, since classes accessible across packages have
one-to-one correspondence between their source file and class file,
ignoring inter-package dependencies (and always recompiling all
sources in an updated package) would considerably simplify the
analysis.</p>
<p>A proof of this concept (including a demonstration of problems
with file-level granularity) is werken.javad [<a href=
"#Werken">Werken</a>]. In my view, it suffers from being a
<tt class="literal">make</tt> add-on, but it certainly can serve as
a basis to build from - all code I wrote to research this article
(and don't present it - it's just not good enough) is based on
werken.javad.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e203" id="d0e203"></a>Does anybody
care?</h2>
</div>
<p>So, technically, it seems possible to build a Java-aware build
tool - yet none exists. And I'm as guilty of it as anyone - I used
<tt class="literal">make</tt> to build my experimental code, and it
did not work right. New <tt class="literal">make</tt> replacements
like Apache Ant [<a href="#Ant">Ant</a>] don't do dependency
management at all. Is the problem of building Java applications
solvable, but just too hard to bother solving? IDEs do try to solve
it, but not very well (JBuilder, to pick my favorite whipping-boy,
does have some dependency management, but it's unreliable - it
doesn't handle files being removed from the project, for example),
and there is no commercial build tool which isn't an all-singing,
all-dancing, all-its-own IDE. I believe the problem is too hard for
homemade solutions, but also that there's more Open Source Java
projects than any one company has, and that these projects would
profit from a Java-aware build tool. I believe they could profit as
much as the GNU [<a href="#GNU">GNU</a>] projects profit from
autoconf [<a href="#autoconf">autoconf</a>] - a piece of software
whose development couldn't have been justified by competing Unix
vendors precisely <span class="emphasis"><em>because</em></span>
their customers find it so useful (to mix and match their
software).</p>
<p>Any takers?</p>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e228" id="d0e228"></a>References</h2>
</div>
<div class="bibliomixed"><a name="make" id="make"></a>
<p class="bibliomixed">[make] GNU make manual: <span class=
"bibliomisc"><a href=
"http://www.gnu.org/manual/make/html_node/make_toc.html" target=
"_top">http://www.gnu.org/manual/make/html_node/make_toc.html</a></span></p>
</div>
<div class="bibliomixed"><a name="CONS" id="CONS"></a>
<p class="bibliomixed">[CONS] CONS - A Software Construction System
<span class="bibliomisc"><a href=
"http://www.dsmit.com/cons/stable/cons.html" target=
"_top">http://www.dsmit.com/cons/stable/cons.html</a></span></p>
</div>
<div class="bibliomixed"><a name="JVM" id="JVM"></a>
<p class="bibliomixed">[JVM] The Java Virtual Machine
Specification: <span class="bibliomisc"><a href=
"http://java.sun.com/docs/books/vmspec/" target=
"_top">http://java.sun.com/docs/books/vmspec/</a></span></p>
</div>
<div class="bibliomixed"><a name="ANTLR" id="ANTLR"></a>
<p class="bibliomixed">[ANTLR] The ANTLR website: <span class=
"bibliomisc"><a href="http://www.antlr.org/" target=
"_top">http://www.antlr.org/</a></span></p>
</div>
<div class="bibliomixed"><a name="JLS" id="JLS"></a>
<p class="bibliomixed">[JLS] The Java Language Specification:
<span class="bibliomisc"><a href=
"http://java.sun.com/docs/books/jls/" target=
"_top">http://java.sun.com/docs/books/jls/</a></span></p>
</div>
<div class="bibliomixed"><a name="Werken" id="Werken"></a>
<p class="bibliomixed">[Werken] The Werken Digital website:
<span class="bibliomisc"><a href="http://code.werken.com/" target=
"_top">http://code.werken.com/</a></span></p>
</div>
<div class="bibliomixed"><a name="Ant" id="Ant"></a>
<p class="bibliomixed">[Ant] The Jakarta Project website (Apache
Ant): <span class="bibliomisc"><a href=
"http://jakarta.apache.org/ant/" target=
"_top">http://jakarta.apache.org/ant/</a></span></p>
</div>
<div class="bibliomixed"><a name="GNU" id="GNU"></a>
<p class="bibliomixed">[GNU] GNU website: <span class=
"bibliomisc"><a href="http://www.gnu.org/" target=
"_top">http://www.gnu.org/</a></span></p>
</div>
<div class="bibliomixed"><a name="autoconf" id="autoconf"></a>
<p class="bibliomixed">[autoconf] GNU autoconf information and
resources: <span class="bibliomisc"><a href=
"http://www.gnu.org/software/autoconf/" target=
"_top">http://www.gnu.org/software/autoconf/</a></span></p>
</div>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
