Basic Java Programming Exercise
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.
8. You should see the output shown in figure 1-6.
Author Bio: Develop Intelligence is the leader in Java Training and html5 training. To Schedule your own training visit http://www.developintelligence.com
Category: Computers and Technology
Keywords: Java Training, HTML5 training, Javascript