How To Write & Compile Java Code In Command Line

Step 1: Check if Java is installed

execute java -version in your terminal.

If you are not able to see the java version installed, follow the instructions here.

You can also use homebrew on macOs by running brew cask install java in your terminal.

Step 2: Write Java code in your favorite editor,

in this case we will use vim . Create a new file named as shown below.

press i to enter INSERT mode, and type some java code.

press esc key to exit from INSERT mode. Press :wq to write and quit vim .

Step 3: Compile the java code.

Write the command javac <filename>.java to compile the code.

If your code has an error, a message will be displayed, indicating the line where the error occurred, and a brief description.

Recompile the code after fixing the error . If no error shows up in the terminal then the program compiled successfully.

Step 4: Run the program

Type java <filename> without the extension .java

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.