Section 5.4. The Java Runtime Engine


5.4. The Java Runtime Engine

You can download and install just the software required to run Java applications without the development tools. This is known as the Java 2 Runtime Edition.

5.4.1. The Basics

You run a Java program by invoking the java command. Usually, the argument to the command is a class name. That class is loaded, and its main() method is run.

Remember, this is not a filename, but a class name! The rest of the command-line arguments that follow the class name are passed as an array of Strings to the main() method of the named class. Example 5.4 demonstrates running the FetchURL program.

Example 5.4. Running a Java program with java
 $ java FetchURL http://www.yahoo.com/news http://www.yahoo.com/news: <html> <head><title>Yahoo! Directory News and Media</title> <script type="text/javascript"   src="/books/3/47/1/html/2/http://us.js1.yimg.com/us.yimg.com/lib/common/yg_csstare.js"> </script> <style>   li { font-size: 12px; margin-bottom: 2px; } </style> <base href=http://dir.yahoo.com/News_and_Media/></head> <body> ... ... etc. 

Caution

Remember that if you override the classpath, either with the -classpath or -cp command-line options (detailed below) or with the CLASSPATH environment variable, you must include the "." directory somewhere in the classpath if you want Java to include current working directory in the search list. Since "." is the default classpath, many people are surprised when they set a classpath and suddenly can no longer run a .class file in their current directory.


The class invoked must be findable on either the bootstrap or user-specified classpath.

5.4.2. java Options

Just as the Java compiler, the runtime program, java, takes a number of command-line options. Here are the most commonly used ones:

-classpath or -cp

Sets the runtime classpath. Overrides any value in the CLASSPATH environment variable.

-Dproperty=value

Allows a system property to be set on the command line.

-jar

Specifies that the first nonoption command-line argument is not a Java class name, but the name of a JAR file. The JAR file must have a Main-Class: specification in its MANIFEST (see Section 5.11). The main() method of the class named by the JAR's MANIFEST Main-Class: specification will be called as if that class had been named on the command line, rather than the JAR file. This is commonly used in shell scripts and batch files that accompany Java applications distributed in single .jar files.

There are several other command-line options that are less commonly used. We will cover some of them in later chapters when their use will make more sense. Of course, full documentation on all options for this command is in the Sun Java SDK Development Tools documentation.



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net