Compile And Run A Java Program Using Command Prompt

The following tutorial is going to take you step-by-step with the process of compiling and running java program. The easiest method to understand is to compile and run these programs on your own. I chose to make this because I've come across a lot of java beginners who does not know how to compile and run a java program.

To start with, we need to have JDK installed on our PC. You can easily download JDK from java website or you can also check Tutorial To Download And Install Java In Windows.

Compile Java Program Using Command Prompt :

Before Java virtual machine (VM) can run a Java program, Java source code needs to be compiled into byte-code using the javac compiler. In order to compile a Java source code of file helloworld.java you should do the following steps.

1. open cmd(command prompt) from run.
2. open directory in command prompt in which your java file is stored.
3. compile your java file using javac command.

Syntax To Compile A Java Program :
javac FileName.java

Example :
javac helloworld.java

4. If there are no errors in your source file, the Java compiler will produce .class file. For instance, the results of a successful compile of helloworld.java will produce a byte-code version of the class in a file named helloworld.class.

Running A Java Program Using Command Prompt :

1. After you have successfully compiled your Java source code, you'll be able to invoke the Java Virtual Machine to run your program byte code.
2. You can run a java program in command prompt using java command.

Syntax To Run Java Program In Command Prompt :
java FileName

Example :

java helloworld

Video Tutorial To Compile And Run A Java Program Using Command Prompt :

No comments:

Post a Comment