Java with Visual Studio code (Vscode) Part 1
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
- 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”
In Figure 1.1
- Click on setting button, you will find that there is a menu,
- from menu, you just click on “settings”, Visual Studio Code settings page will appear.
In Figure 1.2
- In “1” After “User Settings” JSON file open, Then You will search for “Java.home” in search option.
- In “2” Click on “Edit in Setting.json” link
In Figure 1.3,
- 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.
- Click on “Edit Pencil Icon”
- “Java.home” will shows at “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
In Figure 1.4,
- Run the Command
/usr/libexec/java_home -v
it will gives you the home directory where the JDK is installed. - This is “JDK HOME” directory in my Mac OS X.
In Figure 1.5,
- Open “User Setting” from clicking of “Setting icon” from left bottom like windows visual studio code.
- click on edit Icon, “java.home” will added into “3” user custom setting option.
- Add the “JDK HOME” directory to “java.home” properties. Save the user Settings.
In Figure 1.6,
- Click on “Extensions” icon in visual studio code
- Search by “java” keyword
- Then Install the “Java Extensions Pack” which is made by “Microsoft” for visual studio.
- 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
- For windows press
ctrl + shift + p
and for Mac OS X presscommand +shift + p
for open command option in visual studio code.
In Figure 1.7
- In “1”, write “reload window” in command option
- 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
- Press
command + shift + p
from keyboard below screen will appear
In Figure 2.1,
- In “1” Search the “shell command” enable for Visual Studio Code
- Then Press On “2” to Enable the shell command for
code
in path - 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
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.
.vscode
directory with.launch.json
fileRun
andDebug
option added automatically tomain
method of java class. You can run or debug java code by clicking on this icons
In Figure 2.3,
- In “1”, this is a “launch.json” file
- 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,
- Click on Debug Section of Visual Studio Code
- Then Click On “Setting drop Down”, Then You can find “Add Configuration” option
In Figure 2.5
After Click on “Add Configuration”. There is option appear for selecting the debugger configuration for which language.
- Search “Java” from there.
- 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.
- You can also change the “name” of the Debug options.
- After Clicking on “Run” or “Debug” options. Application will provide output into “Debug Console”
- If the Java Code is compile correctly, it will provide output perfectly.