Compiling and running a Java program is the first step in learning, especially in java, since this process is a little different than in other languages. The process involves writing source code, compiling it into bytecode, and then running the compiled program. To begin with, all that is required is an editor for writing source code and a command prompt for interacting with the compiler.
Once written, source code can be saved as. java files which are then fed into the Java compiler from within the command line interface (CLI). This compiles them into class files containing bytecode which can be run on any machine equipped with a JVM (Java Virtual Machine).
● Compiling:
Hence to compile the java program, type the following command in CLI: For example, let’s say the file name is FirstProgram.java, you will have to type the following command:
javac FirstProgram.java
● Running:
Finally, these compiled applications can be executed by simply typing ‘java’ followed by their name at the CLI and pressing enter - thus completing both the compilation and execution of your program in one step!
Example: java FirstProgram
Shortcut: Some IDEs like Eclipse, IntelliJ IDEA, and Netbeans provide a shortcut to compile and run the code by using the "run" button or "ctrl+f11" or "cmd+f11" respectively.
Hence, once the java code is written, it has to be saved with .java, and then it has to be compiled and executed.