Friday, November 17, 2017

How to Install, Compile And Run a Hello World Java Program

How to Install Java

  • Installs the JDK if you don't have installed it, download the JDK and install it.
  • Set the path of the JDK/bin directory.
  • Set path=C:\Program Files\Java\jdk1.6.0_23\bin
  • Create the java program
  • Compile and run the java program

Compiling and Executing Java Program

1. Compiling source code into bytecode using javac compiler.

2. Executing bytecode program using java interpreter.

Example of Hello World Java Program

/*
This is a simple Java program.
Call this file "Example.java".
*/
      class Example 
         {
// Your program begins with a call to main().

                    public static void main(String args[]) 
                  {
                   System.out.println("This is a simple Java program.");
                   }
          }

Run Java Program From Command Line

To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here: hello world program in java using notepad.

C:\>javac Example.java

  • The javac compiler creates a file called Example.class that contains the bytecode version of the program.
  • The Java bytecode is the intermediate representation of your program that contains instructions the Java interpreter will execute.
  • Thus, the output of javac is not code that can be directly executed. To actually run the program, you must use the Java interpreter, called java.
  • To do so, pass the class name Example as a command-line argument, as shown here:

C:\>java Example


Run Java Program From Command Line

No comments:

Post a Comment

Top 200+ Core Java Interview Questions And Answers For 2019

Most Popular Java Interview Questions Given below is a comprehensive list of most important and commonly asked basic and advanced Java p...