You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 27 Next »

VSNetBeans, the Apache NetBeans Extension for Visual Studio Code, is created as a promotional tool by the Apache NetBeans community for VS Code users.

Install VSNetBeans into VS Code, use it, and maybe you'll want to try the complete Apache NetBeans experience, which is Apache NetBeans. And, if not, that's also fine. 

Full Java editing and debugging support is provided by VSNetBeans directly in your VSCode environment. By using VSNetBeans, you're using the same Java tools that are part of Apache NetBeans, so that we hope that VSNetBeans will provide a new avenue for feedback to the Apache NetBeans project.

VS Code already has support for Java, provided by an extension created by Red Hat, though what that extension misses are the following features that VSNetBeans provides out of the box:

  1. Support for JDK 8. (The Red Hat extension requires JDK 11 or above.)
  2. Polyglot debugging.
  3. Support for OpenJDK projects.

Getting Started

Get VSApacheNetBeans from the VS Code market place, once it is available there.

Until then, download the Apache NetBeans Language Server extension and install it into VSCode via Install from VSIX...

Scenarios

Scenario 1: Hello World

1. Get an existing Java project, for example, Micronaut: https://micronaut.io/launch/

Don't forget to select Maven and Java. Generate the project, download it, extract it into local disk.

$ mkdir microdemo
$ cd microdemo/
microdemo$ unzip ~/Downloads/microdemo.zip 
microdemo$ JAVA_HOME=/jdk-14 mvn install

2. Open the microdemo  folder in VSCode

3. Wait for message "Indexing completed" and then you can enjoy editing, code completion and other NetBeans goodies

Plus your VSCode now supports all the latest and greatest JDK14 coding structure:

var running = application.isRunning();
Assertions.assertTrue(running);

Can your VSCode do that with the RedHat extension on JDK 8? If not, install the Apache NetBeans VSCode extension!

Scenario 2: Polyglot Debugging

Please install Apache NetBeans Language Server extension (VSNetBeans).  Use Install from VSIX command. Once released it will be available from VSCode Marketplace for direct installation

In this sample we will go over how to create and debug Java <> JavaScript polyglot project using JDK 11, Graal.JS and ready to go graal-js-jdk11-maven-demo Maven Archetype. Sample is simple Java application invoking in a loop fibonaci calculation for incremented number written in JavaScript. Complete sample is attached for your convenience as well.

Steps

  1. Clone sample repository and move to the newly cloned directory
  1. Make sure that JAVA_HOME points at a JDK11 export JAVA_HOME=/path/to/jdk11
  2. Open VSCode with VSNetBeans installed.
    1. Go to File | Preferences and search for netbeans.jdkhome and set it to /path/to/jdk11
  3. Open Folder with graal-js-jdk11-maven-demo  in VSCode
  4. Remove App.java and AppTest.java originally coming with the sample Maven project
  5. Into main/java/com/mycompany/app add new File named PolyglotSample.java with following code:


    PolyglotSample.java
    package  com.mycompany.app;
    import org.graalvm.polyglot.Context;
    import org.graalvm.polyglot.Source;
    import org.graalvm.polyglot.Value;
    import java.net.URL;
    
    public class PolyglotSample {
        public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {
            Context ctx = Context.create();
            URL fibUrl = PolyglotSample.class.getResource("fib.js");
            Source fibSrc = Source.newBuilder("js", fibUrl).build();
            Value fib = ctx.eval(fibSrc);
            for (int i = 0; i < 10; i++) {
                Value res = fib.execute(i);
                System.out.println("Polyglot with Graal.JS - fib(" + i + ") = " + res.asInt());
                Thread.sleep(500);
            }
        }
    }  
  6. Then in terminal or file explorer create following structure where to store the JS source file so Java can find it, it has to be under main folder: resources/com/mycompany/app
  7. Inside this  resources/com/mycompany/app create file named fib.js and paste in following JS code:


    fib.js
    (function(n) {
        function fib(x) {
            if (x <= 2) {
                return 1;
            }
            let fibX1 = fib(x - 1);
            let fibX2 = fib(x - 2);
            return fibX1 + fibX2;
        }
     
        let fibN = fib(n);
        print(`JavaScript computed that fib(${n}) is ${fibN}`);
     
        return fibN;
    })
  8. Save all files and invoke action from Command Palette: Java: Compile Workspace
    • this is the action provided by VSNetBeans which builds the project.
    • Project is normally built also when debugger start but it is worth for the 1st time see if maven build passes.
  9. Once the build succeeds lets test the project.
  10. Set breakpoint to line 11 in fib.js and
  11. Select F5 to launch debugger and select type Java 8+ which is VSNetBeans provided debugger for JDK 8 and Polyglot capable of debugging Java and scripting languages like Graal.JS impl. of JS.
  12. When started you should see something like following screenshot with Call Stack from JS to Java and variables from JS and Java depending on what context you have  currently stepped into.

The modified graal-js-jdk11-maven-demo is available for your convenience as:

$ git clone https://github.com/JaroslavTulach/graal-js-jdk11-maven-demo
$ cd graal-js-jdk11-maven-demo
$ git checkout Fibonacci

Happy Polyglot development and debugging with VSNetBeans. 

Scenario 3: OpenJDK

to be done...

$ git clone https://github.com/graalvm/graal-js-jdk11-maven-demo
$ cd graal-js-jdk11-maven-demo
  • No labels