GraalJSEngineFactory could not be instantiated

2.4k views Asked by At

I am new to Graal. I am referring this document. I am using JDK 11.

Below is the code I am trying to run:

ScriptEngine graaljsEngine = new ScriptEngineManager().getEngineByName("graal.js");
if (graaljsEngine == null) 
{
    System.out.println("Graal.js not found");
    return null;
}
try 
{
    System.out.println(graaljsEngine.eval("IncomingMessageString"));
} 
catch (ScriptException e) 
{
    e.printStackTrace();
}

jars being used:

1: truffle-api-20.2.0.jar    
2: js-scriptengine-20.2.0.jar    
3: js-20.2.0.jar    
4: graal-sdk-20.2.0.jar    
5: compiler-20.2.0.jar

When trying to execute the code above, I am running into the follow error:

ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider com.oracle.truffle.js.scriptengine.GraalJSEngineFactory could not be instantiated

I have tried running the code with the following VM options:

-Dpolyglot.inspect.Secure=false

it still doesn't resolve the issue.

2

There are 2 answers

0
Maazen On

Finally had to use build automation and that got it working , which is strange as I am not mentioning any new jars except for the ones I had already manually added .

If using gradle put this in your build.gradle and you are good:

dependencies {
    compile group: 'org.graalvm.js', name: 'js', version: '20.2.0'
    compile group: 'org.graalvm.js', name: 'js-scriptengine', version: '20.2.0'
    compile group: 'org.graalvm.sdk', name: 'graal-sdk', version: '20.2.0'
    compile group: 'org.graalvm.truffle', name: 'truffle-api', version: '20.2.0'
}

If using maven put this in your build.gradle and your are good:

    <properties>
        <graalvm.version>20.2.0</graalvm.version>
        <compiler.dir>${project.build.directory}/compiler</compiler.dir>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.graalvm.sdk</groupId>
            <artifactId>graal-sdk</artifactId>
            <version>${graalvm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>${graalvm.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
            <version>${graalvm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.tools</groupId>
            <artifactId>profiler</artifactId>
            <version>${graalvm.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.graalvm.tools</groupId>
            <artifactId>chromeinspector</artifactId>
            <version>${graalvm.version}</version>
            <scope>runtime</scope>
        </dependency>

Under the hood I am sure these build automation tools are resolving the issue by getting other missing jars. I found this link useful : http://www.mosgrom.net/stuff/graalvm.html but as I said just manually adding the requisite jars by unzipping graalvm community binary or from maven repo to my java project didn't resolve the issue unless I used maven/gradle for dependency management . Hope that helps anyone else with same issue :) .

0
Steves On

The simplest and most efficient solution is to run your code on GraalVM distribution: just change your JAVA_HOME to the path to GraalVM or change the Java home setting in your IDE.

If you insist on using another JDK distribution, then take a look at https://www.graalvm.org/reference-manual/js/RunOnJDK/