"Hello World!" program

"Hello World!" program is the first lesson which people learn in programming code. Before we start, make sure we already JDK on our PC.  If you don't have please download it.

Download JDK

After download the JDK, install the software.
JDK will be used to compile the source code (.java) file into bytecodes (.class) file.
[How it work?]
I would recommend any newbie like me to read the link above before continues.

Here the source code for "Hello World!" Program.
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }//End of main
}//End of HelloWorld Class

  1. Open a notepad, copy the source code into the notepad then save as HelloWorld.java (case sensitive).
  2. Now you will need to use the JDK to compile the source code (.java) file into bytecode (.class) file. On your windows, go to DOS or command prompt  (click start > run > type cmd]
  3. On DOS, Compile the file from DOS prompt by typing javac HelloWorld.java. Successful Compilation, results in creation of .class containing byte code
  4. Execute the file by typing java HelloWorld

 In short, the procedure of saving, compiling and running java work in this way
Step 1:Save the program With .java Extension.
Step 2:Compile the file from DOS prompt by typing javac <filename>.
Step 3:Successful Compilation, results in creation of .class containing byte code
Step 4:Execute the file by typing java <filename without extension>

Troubleshooting
In case, you faced any error when entering command in DOS, here are the troubleshoot.


1. ‘javac’ is not recognized as an internal or external command, operable program or batch file
When you get this error, you should conclude that your operating system cannot find the compiler (javac). To solve this error you need to set the PATH variable.
How to set the PATH Variable?

Firstly the PATH variable is set so that we can compile and execute programs from any directory without having to type the full path of the command. To set the PATH of jdk on your system (Windows XP), add the full path of the jdk<version>\bin directory to the PATH variable. Set the PATH as follows on a Windows machine:

a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.
c. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:

C:\jdk<version>\bin (jdk<version is nothing but the name of the directory where jdk is installed)

If there are already some entries in the PATH variable then you must add a semicolon and then add the above value (Version being replaced with the version of JDK). The new path takes effect in each new command prompt window you open after setting the PATH variable.

2. Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorld
If you receive this error, java cannot find your compiled byte code file, HelloWorld.class.If both your class files and source code are in the same working directory and if you try running your program from the current working directory than, your program must get executed without any problems as, java tries to find your .class file is your current directory. If your class files are present in some other directory other than that of the java files we must set the CLASSPATH pointing to the directory that contain your compiled class files.CLASSPATH can be set as follows on a Windows machine:

a. Click Start > Right Click “My Computer” and click on “Properties”
b. Click Advanced > Environment Variables.

Add the location of classes’ folder containing all your java classes in User Variables.

If there are already some entries in the CLASSPATH variable then you must add a semicolon and then add the new value . The new class path takes effect in each new command prompt window you open after setting the CLASSPATH variable.

Reference:
http://www.javabeginner.com/learn-java/getting-started-with-java

No Response to ""Hello World!" program"

Post a Comment

Related Posts Plugin for WordPress, Blogger...