Home > front end >  Graalvm not found within Java application
Graalvm not found within Java application

Time:05-14

I have an Apache Tomcat servlet in which I would like to run some other language - for this purpose, I would like to try to get Graalvm functionality working within OpenJDK 17.

I added the following dependency to my build.gradle file:

// https://mvnrepository.com/artifact/org.graalvm.sdk/graal-sdk
implementation group: 'org.graalvm.sdk', name: 'graal-sdk', version: '22.1.0'

The Gradle file parses and runs without issue, however when I try to access the Graalvm classes in my application (servlet), nothing is found!?

import org.graalvm; // is not found

For a first test, I'd like to get something simple like the following to work:

import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;

...

try (Context context = Context.create()) {
    context.eval("js", "print('Hello JavaScript!');");
}

But this fails, as the required class (Context) is not found. I'm sure I'm missing something simple, but what?

Update

Thanks to @Mohammed Fataka suggestion, the Gradle build now runs successfully. My IDE (eclipse) was still showing compile errors, however a "Refresh Gradle Project" resolved that issue. (Right-click on the project within the Package Explorer, then Gradle -> Refresh Gradle Project).

I now have a new problem in that the resultant servlet crashes with the following error message: IllegalStateException: No language and polyglot implementation was found on the classpath. Make sure the truffle-api.jar is on the classpath.

So I added the following dependency:

// https://mavenlibs.com/maven/dependency/com.oracle/truffle-api
implementation group: 'com.oracle', name: 'truffle-api', version: '0.8'  

But this hasn't helped, the same error still occurs.

Update 2

I finally have it working, I needed to use a different dependency:

// https://mvnrepository.com/artifact/org.graalvm.truffle/truffle-api
implementation group: 'org.graalvm.truffle', name: 'truffle-api', version: '22.1.0'

// https://mvnrepository.com/artifact/org.graalvm.js/js
implementation group: 'org.graalvm.js', name: 'js', version: '22.1.0'

I hope this helps others...

CodePudding user response:

hey there please try to see if you are required to add compiler for that, if yes then add to your plugins section in build.gradle

    id "org.graalvm.plugin.compiler" version "0.1.0-alpha2"

btw,i run the code you got i have no issue and i can access to them, try to see your repositories as well. docs are here

  • Related