01.09.2014 Views

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

The Linux Development Platform Configuring, Using, and ... - Classes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CH09.fm Page 270 Monday, October 7, 2002 9:18 PM<br />

270 Chapter 9 • <strong>Platform</strong> Independent <strong>Development</strong> with Java<br />

9.5 Building Java Applications<br />

A Java application is a compiled Java program that can be executed using a Java virtual machine.<br />

<strong>The</strong>re are three basic steps to create a Java application. <strong>The</strong>se steps are:<br />

1. Create source code.<br />

2. Compile it.<br />

3. Run it using the virtual machine.<br />

Let’s see how you can go through these steps.<br />

9.5.1 Creating Source Code File<br />

Like any other programming language, Java source code files are created using an editor.<br />

<strong>The</strong>se source code files contain Java instructions. <strong>The</strong> simplest source code file contains an<br />

instruction to print the message “Hello World”, similar to your first C program. Please note that<br />

Java examples in this chapter are just to demonstrate how to use development tools.<br />

<strong>The</strong> program that prints the “Hello World” message is shown below:<br />

class HelloWorld {<br />

public static void main(String[] args) {<br />

System.out.println("Hello World!");<br />

}<br />

}<br />

<strong>The</strong> program must have a function main(), like C programs. <strong>The</strong> program may be saved<br />

as HelloWorld.java text file.<br />

Note that you can use any editor to create the source code file. I would recommend using<br />

Emacs which you have already learned in Chapter 2. Emacs underst<strong>and</strong>s Java programming language<br />

syntax. <strong>The</strong>re are other GUI editors <strong>and</strong> integrated development environment (IDE) packages<br />

that you can use to create Java source code.<br />

9.5.2 Compiling Java Code<br />

<strong>The</strong> Java compiler will compile the Java code into an executable. <strong>The</strong> following comm<strong>and</strong><br />

creates an output file HelloWorld.class from source code file HelloWorld.java.<br />

/usr/java/j2sdk1.4.0/bin/javac HelloWorld.java<br />

Note that the output file name is the same as the name of the class that contains function<br />

main().<br />

You can check the type of the output file type by using the file comm<strong>and</strong>. <strong>The</strong> following<br />

comm<strong>and</strong> shows type of the output file as compiled class data.<br />

[root@desktop /root]# file HelloWorld.class<br />

HelloWorld.class: compiled Java class data, version 46.0<br />

[root@desktop /root]#

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!