• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Run Java Program in Command Line

Written by Rahul, Updated on March 3, 2017
JAVA java

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.

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

Share it!
Share on Facebook
Share on Twitter
Share on Google+
Share on Reddit
Share on Tumblr
Rahul
Rahul
Connect on Facebook Connect on Twitter Connect on Google+

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Related Posts

  • How to Install Oracle Java 11 on Ubuntu 16.04 LTS (Xenial)

    October 7, 2018
  • How to Install Java 11 on CentOS 7/6 & Fedora 29-25

    September 30, 2018
  • How to Install Oracle Java 11 on Ubuntu 18.04 LTS (Bionic)

    September 30, 2018
  • How to Switch Between Multiple Java Versions on Ubuntu 18.04 & 16.04

    September 5, 2018
  • How to Switch Between Multiple Java Versions on Debian 9/8

    September 5, 2018

Leave a Reply

Cancel reply

Popular Posts

  • How to Setup Squid Proxy Server on Ubuntu 18.04 & 16.04
  • How To Install Zabbix Agent on Debian 9/8
  • How to Install and Configure Squid Proxy on Debian 9
  • How to Remove “public/index.php” from URL in Laravel
  • How To Install Wine 3 on Debian 9 (Stretch)
All rights reserved. © 2013-2018 TecAdmin.net. This site uses cookies. By using this website you agree our term and services