This article introduces basic Java programming example from coding to execution. We begin with examples of programs that display simple messages on the screen. We then present a program that obtains name of a user, and displays name numerology results in the next article.
This article uses tools from the Java SDK to compile and run programs. We recommend you to start your first program without IDE to understand compiler and class path settings.
A Java application is a computer program that executes when you use the java command to launch the Java Virtual Machine (JVM). In this article we will discuss how to compile and run a Java application.
First we consider a simple Java application that displays a line of text, “Welcome to Java World!”.
|
|
Step 1: | First download and install Java Development Kit (JDK) from official Oracle site. The JDK is the software environment where you can compile and run your Java programs. Download at http://www.oracle.com/ |
|
|
Step 2: | The knowledge about commands to compile and run a Java program. Before that you need to set the path to point your Java compiler on your development location. Assume that you have installed JDK on your C:\Program Files\Java\jdk1.8.0 drive. You need to run the following line of code in your command prompt before accessing Java compiler.
#command for Windows
SET PATH = C:\Program Files\Java\jdk1.8.0\bin; #command for Linux Read more about path settings at https://docs.oracle.com/
Open start menu and type cmd on your Windows machine to open command window. Now open your folder where your Java programs are saved. For example: D:/JavaPrograms. Note that “jdk1.8.0\bin” folder contains Java compiler (javac) and Java interpreter (java) applications. |
|
|
Step 3: | Now you are ready to run a sample Java program on your machine. Just copy the following content and paste it in notepad or any text editor. Save as WelcomeJava.java. // WelcomeJava.java // Welcome to Java World! program. public class WelcomeJava { // main method begins execution of Java application public static void main( String[] args ) { System.out.println( "Welcome to Java World!" ); } // end method main } // end class WelcomeJava |
|
|
Step 4: | Compile your program. Just open your working directory where you saved these .java file and follow the below steps. Open command prompt and go to your working directory.Type SET PATH = C:\Program Files\Java\jdk1.7.0\bin; // recall Minute 2 session Type javac *.java or javac WelcomeJava.java This command will compile WelcomeJava Java file. This action will generate WelcomeJava.class file inside your working directory. |
|
|
Step 5: | Run your application. Open command prompt and go to your working directory. Just type java WelcomeJava in your command window.You will see the following output. ______________________________
|
|
Congratulations!, Your first Java program is done.
One of the best ways to develop your Java knowledge is by MyExamCloud’s Java Certification Study Plans. You can find different Java Certification study plans at http://www.myexamcloud.com/
Checkout more Java articles at http://www.epractizelabs.com/