CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Compile and Run Java Program

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Compile and Run Java Program

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: July 9, 2021 · 3 min read · 0 Comments
Share: in X
Compile and Run Java Program

Are you new to Java and trying to learn how to compile and run a simple Java program? Look no further! In this tutorial, I will walk you through the steps to compile and run your first Java program, as well as troubleshoot any issues you may encounter along the way.

Before we begin, make sure that you have the Java Development Kit (JDK) installed on your machine. If you do not have the JDK installed, you can download it from the Oracle website.

Following are the steps to compile and run a Java program:

Compile and Run Java Program

Table of Contents

  • Write the Java Program
  • Open a Command Prompt or Terminal
  • Navigate to the Directory
  • Compile Java Program
  • Run Java Program
  • Troubleshooting: javac or java is not recognized
  • Frequently Asked Questions
    • How do I compile a Java program?
    • What happens during the compilation process?
    • How do I run a compiled Java program?
    • What is the purpose of the main method in Java?
    • Can I specify command-line arguments when running a Java program?

Write the Java Program

Use a text editor to write your Java code. Save the file with a .java extension. For example, you could save it as HelloWorld.java.

Example code:

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

Open a Command Prompt or Terminal

Open a command prompt (Windows) or terminal (macOS/Linux).

Navigate to the Directory

Use the cd command to navigate to the directory where your Java file is saved. For example, if your file is saved on the desktop, you can use the command cd Desktop to navigate to the desktop directory.

Compile Java Program

In the command prompt or terminal, enter the following command to compile the Java program:

javac HelloWorld.java

If there are no errors in your code, the Java compiler will generate a bytecode file with a .class extension. For example, if your file is HelloWorld.java, the compiler will generate HelloWorld.class.

Run Java Program

After the compilation is successful, you can execute the Java program using the java command:

java HelloWorld

The JVM will start executing the bytecode and your program will run. Any output or results will be displayed in the command prompt or terminal.

Hence, in our example we see an output:

Hello World

It’s important to note that the javac and java commands assume that you have Java Development Kit (JDK) installed on your system and that the java and javac executables are in your system’s PATH variable. If you encounter any issues, make sure that Java is properly installed and configured on your machine.

Troubleshooting: javac or java is not recognized

If you receive an error saying that the javac or java commands are not recognized, it means that the system cannot find the executables for these commands. This can happen if the PATH environment variable is not set correctly.

You can read my other post on How to solve error “javac or java is not recognized”.

Frequently Asked Questions

How do I compile a Java program?

Compile a Java program using the javac command followed by the source code filename with the “.java” extension.

What happens during the compilation process?

During compilation, the Java source code is translated into bytecode, which is executed by the Java Virtual Machine (JVM).

How do I run a compiled Java program?

Run a Java program using the java command followed by the class name (without the “.class” extension) that contains the main method.

What is the purpose of the main method in Java?

The main method is the entry point of a Java program. It is where the program execution begins.

Can I specify command-line arguments when running a Java program?

Yes, you can pass arguments to a Java program by including them after the class name, and these arguments can be accessed within the main method using the args parameter.

Related Posts:

  • Install and Set Up Java Development Environment
  • Control Statements in Java
  • Difference Between JDK JRE and JVM
  • What is Java? Exploring Its Key Features, Benefits,…
  • Host Static Website on S3 AWS: Step-by-Step Guide
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
Was this article helpful?
← Previous ArticleIncrease EBS volume size in AWS EC2
Next Article →Create Organization in AWS

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap