0
0
Javaprogramming~5 mins

Command line execution in Java

Choose your learning style9 modes available
Introduction

Command line execution lets you run your Java programs by typing commands. It helps you test and use your programs outside of an editor.

You want to run a Java program you wrote without using an IDE.
You need to test small Java programs quickly.
You want to share your program with others who can run it from their terminal.
You are learning Java and want to understand how programs run.
You want to automate running Java programs in scripts.
Syntax
Java
javac FileName.java
java FileName

javac compiles your Java code into bytecode.

java runs the compiled bytecode.

Examples
Compile and run a simple program named HelloWorld.java.
Java
javac HelloWorld.java
java HelloWorld
Run MyProgram with two command line arguments arg1 and arg2.
Java
javac MyProgram.java
java MyProgram arg1 arg2
Sample Program

This program prints "Hello, world!" to the screen. You compile it with javac HelloWorld.java and run it with java HelloWorld.

Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
OutputSuccess
Important Notes

Make sure your file name matches the public class name exactly, including capitalization.

Run the java command without the .class extension.

If you get errors, check your Java installation and your current folder.

Summary

Use javac to compile Java files.

Use java to run compiled programs.

Command line execution helps you run Java programs outside an editor.