Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5
URL url = new URL ("http://www.javaworld.com/columns/jw-qna-index.shtml");
System.out.println ("URL path: " + url.getPath ());
Since this code requires at least J2SE 1.3, I should use -target 1.3 when building it. Why force my users to deal with java.lang.NoSuchMethodError surprises that occur only when they have mistakenly loaded the class in a 1.2 JVM? Sure, I could document that my application requires J2SE 1.3, but it would be cleaner and more robust to enforce the same at binary level.
I don't think the practice of setting the target JVM is widely used in enterprise software development. I wrote a simple utility
class DumpClassVersions (available with this article's download) that can scan files, archives, and directories with Java classes and report all encountered class version stamps. Some quick
browsing of popular open source projects or even core libraries from various JDKs/J2SDKs will show no particular system for
class versions.
When translating Java source code, the compiler needs to know the definition of types it has not yet seen. This includes your
application classes and core classes like java.lang.StringBuffer. As I am sure you are aware, the latter class is frequently used to translate expressions containing String concatenation and the like.
A process superficially similar to normal application classloading looks up a class definition: first in the bootstrap classpath,
then the extension classpath, and finally in the user classpath (-classpath). If you leave everything to the defaults, the definitions from the "home" javac's J2SDK will take effect—which may not be
correct, as shown by the Hello example.
To override the bootstrap and extension class lookup paths, you use -bootclasspath and -extdirs javac options, respectively. This ability complements the -target option in the sense that while the latter sets the minimum required JVM version, the former selects the core class APIs available
to the generated code.
Remember that javac itself was written in Java. The two options I just mentioned affect the class lookup for byte-code generation.
They do not affect the bootstrap and extension classpaths used by the JVM to execute javac as a Java program (the latter could be done
via the -J option, but doing that is quite dangerous and results in unsupported behavior). To put it another way, javac does not actually
load any classes from -bootclasspath and -extdirs; it merely references their definitions.
With the newly acquired understanding for javac's cross-compilation support, let's see how this can be used in practical situations.
This is a very common case: several J2SE versions support your application, and it so happens you can implement everything via core APIs of a certain (I will call it base) J2SE platform version. Upward compatibility takes care of the rest. Although J2SE 1.4 is the latest and greatest version, you see no reason to exclude users who can't run J2SE 1.4 yet.