Giter Club home page Giter Club logo

hands-on-graalvm's Introduction

Hands-on GraalVM

This is my Hands-on on GraalVM demo repository. The README contains the basic instructions and commands that I used to create the project.

GraalVM installation

I used Jabba as a JVM manager to install GraalVM on my machine. Once this is done, make sure to install R, Python and Ruby using the Graal updater.

# use Jabba to install GraalVM
jabba remote-ls
jabba install [email protected]
jabba use [email protected]

export GRAALVM_HOME=$JAVA_HOME

# Use the GraalVM updater to install additional languages
gu available
gu install R
gu install python
gu install ruby

Polyglot Mayhem

Another power of GraalVM is that you can run multiple languages in the same VM at the same time and you can also access each others type system. Polyglot mayhem!

This is not the same as provided by the Java Scripting API (defined by JSR 223)!

To run this demo simply perform ./gradlew polyglotMayhem. This will use and call Java, JavaScript, R, Ruby and Python all in the same JVM and also pass and modify a single share object. The output of this tasks should be something like

> Task :polyglotMayhem
Hello from Java!
Hello from JavaScript!
[1] "Hello from R!"
Hello from Ruby!
Hello from Python!

Called 5 languages in the same JVM. Polyglot Mayhem!

Have a look at the class hands.on.graalvm.PolyglotMayhem and also the official Graal documentation on how to embed and use other languages via the Graal Polyglot API.

On the command line you can also run Ruby or Python scripts using the GraalVM. Make sure to install the binaries using the gu command.

$ ruby src/test/polyglot/hello-ruby.rb
$ time ruby src/test/polyglot/hello-ruby.rb

# with GraalVM Ruby
$ gu install ruby
$ time $JAVA_HOME/bin/ruby src/test/polyglot/hello-ruby.rb

$ python src/test/polyglot/hello-python.py
$ time python src/test/polyglot/hello-python.py

$ python3 src/test/polyglot/hello-python.py
$ time python3 src/test/polyglot/hello-python.py

# with GraalVM Python
$ gu install python
$ time $JAVA_HOME/bin/graalpython src/test/polyglot/hello-python.py

Native CLIs with Picocli and GraalVM

The combination of Picocli and GraalVM makes it possible to build cool and slim native CLI applications in Java. Have a look at the hands.on.graalvm.HandsOnGraal implementation. In the Gradle build file, the following dependencies are required.

dependencies {
    implementation 'info.picocli:picocli:4.6.1'
    implementation 'info.picocli:picocli-jansi-graalvm:1.2.0'

    annotationProcessor 'info.picocli:picocli-codegen:4.6.1'

    runtimeOnly 'info.picocli:picocli-codegen:4.6.1'
    runtimeOnly 'info.picocli:picocli-shell-jline3:4.6.1'
}

Once this is done we can use the GraalVM native-image command to compile the native executable from the bytecode of our Java CLI application.

task copyRuntimeLibs(type: Copy) {
    into "$buildDir/libs"
    from sourceSets.main.runtimeClasspath
    include "*.jar"
}

assemble.finalizedBy copyRuntimeLibs

String getClasspath() {
    'libs/' + file("$buildDir/libs").list()?.join(':libs/')
}

task graalNativeImage(description: 'Generate native image with GraalVM', dependsOn: 'build', type: Exec) {
    workingDir "$buildDir"
    commandLine = [
            'native-image',
            '-cp', getClasspath(),
            '-H:+ReportUnsupportedElementsAtRuntime',
            '-H:+ReportExceptionStackTraces',
            '-H:+AddAllCharsets',
            '--initialize-at-run-time=org.fusesource.jansi.WindowsAnsiOutputStream',
            '--no-server',
            'hands.on.graalvm.HandsOnGraal',
            'hands-on-graal'
    ]
}

More demos

Make sure to checkout the official GraalVM demo repository with its many examples.

git clone https://github.com/graalvm/graalvm-demos
cd graalvm-demos/polyglot-javascript-java-r
./build.sh
$GRAALVM_HOME/bin/node --polyglot --inspect --jvm server.js
open http://localhost:3000

References

Maintainer

M.-Leander Reimer (@lreimer), [email protected]

License

This software is provided under the MIT open source license, read the LICENSE file for details.

hands-on-graalvm's People

Contributors

lreimer avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

qaware

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.