Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JAVA»How to Run Java Program in Command Line

    How to Run Java Program in Command Line

    By RahulMarch 3, 20171 Min Read

    Run Java Program in Command Line. To run a Java program, you first need to compile it. After compiling the java program a .class file created with the class name. Now you can run this class file.To compile java programs you must have JDK installed on your system. If you don’t have already installed, visit one of following links to install and configure Java on your system.

    Advertisement

    Run Java Program in Command

    Firstly, create a sample Java program in a .java file. Add below code to HelloWorld.java file and save it. The file name must be the same as class name with the main function. So if you want to change the file name, then change class name as well.

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

    Now compile the HelloWorld.java file using java compiler. This will create a HelloWorld.class file in current directory.

    $ javac HelloWorld.java
    

    Finally, run your java program which ‘java’ command followed by your class name.

    $ java HelloWorld
    

    On the successful run, you will see output like below.

    Run Java Program in Command

    Java
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Set JAVA_HOME environment variable on macOS

    How to Set JAVA_HOME environment variable on macOS

    What is the Access Modifiers in Java

    What are the Access Modifiers in Java

    Installing Java on Ubuntu 22.04

    How to Install JAVA on Ubuntu 22.04

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • 20 Basic Linux Commands for the Beginners (Recommended)
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.