We will use the Customers table to illustrate the SQL LIKE clause usage: FirstName LastName Email DOB Phone John Smith John.Smith@yaho...
Monday, February 23, 2009
Exercise 3.1: Write, Compile, and Run Hello Java Program
Introduction:
The goal of this exercise is to let you experience a complete development cycle - writing, compiling, and running a simplest possible Java program - using command line tools. If you have done any programming in the past using different programming languages such as C or C++, this is not that much different from it. (There is a slight difference, however. In Java, the compiler javac compiles the Java source code into what is called bytecode which can be run on any Java compliant platform, thus provides the portability of the Java programs. The bytecode is the same thing as Java class file, which is represented by *.class file notation.)
You will also get some exposure to the concept of the classpath. The classpath is the most basic but essential concept you will need to understand - it is basically a location where *.class files reside. If the classpath is not set up correctly, you will experience the infamous "java.lang.NoClassDefFoundError: " exception.
Steps to follow:
1. mkdir c:\myjavaprograms (to create a directory of your choice - this is the directory where you are going to write Java programs) 2. cd \myjavaprograms (this directory becomes the current directory) 3. Write Hello.java using your editor of choice (in this example, I am using jedit) as shown in Code-3.1-a below. You can cut and paste the code from the Code-3.1-a but I encourage you to write the code line by line yourself manually just to experience some compile errors.
* jedit Hello.java
public class Hello {
/** * My first Java program */ public static void main( String[] args ){
// Print the string "Hello world" on screen System.out.println("Hello world");
}
} Code-3.1-a: Hello.java
4. Compile Hello.java using javac compiler. The javac compiler comes with J2SE SDK you've download. It resides in %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Solaris/Linux) directory. The result of compilation will be the creation of Hello.class file.
* javac Hello.java
Trouble-shooting 3.1.a: If you experience the following error condition, it means the %JAVA_HOME%\bin for Windows platform or $JAVA_HOME/bin for Solaris/Linux platform is not set in your path. You can try C:\Program Files\Java\jdk1.5.0_06\bin\javac Hello.java (for Windows) if you want to proceed without setting the path.
* C:\myjavaprograms>javac Hello.java 'javac' is not recognized as an internal or external command, operable program or batch file.
Trouble-shooting 3.1.b: If your Hello.java program contains a problem such as a typo or missing semi-colon after each statement, you will experience compile errors like an example compile error below (I misspelled it - it should have been System.out.println("Hello world"); instead of Syste.out.println("Hello world");.
* C:\myjavaprograms>javac Hello.java Hello.java:9: package Syste does not exist Syste.out.println("Hello world"); ^ 1 error
Trouble-shooting 3.1.c: If you name your file as hello.java (instead of Hello.java), you will experience the following problem. It is because the file name, hello.java, and the class name inside the code does not match. Inside the code, you use Hello in "public class Hello" statement and you name your file as hello.java. They have to match. And Java is case sensitive. hello.java and Hello.java are different files as far as Java is concerned. So rename the file to Hello.java and do the compilation again.
* C:\myjavaprograms>javac hello.javahello.java:1: class Hello is public, should be declared in a file named Hello.java public class Hello { ^ 1 error
Experimentation 3.1.d: Try "javac -verbose Hello.java" to see what is happening when you run javac. You really don't need to understand what is happening underneath at this point, however, except that it reads Hello.java file and then created Hello.class at the end.
Experimentation 3.1.e: Try "javac -help" to see what options you can specify when you compile Java code using javac compiler. You don't need to understand every option right now but the important options are "-classpath ", "-cp ", "-sourcepath ", "-d ". Please feel free to play around with these options. Among these four, "-classpath ", "-cp " are more important to understand. By the way, "-classpath ", "-cp " are the same thing.
No comments:
Post a Comment