
If you obtain an error message like error: cannot read: HelloWorld.java 1 error, your file is not in the current folder or it is badly spelled. If you do not know how to do this, consider reading through our crash courses for command-line applications for Windows or Linux.Ĭompile the Java source file using the following command which you can copy and paste in if you want: In your command-line application, navigate to the directory where you just created your file. Next, open your preferred command-line application.įor example, Command Prompt on Windows and, Terminal on Linux and Mac OS. This name is case-sensitive, which means you need to capitalize the precise letters that were capitalized in the name for the class definition. Save the file as HelloWorld.java - the name of your file should be the same as the name of your class definition and followed by the. Write the following lines of code in a new text document: Open your preferred text editor - this is the editor you set while installing the Java platform.įor example, Notepad or Notepad++ on Windows Gedit, Kate or SciTE on Linux or, XCode on Mac OS, etc. Proceed only if you have successfully installed and configured your system for Java as discussed here. To execute your first Java program, follow the instructions below: This is different from other languages like C/C++ where programs are to be compiled to machine code and linked to create an executable file before it can be executed. The Java classes/bytecode are compiled to machine code and loaded into memory by the JVM when needed the first time. When the bytecode is run, it needs to be converted to machine code.

Java programs need to be compiled to bytecode.Java code needs to be compiled twice in order to be executed: The result is machine code which is then fed to the memory and is executed. When the program is to be run, the bytecode is converted, using the just-in-time (JIT) compiler. The bytecode gets saved on the disk with the file extension. Java source code is compiled into bytecode when we use the javac compiler. In Java, programs are not compiled into executable files they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime.
