What is necessary to prepare to start Java programming?
1. Prepare the Java runtime environment
1.1. Install JDK or JRE?
These are the two basic Java components that you will encounter frequently. Both are libraries and tools to support java, providing a foundation for running and compiling java programs. Below is a summary comparison of these two concepts.
Criteria | JRE | JDK |
---|---|---|
Abbreviation for? | Java Runtime Environment | Java Development Kit |
What to do? | Used to run programs written in Java | Used to compile Java code into executable programs |
For who? | For users who want to use programs written in Java | For programmers to write java programs |
Include | JVM (java virtual machine, where the program is run) and java libraries | JRE and other development tools |
Note, the JDK contains the JRE inside, so when you install the JDK, your computer also has a JRE. And as shown in the picture, the JDK contains the JRE in addition to additional development tools to help programmers in writing code.
If you are a programmer, want to use java to write software, use the JDK. Conversely, if you only want to use apps written in Java (like Minecraft), just install JRE.
1.2. Select version and install
There are two popular types of JDK (and JRE) currently available: Oracle and OpenJDK:
- Oracle requires a license for commercial use
- OpenJDK is completely free
In addition, there are some other points between them, such as release schedule, performance (slightly different), … however the java code is still quite similar. For a programmer, both can be used to learn to code.
Download the JDK and JRE at the Oracle homepage https://www.oracle.com/java/technologies/javase-downloads.html . The installation steps after that you can do it yourself, so I do not discuss it.
Regarding the version, I encourage to use version 11, because this is the most stable (stable) version and has LTS (Long terms support). If you want to use the latest java features, install JDK 15 (latest current). Although java 8 is the most popular, but will soon move to java 11, as both are LTS.
Small note about the Java version number, this part is probably unknown. From java 8 onwards, the JDK version number is the same as the Java version (for example, java 8 is JDK 8, java 11 is JDK 11). As for versions before java 8, the JDK is 1.X
, for example, java 8 is JDK 1.8.
1.3. Set an environment variable
Once installed, you need to set the environment variable for the JDK (or JRE). The goal is to be able to run javac
or java
anywhere, every different directory in the computer.
First, you find the JDK installation directory, the default is C:Program FilesJavajdk-15.0.2
. Copy that path.
Then open up System properties (using Windows search or using Control panel> System). The dialog box appears as follows, click on Environment Variables .
Then in the System variables section, create an entry called JAVA_HOME
(or JDK_HOME
) with the value of the JDK directory copied above.
Then in the section above User variables for User , find the Path section and double click on it. Add an entry of %JAVA_HOME%bin
(or %JDK_HOME%bin
for the above respectively).
That’s it, the environment variable has been set.
2. Java programming IDE
Next, you need a good IDE to facilitate java code support. Currently there are 3 quite popular candidates:
- Eclipse: simple, light, easy to use
- IntelliJ IDEA: great, but quite heavy
- NetBean: I don’t use it because I don’t like its interface very much
For beginners, I recommend using Eclipse. This IDE is functional enough, and quite lightweight and easy to use, perfect for beginners.
In addition, VSCode can also code Java, but need to install a plugin. You can learn more.
3. Start to code and build the program
3.1. The first show
After you have installed the JDK and the IDE, you are ready to code the first program – Hello world . You create a new project, then create a file called HelloWorld.java
and enter the following code.
1 2 3 4 5 6 | <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloWorld</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> <span class="token class-name">String</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> args <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span> <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> <span class="token string">"Hello World"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
At this point you do not need to understand the meaning of each keyword in the program above, just know 3 things:
- The
main()
function is where the Java program starts - The command
System.out.println()
is to print text to the screen (console) - The name of the main class
HelloWorld
must match the file nameHelloWorld.java
(case-sensitive).
To run the program, click Run (depending on the IDE) and see the results with the words “Hello World” printed on the screen.
3.2. Build with the command line
Although you already have an IDE that supports building and running the program, you also need to know how to build code written manually (using the command line). Consists of 2 steps, corresponding to 2 statements:
- Compile source code in
.java
file into.class
file (containing bytecode) - Run the compiled bytecode
.class
file
1 2 3 4 5 6 | <span class="token comment"># Biên dịch HelloWorld.java thành HelloWorld.class</span> javac HelloWorld.java <span class="token comment"># Chạy file đã biên dịch HelloWorld.class (không cần thêm .class)</span> java HelloWorld |
As a side note, the above two commands are in two different parts of Java:
javac
belongs to the JDK, used to compile Java codejava
belongs to the JRE (the JDK contains the JRE, so it also containsjava
), used to run Java bytecode
Therefore, when you cannot run javac
, it means that your computer has only JRE and no JDK. The JDK needs to be installed to compile Java code.