Dec 1, 2016

A "Hello World" example with Visual Studio Code and Java

Yes, I just got my new 13" Macbook Pro (Oct 18, with Fn keys, of course). It has been more than 10 years since I replaced my iBook G4 with a Dell :( --Don't make me wrong, I love windows:)

It is a great time nowadays with cross-platform programming become easier and easier. I don't use VS code, or Visual Studio Code very often. I love its simplicity. For this reason, I install it on the Mac and begin to play with it. This time, in Java. Why Java?Well, it just happens like that.

So let's begin with a simple "Hello World" example.

Outline:
- Download VS Code;
- Install JDK (Java Development Kit);
- Test the example.
- Install a Java extension (optional);

(1) Download VS Code from here or google it. 

(2) Install JDK (Java Development Kit) from here(Oracle site).

(3)Test our example.

3-1 From the menu, select "Open..", select a folder where you want to put your file in;

3-2 "New file" to add a file, for example, "example.java" and copy the following code in it:

class hello{
    public static void main( String args[] ) {
    System.out.println("Hello World");
    }
}

 3-3 compile and debug with it.

-From the menu select "View->Integrated Terminal"
-compile the source file, here is "example.java", using "javac"
javac example.java
-run the built class file, here is "hello", using "java"
java  hello
-check the output

3-4 (optional) install a Java extension and how to find java-home path

I am not going to introduce the extension here. But if you need further functionality like auto-completion, you will need such extensions, just search for it. Just in case, you don't know how to set the settings for java configurations, especially for "java-home".

Select the "Code->Preference->User Settings", VS code will show a window with default settings, and new window for your to overwrite the default settings.
add a line call "java.home" (as you are typing, this should appear for you to choose), after that, input the path for your JDK, by default for my mac, it is:

    "java.home": "/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home"

In fact, the problem is, how to find this path for your system on a Mac? Here is a good reference on Stackoverflow. In short, in the Terminal, use 

ls -l 'which java'
and copy the one similar to the above.

Userful links:

VS Code with Java (Japanese): blogs.msdn.microsoft.com/ayatokura/2016/07/05/vscode_java/
Path for java-home: stackoverflow.com/questions/18144660/what-is-path-of-jdk-on-mac