I finally had the motivation to continue my blogging activity, and I also felt like making some Java code to spend my times along. After all, it’s been close to 4 years since I touched Java. I wondered what Java is like right now, and I have found a favorite text editor that I wanted to try Java out in: Visual Studio Code!
While I was scouring around for information on setting up a new Java environment, it occurred to me that:
- Java 6 and Java 7 are unsupported, which is understandable.
- Starting from Java SE 7, documentations and standards are now matching with OpenJDK reference.
- Java 8 is no longer LTS, and it has just reached its end of life support, also understandable.
- Java 11 is the new LTS.
- Java 12.0.1 just released very recently.
- New licensing issues stemmed from Java 8, thanks to Google vs. Oracle lawsuit. This lawsuit is still currently pending as of April 2019.
- A completely complicated mess on figuring out which Java Development Kit to use, based on licensing. (To a newcomer, no less.)
Just to put it short, since I mainly do game development in Java, the best JDK environment to use is the OpenJDK. This is my decision tree:
- I am only allowed to use Oracle JDK 12 if I’m writing Java code for personal use, development, Oracle approved product use, and/or Oacle Cloud infrastructure use. (Per licensing.)
- For everything else, including game development, I would need to pay Oracle for the use of Java.
- I should use OpenJDK 12 to develop Java games, since the JDK is GNU/GPLv2 licensed.
So, there you have. I now have OpenJDK installed (or rather, unzipped) on my hard drive. It’s a huge JDK, and that’s definitely not going into my SSD at any time soon.
With OpenJDK installed, and my Visual Studio Code ready to go, I looked around for some tutorials on setting up Java. The results were… in VAIN! I honestly could not tell you what or why there are no tutorials about this. It’s all Eclipse, JetBrains, IntelliJ, and Android Studio for setting up Java, but nothing related to Visual Studio Code. I really wondered why… when Visual Studio Code is one of the most popular text editing software for programming out there (since December 2018)…
And that’s when it dawned upon me…
Java 9 introduced a JDK Module System, which is a feature aimed at modularizing the JDK. This means that the JDK requires a Java Build Tool to assist the developers in creating the Java project. I could be wrong, but it seems you require Maven or Gradle nonetheless for new Java project setups. Personally for me, I went to with Gradle, because it has better integration for JUnit 4.12.
Also unfortunate for me, is that I currently don’t know how to set up Gradle to use JUnit 5. If anyone knows how, please email me with the subject, “JAVA GRADLE FIX”, in all uppercase.
I have decided to use Java Spring’s “Building Java Projects with Gradle” guide, available here. This guide is somewhat outdated, as it refers to the reader to obtain a JDK version 6 or later, but most of the information from that article is still relevant. I’m just going to make it up-to-date with OpenJDK 12.0.1 (with the Japanese Reiwa era changes included), the latest Gradle version, Gradle 5.4.1, and Visual Studio Code, build version 1.34.0.
Short TL;DR list, starting from a completely blank slate (Windows):
- Download OpenJDK 12.0.1 (or later).
- Download Gradle 5.4.1 (or later).
- Download Visual Studio Code installer from GitHub.
- Install Visual Studio Code first.
- Unzip OpenJDK 12.0.1 to somewhere on your system. (In my case, it's
D:\Java
.) - Unzip Gradle 5.4.1 to somewhere on your system. (In my case, it's
D:\Gradle
.) - Set the environment variable,
JAVA_HOME
, to bepath\to\Java
(not thepath\to\Java\bin
directory). - Set the environment variable,
GRADLE_HOME
, to bepath\to\Gradle
root directory. - Add
%GRADLE_HOME%\bin
to the environment variable,PATH
. - Make sure environment variables are "Applied" and "OKed".
- Open PowerShell terminal.
- Verify both Java and Gradle works in PowerShell. If not, redo the previous steps.
- Browse or navigate PowerShell terminal to your Java project's working directory. (In my case, it's
D:\Documents\JavaProjects\java_practice
.) - Type
gradle init
in PowerShell terminal. This will also set up JUnit 4.12 for Java unit testing. - Follow the instructions.
- Open Visual Studio Code. Close anything in Visual Studio Code, such as Current Unsaved Workspace and such.
- Install the Java Extension Pack, provided by Microsoft, for Visual Studio Code. (Here.)
- Open the folder of your Java project's root directory. (In my case, it's
D:\Documents\JavaProjects\java_practice
.) - Use Visual Studio Code to create a
.vscode
folder directory inside the root project directory, and save your Current Workspace inside.vscode
folder. - Open the Debug tab. (Or use the hotkey:
CTRL
+SHIFT
+D
). - Near the top, click on the gear to create a
launch.json
file. -
Edit the
launch.json
with the following, while remembering to rename the "name", "mainClass" and "projectName" each to something else:{ "configurations": [ { "type": "java", "name": "Launch My Java Project", "request": "launch", "mainClass": "my.package.names.App", "projectName": "java_practice", "console": "externalTerminal" } ] }
-
Create a simple Java "Hello World" application, while also renaming the class name to whatever you entered in
launch.json
:package my.package.names; public class App { public static void main(String[] args) { System.out.println("Hello world."); } }
- Make sure to fix to JUnit 4.12 test cases, and fix all remaining Java problems/warnings.
- Press
F5
key to launch the Java program from Visual Studio Code. You should now see a Java console application.
On Linux, you may prefer to use a package manager and work from there, or manually install the OpenJDK Linux packages and set up the environment variables in your user profile’s configuration file.
References:
-
Google vs. Oracle lawsuit (Latest Status)
https://www.reuters.com/article/us-usa-court-google-oracle/u-s-supreme-court-seeks-trump-administration-views-on-google-oracle-copyright-feud-idUSKCN1S51CQ -
Oracle Java SE Licensing
https://www.oracle.com/technetwork/java/javase/terms/license/javase-license.html -
Building Java Projects with Gradle
https://spring.io/guides/gs/gradle/