Home Java Java - Compiling a Java Program Your Ad Here
 

Search on this site

Related Items

Advertisement Partners

Job Search

Click here to search all jobs Powered by Monster.

Advertisements

Study in Russia
Java - Compiling a Java Program PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Admin   
Thursday, 19 February 2009 08:32

Java Compiler Tutorial

Purpose of this Lab

Selecting the right tool for the job is an important task for all computer scientists. As you know, no one tool is appropriate for all circumstances. A Swiss army knife is a great general-purpose tool, but at times, a scalpel or a chainsaw does a better job.

In this lab, you will learn four (!) different ways of compiling and running a Java program. As you work through the lab exercises, think about the advantages and drawbacks of each method. By the end of the lab, you may have a personal favorite that you want to use for your routine programming tasks. Just as importantly, you will have been exposed to a variety of tools that are valuable in different circumstances.
Checking the JDK Installation
To compile and run Java programs on your computer, you need to have a working installation of the Java Development Kit (JDK) from Sun Microsystems. The JDK includes command-line commands to compile and run Java programs.
You launch these commands directly from the Windows command shell that you encountered in the Windows lab.

Exercise 1
Open a command shell window. Describe what you did to open it.



Exercise 2
In the command shell window, type the command
javac -version
What output did you get?

If the output of the javac -version command starts with a version number such as
javac 1.5.0_04, then your computer already has the Java Development Kit installed.

It is ok if the version number is later than 1.5.0, but if it is earlier, then you need to install a newer version of the JDK.

NOTE: The Java version numbering scheme is a bit of a mess, thanks to the geniuses in Sun's marketing department. Java 5 is internally numbered as 1.5. In fact, the various versions of Java are officially named Java 1.0, Java 1.1, Java2 SE 1.2, Java2 SE 1.3, Java2 SE 1.4, Java2 SE 5.0, Java SE 6. The SE denotes the "standard edition". There is a micro edition (ME) for cell phones and other small devices, and an enterprise edition (EE) for servers.

If you get an error message such as "command not found" or "not recognized as an internal or external command, operable program or batch file", check that you typed java correctly. If you did and you still get an error message, then you need to install the JDK.

If you work on a computer in a school lab, the JDK should be properly installed. If the test in Exercise 2 fails, see a lab monitor for assistance. But if you work with your own computer, it is entirely possible that the JDK is not properly installed. In that case, go to Appendix 1 of this tutorial, follow the instructions for installing the JDK, and return here when you are done.

Running The JDK Compiler
In the following exercises, you will compile and run a Java program. You will need to create a working directory and place two Java files into it. If you have problems with these activities, you should review the Windows lab.

Exercise 3
In the command shell window, change to your personal directory. (If necessary, create it.) What commands did you issue?

Exercise 4
Inside your personal directory, create a subdirectory named compilerlab. What command did you issue?

Next, download the files CashRegister.java and CashRegisterTester.java and place them into the c:\yourname\compilerlab directory. (As always in this tutorial, c:\yourname denotes your personal directory. Be sure to substitute c:\jqsmith or whatever your personal directory is called.)

To download each file, follow these steps:
" Click on the link
" The browser will display the file
" Select File -> Save from the menu, or better, use the Ctrl+S keyboard shortcut
" In the file dialog, supply the complete path and name, such as c:\yourname\compilerlab\CashRegister.java. (Do not simply accept the default location-your browser will then save the file in the wrong place.)
" Hit the Back button in the browser to get back to the tutorial
" In the command shell, type the commands
cd c:\yourname\compilerlab
dir
" You should see a listing of files in the directory, which should include the file that you just downloaded
.
Exercise 5
Go ahead and download the two files now. Then list the files of the working directory for this lab. What is your directory listing?


Now we are ready to try the compiler. The compiler is invoked with the javac command. You tell the compiler which file or files you want to compile, such as
javac CashRegisterTester.java
javac CashRegisterTester.java CashRegister.java
javac *.java
The last command means "compile all Java files in the current directory".

Exercise 6
Type the command
javac CashRegisterTester.java
Then type the command
dir
What files are contained in the current directory?


The Java compiler doesn't execute the Java program-that is the job of the Java virtual machine. However, the Java virtual machine cannot execute .java files directly. The compiler's job is to translate Java source files into "class files" that the virtual machine can execute.

If you compile a Java source file and there are no compilation errors, the Java compiler produces one or more "class files" with extension .class. In our example, you should have obtained two class files: CashRegisterTester.class and CashRegister.class.

NOTE: The Java compiler is smart enough to compile all source files that are needed by the source files that you specify. For example, the CashRegisterTester class requires the CashRegister class. When you compile CashRegisterTester.java, the compiler automatically compiles CashRegister.java. (Be sure, however, that you save all your Java files before compiling. If your editor contains a change that you didn't save to disk, the Java compiler can't know about it.)

Now we are ready to execute the Java program. You start the Java virtual machine with the java command, followed by the class that contains the main method. In our case, you issue the command
java CashRegisterTester

CAUTION: The javac command takes file names as input. The java command takes a class name, without the .java or .class extension.

CAUTION: If the javac command works but the java command fails with a "class not found" error, then you need to fix the CLASSPATH setting. This is sometimes a problem on Windows systems, when a poorly written installer makes a global change to this setting. (QuickTime is the most common culprit.) Follow the description of setting the PATH in the Java/Windows installation at the end of this tutorial. In addition to adding the Java directory to the PATH, add the string
.;
to the beginning of the value of the CLASSPATH environment variable. (The . denotes the current directory, and ; is the separator between directories.)

Exercise 7
Execute the CashRegisterTester class. What is the output?

Comments
Search RSS
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 

Donate & Support Us

Enter Amount:

Login Form



Joomla Templates by Joomlashack