How To Write & Compile Java Code In Command Line
Step 1: Check if Java is installed
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
example.java as shown below.
i to enter
INSERT mode, and type some java code.
esc key to exit from
INSERT mode. Press
:wq to write and quit
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
java <filename> without the extension