Creating Java Projects Using Macintosh OS X Developer Tools


Creating Java Projects Using Linux

The issues related to Java programming in the Linux environment are similar to those encountered with the Microsoft Windows operating system. You will need to obtain the Java SDK, install it, and configure your operating system s PATH and CLASSPATH environment variables. You will also need to select a suitable text editor or IDE. Once you have properly installed the Java SDK on your Linux system you can create, compile, and run your Java programs just like you would in the Windows environment.

This section assumes you are running Linux on a personally-owned computer for which you have root access. If you are using Linux at a school computer lab and need access to the Java programming tools, you must seek the help of the computer lab administrator.

A Brief Note On The Many Flavors Of Linux

Linux is a popular, open-source, UNIX-like operating system that comes in many flavors and targets several different processors. The examples used in this section are derived from RedHat“ Linux version 6.x running on an a PC using an Intel iX86 or compatible processor.

As long as you run Linux on a PC, you should be OK when it comes to Java development. If your particular Linux distribution didn t come with Java developer tools, you can download the Java SDK from Sun. If, however, you are running Linux on a non-PC compatible computer, you may find that Java is not supported.

Obtaining and Installing Suns J2SDK For Linux

Most Linux distributions, such as RedHat, come with a set of developer tools that let you do software development on your particular system. For instance, RedHat ships with the GNU compiler tools g++, gcc, etc. For more information about the GNU project, visit the gnu.org website [ http://www.gnu.org/gnu/thegnuproject.html ].

If your distribution fails to include Java, or if you want to upgrade your Linux Java version, you can obtain the SDK from Sun s Java website [ www.java.sun.com ].

When you open the Java download page, you ll see two SDK download options: a self-extracting file and a self-extracting RPM module. For this example, I downloaded the self-extracting file. If you choose to download the self-extracting RPM module, you will have to consult the RPM utility documentation for installation instructions.

Figure 2-26 shows the j2sdk-1_4_2_02-linux-i586.bin file being saved to the /usr/local directory.

image from book
Figure 2-26: Downloading a Linux Java Self-Extracting Bin File

Selecting An Installation Location

After you download the j2sdk self-extracting bin file, you will have to decide on an installation directory. I recommend creating a Java subdirectory in the /usr/local directory. To create the directory, move to the /usr/local directory and type the following command:

 mkdir Java

After you create the Java directory, move or copy the j2sdk self-extracting bin file to the Java directory to perform the extraction. To extract the SDK in the Java directory, type the following command:

 sh *.bin

Figure 2-27 shows the self-extracting file being executed. When execution begins, you are presented with a license agreement. After reviewing and agreeing to the license agreement, the Java package-extraction process automatically begins. When the self-extracting file completes execution, you will see a new subdirectory as is shown in figure 2-28.

image from book
Figure 2-27: Executing the Self-Extracting Bin File

image from book
Figure 2-28: New Subdirectory Resulting from SDK Extraction

Figure 2-29 shows the contents of the j2sdk1.4.2_02 directory. Make a note of the path to the bin directory. If you installed the SDK in the /usr/local/Java directory the full path to the bin directory will be:

 /usr/local/Java/j2sdk1.4.2_02/bin

image from book
Figure 2-29: Contents of the j2sdk1.4.2_02 Directory

You will need to know this path to set the PATH environment variable

Setting The PATH Environment Variable (bash shell)

After installing the Java SDK, you need to set your shell s PATH environment variable. The PATH environment variable is used by the shell to locate executable commands. The PATH environment variable is already set to some default value. Most likely it is set to include the /bin and the /usr/bin directories. To check the PATH variable s current setting, use the env command as is shown in figure 2-30.

image from book
Figure 2-30: Checking Environment Variables with the env Command

As you can see from figure 2-30, the env command displays a lot of information. Inspect your listing for the PATH environment variable setting.

To set the PATH environment variable, you will need to su as root and edit the bash profile file located in the /etc directory. Add the following line to the end of the file:

 export PATH=$PATH:/usr/local/Java/j2sdk1.4.2_02/bin

When you ve finish editing the profile file, save the changes and restart Linux. To test the PATH variable, use the env command again and inspect the PATH variable as shown in figure 2-30, or type java -version at the prompt.

Setting The CLASSPATH Environment Variable (bash)

You can set the CLASSPATH environment variable the same way you set the PATH variable, by editing the bash profile file. The CLASSPATH variable can be set by adding the following command to the end of the profile file: export CLASSPATH=$CLASSPATH:/usr/local/Java/j2sdk1.4.2_02/jre/lib:./:./classes

Creating, Compiling, And Running Java Programs

With the SDK installed, and the PATH and CLASSPATH environment variables set, you are ready to create, compile, and run Java programs. To do this, you will need to create the Java source files using a text editor, compile the resulting source files with the javac command, and then execute the main application using the java command. I discuss these steps in detail in the following sections.

Creating Java Source Files

To create Java source files you ll need a suitable text editor. RedHad¤ Linux ships with several text editors that can do the job: vi and emacs.

vi

Vi is a compact yet powerful text editor used by professional programmers all over the world. If you are a novice Linux user you may find vi to be somewhat cryptic upon startup, and its array of commands overwhelming. But, with a little practice, and the memorization of a few commands, you can quickly learn enough about vi to create and edit Java source files.

The important thing to know about vi is that it has two primary modes of operation: command and input. When you use vi, you need to keep in mind which mode you are in. When you enter vi commands, you need to be in the command mode; when you want to edit text, you need to be in the input mode.

emacs

If you prefer to use the mouse and execute menu commands, then you may find emacs more user-friendly than vi. Emacs is an extremely powerful editing tool that can do more than just edit text. Emacs can be extended and customized. Unlike vi, emacs does not require the user to switch between command and input modes of operation. However, like vi, you will have to exert some effort to learn and understand how emacs works. But once you learn how to create, edit, and save files in emacs, you will be up and running in no time.

Creating Your Working Directory

Before you create your Java source files, you should create a working directory for the Java project you are currently working on. This working directory would usually be created as a subdirectory in your home directory. For instance, when I log into my Linux system, my home directory is /home/rick. I could create a working directory named Projects , and in that directory create subdirectories named Project1 , Project2 , etc. How you set up your working directory environment is largely a matter of taste.

Create And Save Java Source Files

Using the text editor of your choice, create and save the Java source files image from book SampleClass.java and image from book ApplicationClass.java given in examples 2.1 and 2.2 respectively. When you create these files, save them in your working directory using the required source-code package structure.

Compiling Java Source Files

After you have saved the source files you are ready to compile them. Use the javac compiler command to compile the .java files into .class files. You can compile the classes with the following command:

 javac -d ./classes src/com/pulpfreepress/jfa/chapter1/*.java

If you have any compiler errors you will need to edit the source file again, fix the errors, and repeat the compilation step. When the source files successfully compile the resulting .class files will be automatically placed in their proper package structure in the classes subdirectory.

Figure 2-31 shows the tree command being used to reveal the directory structure of the Project1 working directory after both .java source files have been compiled and the resulting .class files have been copied to the com/pulpfreepress/jfa/chapter1 directory.

image from book
Figure 2-31: Using the tree Command to Show Directory Structure

Running The Application

You are now ready to run the program. From the Project1 working directory type the following command to execute the main application class:

 java com.pulpfreepress.jfa.chapter1.ApplicationClass

Figure 2-32 shows the results of running the ApplicationClass in the Linux environment.

image from book
Figure 2-32: Running ApplicationClass in the Linux Environment

Quick Review

The issues regarding Java development in the Linux environment are similar to those associated with the Windows operating system. You must obtain and install the Java SDK, configure the PATH and CLASSPATH environment variables, select a suitable text editor with which to create the Java source files, compile them using the javac compiler command, and run the main application with the java command.

The actual procedure for setting the PATH and CLASSPATH environment variables depends on your choice of Linux command shells. If you use a shell other than sh or bash, consult a reference for that shell.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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