How to Compile Java in Command Prompt Or in a Terminal

How to Compile a Java Program in Command Prompt in a Terminal: In this JAVA tutorial, we will see how to write a simple JAVA program and compile and run the JAVA program in a command prompt.

public class SeleniumJavaProgram 
{
   public static void main(String[] args) 
   {
      System.out.println("This Is My First Program");
   }
}

if we run this program, then the output should come as This is my first program in the JAVA selenium series

How to Run the JAVA program in the Command prompt

  • Before writing the program, some prerequisites exist, like installing Java and setting JAVA_Home Variable on your system.
  • You can copy and paste it into an editor like Notepad or TextEdit or write in any IDE like Eclipse, NetBeans, or IntelliJ IDEA, available online for free download.
  • After writing the program, save that program as “SeleniumJavaProgram.Java”.
  • The next step after saving the program is to compile the program. for compilation, you can open the command prompt and terminal using the Mac operating system.
  • To compile the program, type the command below and hit enter.
javac SeleniumJavaProgram.Java

Open Command Prompt in Folder

Compile In Java Command Prompt
Compile In Java Command Prompt

Note: After hitting enter, if you get any error like this: “Javac’ is not recognized as an internal or external command, operable program or batch file,” the path is not set in your system. From here, you can learn how to set Java paths in Windows.

  • Once you set the path after you compile the Java program, you do not get any error, and after a successful compilation, the Java file is converted into a .class file, which is byte code.
  • Now, we can run the Java program in the command prompt. To run the program, type the command below and hit enter.
Java SeleniumJavaProgram

How to Compile a Java Program in Command Prompt:

Execute Java Command Prompt
Execute Java Command Prompt

Please write comments if you find anything incorrect, or you want to share more information about this topic discussed above, then you can use our Contact Us page.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment