Java with Visual Studio code (Vscode) Part 1

Tariqul Islam
6 min readNov 17, 2018

Java is the most popular programming language in present world. Because of Cross platform and huge library for developer. Java Development kit(JDK) provide development environment for developing Java Application.

There are lots of IDE available for developing the Java Application. Today, I am using text editor to Learn java. Text Editor is best way to know java language structures and syntax in dept . Because IDE like Intelij Idea or Eclipse generating all the basic structure of java application, so it can not be helpful for beginners to learn Java application with better understanding. Other advantage of using text editor is, it takes less memory than IDE. So Developer who has low configuration laptop or computer, they can use Text editor as “Java Development” . “Visual Studio Code” is the best option for using as code editor.

Configure the Open JDK with Visual Studio Code

For Windows

  1. First we have to download Open JDK 8 from RedHat site

For Mac os x

You can follow the link for installation JDK for Mac OS X.

Configure the Java with Visual Studio code

For Windows

Extract the OpenJDK files using winrar or other achieve software to any directory of you computer, such as go to “D:” directory, extract the Open JDK, rename it as “openjdk”.

D:\openjdk

Then Open Visual Studio Code and In left bottom corner you can find the “setting icon”

Figure 1.1 Setting Option for Visual Studio Code

In Figure 1.1

  1. Click on setting button, you will find that there is a menu,
  2. from menu, you just click on “settings”, Visual Studio Code settings page will appear.
Figure 1.2 Search For Java Home Configuration of Visual Studio Code

In Figure 1.2

  1. In “1” After “User Settings” JSON file open, Then You will search for “Java.home” in search option.
  2. In “2” Click on “Edit in Setting.json” link
Figure 1.3 Modified the Java Home Directory for Visual Studio Code

In Figure 1.3,

  1. In “1” , Click on User Setting page for edit the user settings. “User Settings” will be the setting for user who using this operating system currently.
  2. Click on “Edit Pencil Icon”
  3. “Java.home” will shows at “4”.
  4. Then Add the “Open JDK” path for where you extract the open JDK. Save the “User Settings”

For Mac os X

For Mac osx, if java 8 already installed in your pc. just open terminal and write command to search where is JDK folder, by using `/usr/libexec/java_home -v` command, then change “java.home” properties of settings

Figure 1.4 Finding the Java Home in Mac OS X

In Figure 1.4,

  1. Run the Command /usr/libexec/java_home -v it will gives you the home directory where the JDK is installed.
  2. This is “JDK HOME” directory in my Mac OS X.
Figure 1.5 Configure the Java Home setting at Mac OS X Visual Studio Code

In Figure 1.5,

  1. Open “User Setting” from clicking of “Setting icon” from left bottom like windows visual studio code.
  2. click on edit Icon, “java.home” will added into “3” user custom setting option.
  3. Add the “JDK HOME” directory to “java.home” properties. Save the user Settings.
Figure 1.6 Install the Java Extension pack for Java Support for Visual Studio Code

In Figure 1.6,

  1. Click on “Extensions” icon in visual studio code
  2. Search by “java” keyword
  3. Then Install the “Java Extensions Pack” which is made by “Microsoft” for visual studio.
  4. Then Click “Install”. After Installation “Reload To Activate” button Appear at “4” at place of “Install” button. Reload the Visual Studio Code for configuring the “Java Development Server” with JDK. Those Steps are both applicable for “Windows and Mac OS X”

Manually Reload the Visual Studio Code

  1. For windows press ctrl + shift + p and for Mac OS X press command +shift + p for open command option in visual studio code.
Figure 1.7 Reload the Visual Studio Code if There is any inconsistent happen

In Figure 1.7

  1. In “1”, write “reload window” in command option
  2. Click on “Reload Window”, Visual Studio will automatically reload the windows for you.

Sample Code for Java application

Create Directory or folder from terminal, those command are compatible with both “Mac OS” and “Windows”.

> mkdir java-test
> cd java-test

For Mac os x user

You can add the code command to open the Visual Studio Code from terminal

  1. Presscommand + shift + p from keyboard below screen will appear
Figure 2.1, Configure the shell command for terminal at Mac OS X

In Figure 2.1,

  1. In “1” Search the “shell command” enable for Visual Studio Code
  2. Then Press On “2” to Enable the shell command for code in path
  3. Then Close the Visual Studio Code. Go to terminal and write
> code .

4. It will open Visual Studio Code for you

Then Create java Class file in java-test folder. Class name is Application.java

Figure 2.2, Add the Application Class And How Visual Studio Code added the Compiler Configuration to Java Application

In Figure 2.2,

I have create sample class named Application.java and add main method or entry point of the java application. Java needs main or entry point method for debug or run code. After creating the class with main method, Visual Studio Code automatically generate the .vscode directory, which contains launch.json file, which contains java application’s run or compile configuration with json format. And Visual Studio Code automatically add the “Run” and “Debug” option with that java class at main method. No need to configure manually.

  1. .vscode directory with .launch.json file
  2. Run and Debug option added automatically to main method of java class. You can run or debug java code by clicking on this icons
Figure 2.3, How Visual Studio Configure Java Application

In Figure 2.3,

  1. In “1”, this is a “launch.json” file
  2. We can see that, In configuration “launch.json” file Visual Studio Code already added the “mainClass” as “Application”. So when you “run” or “debug” by Visual Studio Code, It will find “Application.java” class as main Class.

If the Visual Studio Code do not added any “launch.json” configuration file. We can add it manually by Clicking on Debug Section of Visual Studio Code.

Figure 2.4, Extra or Manually configured Java Run or Debug Option

Figure 2.4,

  1. Click on Debug Section of Visual Studio Code
  2. Then Click On “Setting drop Down”, Then You can find “Add Configuration” option
Figure 2.5, Extra or manual configuration of Java debugger for code

In Figure 2.5

After Click on “Add Configuration”. There is option appear for selecting the debugger configuration for which language.

  1. Search “Java” from there.
  2. Then select “Java: Launch Program”. It will create “JSON” Configuration object. Just add or change “mainClass” name, which file you want to use for “Entry” or “main” class for Java Application.
  3. You can also change the “name” of the Debug options.
Figure 2.6, Run or Debug the Java application
  1. After Clicking on “Run” or “Debug” options. Application will provide output into “Debug Console”
  2. If the Java Code is compile correctly, it will provide output perfectly.

--

--

Tariqul Islam

Tariqul Islam have 9+ years of software development experience. He knows Python, Node Js and Java, C# , PHP.