This is a quick tutorial on how to get started with Java development on Windows using Visual Studio Code. Here we'll go through all the installation and setup steps to get you ready to work on your Java projects.
Official VS Code tutorial: https://code.visualstudio.com/docs/java/java-tutorial
If you don't already have VSCode installed, you can save yourself a lot of time by downloading the Coding Pack for Java, which contains Visual Studio Code, the Java JDK, and all the essential Java extensions for VSCode.
If you do already have VSCode installed, that's not a problem either. The first step is to go to your extensions and find the "Java Extension Pack", and install that. Next you need to install the "Java Development Kit" if you don't already have it, which you probably don't if you haven't installed Java before.
There are two broad types of Java installation you'll hear about. There's the JRE, or "Java Runtime Environment", which allows you to run Java applications on your computer or device. And then there's the JDK, which stands for "Java Development Kit", which is what you need if you want to develop Java applications. If you install the JDK, that will include the "Java Runtime Environment", so you don't need to install both.
So we can install the JDK from VSCode now that we have the Java extensions. Open the Command Palette (either View > Command Palette, or Ctrl+Shift+P) and search for "Java: Configure Java Runtime". Now there are also different JDK implementations to choose from. I recommend you install the "AdoptOpenJDK" because this option is free and open source. If you use the Oracle JDK, you'll need to purchase a commercial license if you ever end up developing business software with it. So for most of you, just go ahead and install the latest stable OpenJDK version. If you need a specific JDK, you can of course download that JDK from its website and install it instead.
Alright, now we've got everything installed, and we just need to let VSCode know where the JDK is located. The path to it. There are two ways to do this. I recommend you set the
JAVA_HOME system environment variable. This way anything you install on your computer, that wants to know where the JDK is, can get it from this value. So let me show you how to do that.
Open "File Explorer" and right-click on "This PC", or it might be "My Computer" on older versions of Windows, and select "Properties". Then in here click "Advanced system settings", and you should see a button that says "Environment Variables...". Click that, and create a "New..." system variable, either for yourself or for everyone on your computer. We're going to set the variable name to
JAVA_HOME, and the value to the folder where the JDK is installed. In my case that's
Now if you don't want to set an environment variable, you can instead let VSCode know directly where the JDK is installed. To do that, in VSCode you'll open the settings. And I like to switch it over to the JSON view. And you'll add a setting for "java.home", and you'll set that value to your JDK path, like
C:\Program Files\Java\jdk1.6.0_02. Alternatively, you can run "Java: Configure Java Runtime" again from your Command Palette, and that will show you your JDK paths and there's a link to edit your settings from there.
Alright, let's run our "Hello World" program to ensure everything works. I'll open a new folder to hold this project. And create a new file called "QuickStart.java". Let's put a main method in this class, and we'll print out to the console the name of this website, "Learn Code by Gaming". And then we'll run it (F5). And in the console you should see the output.
In case I've lost you, you can see these same steps from "Java: Getting Started" in the Command Palette. You can also create a new project, instead of opening a new folder manually, by running "Java: Create Java Project", again in the Command Palette. And this will create a little more structure for you, in your project.