0% found this document useful (0 votes)
4 views3 pages

OOP CS3391 UNIT-1 (Lecture Note-9)

This document provides a step-by-step guide on compiling and running Java programs using the command prompt. It includes instructions for setting the compiler path, creating a Java file, compiling it with 'javac', and running it with 'java', along with two example programs. The examples demonstrate how to print 'Hello World' and double a user-input number.

Uploaded by

RajeeshKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

OOP CS3391 UNIT-1 (Lecture Note-9)

This document provides a step-by-step guide on compiling and running Java programs using the command prompt. It includes instructions for setting the compiler path, creating a Java file, compiling it with 'javac', and running it with 'java', along with two example programs. The examples demonstrate how to print 'Hello World' and double a user-input number.

Uploaded by

RajeeshKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Compiling and running a java program in command prompt

STEPS:
 Set the path of the compiler as follows (type this in command prompt):
Set path=”C:\Program Files\Java\jdk1.6.0_20\bin”;
 To create a Java program, ensure that the name of the class in the file is
the same as the class name of the file.
Save the file with the extension .java (Example: HelloWorld.java)
 To compile the java program use the command javac as follows:
javac HelloWorld.java
This will take the source code in the file HelloWorld.java and create the
javabytecode in a file HelloWorld.class
 To run the compiled program use the command java as follows:
java HelloWorld
(Note that you do not use any file extension in this command.)
Example 1: A First Java Program:

class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}

Save: HelloWorld.java
Compile: javac HelloWorld.java
Run: java HelloWorld

Output:
Hello World
Example 2: A Second Java Program to double the number:
import java.util.Scanner; // Scanner is a class which contains necessary methods to provide a user an
access to the i/p console.
public class Example
{
public static void main(String args[])
{
int num=0,res;
Scanner in=new Scanner(System.in); //creating object of Scanner class to access the i/p stream.
System.out.println("Enter a Number :");
num=in.nextInt();
res=num*2;
System.out.println("The double of given number is "+res);
}
}

To execute this program


Output:
Save: Example.java
Enter a Number: 25
Compile: javac Example.java
The double of given number is 50
Run: java Example2

You might also like