>Basic Java Programming Exercise

February 11, 2011 at 11:56 pm | Posted in Article, computer and high technology | Leave a comment

>Java is comprised of many different technologies to support many different types of application development. There’s the Java Foundation Classes (JFC a.k.a. Swing) for building rich-client applications. There’s the servlet and JavaServer pages technologies for creating modern server-side web applications. And of course, there’s Enterprise JavaBeans (EJBs) used to build highly-scalable back-end (enterprise) systems. From a learning perspective, a starting point for learning each technology within the Java platform is to create a hello world program.

As “hello world” programs go, the primary purpose of the exercise is to prove out the language semantics, the technology concepts, the development process, and the execution process. Typically, when you are successful, you are greeted with a “hello world” message.

In the Java SE platform, there are two primary types of applications. The first form is considered a stand-alone application. It’s only requirements to execute are a operating-system compliant version of the Java Runtime environment. Stand-alone applications can be headless (command-line, server-like applications) or incorporate a rich user interface. Stand-alone applications are typically executed with the Java application launcher known as java. It’s reasonable to think of the Java application launcher as the JRE, containing the platform libraries and the JVM.

The second form of a Java SE application is called an applet. Applets execute within a browser. Most modern web browsers can executed Java Applets using the JRE installed on the end-user’s operating system. The following steps will show you how to create a stand-alone, headless Java application.

Before you Begin

In order to successfully complete this exercise, you will need to ensure the Java compiler (javac) and the Java application launcher (java) are in your path. You can either adjust your path using system environment variables or in your shell. Many people create a environment variable known as java_home which refers to the installation directory of the JDK. They then adjust the path environment variable to include %JAVA_HOME%bin.

Creating and running a Java Application
1. Create a directory where you will place your files for this exercise. We are going to be using the directory C:work for these exercises in the screenshots.

2. Open a new file in text editor and enter the following code into the file. On Windows, you can use either Notepad or WordPad to create the text file.
public class HelloWorld {
public static void main( String [] args) {
System.out.println(“Hello World”);
}
}

3. Save the file as HelloWorld.java (be sure to capitalize the file name exactly as it is written here).

4. Open a command window or shell window and change to c:work directory (where you just saved the HelloWorld.java file). You might want to check that you are in the right directory by doing a directory listing.

5. Run the command javac HelloWorld.java at the command line. If you entered the program correctly, you should see something like the screenshot in figure 1-5. If you saw something else, it probably means that you entered the program incorrectly.

6. Once the program has compiled correctly, check to see that there is file named HelloWorld.class in the directory. If there isn’t, you more than likely experienced a compile error.

7. Run the Java application by typing java HelloWorld at the command prompt.

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: