How to run a Java program in CMD

Ads
 

How to run a Java program in CMD

The article describes How to run a Java program in Command Prompt(CMD) using Notepad. Here you will also find tips to set the Java Class Path.

The article describes How to run a Java program in Command Prompt(CMD) using Notepad. Here you will also find tips to set the Java Class Path.

There are multiple ways to write and run Java programs. In this article we discussed how to write and run Java program in CMD.

Tips to run Java program in command prompt.

  •  Create a myproject folder in C folder
  • Open a notepad or any other editor and write a small program
    in Java as given below.
public class HelloWorld
{
public static void main(String[] args)
 {
System.out.println("Hello, World!");
	}
		}
  •  Save the program file as 'HelloWorld.java' in your C:\myproject folder and cross check the file name it must be
    saved as 'HelloWorld.java'.
  • In windows go to the Start Menu and open the command prompt.
  • Change the directory from c to C:\myproject. To change the directory type the given command in your command prompt.

Cd \myproject and press enter. It will change the folder.

  • Run the dir command to check if the 'HelloWorld.java' exist or not.
  • Make sure that your Java Path is set. If not then you can set it by running following command at your command prompt.

set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

  • Now time to compile your program. Write the following command to your command prompt and press enter.

Cd \myproject> javac HelloWorld.java it should compile the file.

  •  Now time to execute the program. Run the given command.

C:\myproject> java HelloWorld

This will run the Java interpreter and You will see the program output as follows:

Hello, World!

Ads

Ads