Description
Environment:
- GraalVM version or commit id if built from source: 20.0.0.2
- CE or EE: CE
- JDK version: 17
- OS and OS Version: Linux: Ubuntu 20.04
- Architecture: amd64
- The output of
java -Xinternalversion
: OpenJDK 64-Bit Server VM (17.0.2+8-jvmci-22.0-b05) for linux-amd64 JRE (17.0.2+8-jvmci-22.0-b05), built on Jan 20 2022 22:54:40 by "buildslave" with gcc 10.3.0
The error:
Exception in thread "main" org.graalvm.polyglot.PolyglotException: java.lang.NullPointerException: espressoHome not defined
at java.util.Objects.requireNonNull(Objects.java:233)
at com.oracle.truffle.espresso.runtime.EspressoProperties$Builder$1.<init>(EspressoProperties.java:183)
at com.oracle.truffle.espresso.runtime.EspressoProperties$Builder.build(EspressoProperties.java:175)
at com.oracle.truffle.espresso.runtime.EspressoContext.initVmProperties(EspressoContext.java:662)
at com.oracle.truffle.espresso.runtime.EspressoContext.spawnVM(EspressoContext.java:484)
at com.oracle.truffle.espresso.runtime.EspressoContext.initializeContext(EspressoContext.java:448)
at com.oracle.truffle.espresso.EspressoLanguage.initializeContext(EspressoLanguage.java:143)
at com.oracle.truffle.espresso.EspressoLanguage.initializeContext(EspressoLanguage.java:63)
at com.oracle.truffle.api.TruffleLanguage$Env.postInit(TruffleLanguage.java:3317)
at com.oracle.truffle.api.LanguageAccessor$LanguageImpl.postInitEnv(LanguageAccessor.java:289)
at com.oracle.truffle.polyglot.PolyglotLanguageContext.ensureInitialized(PolyglotLanguageContext.java:689)
at com.oracle.truffle.polyglot.PolyglotContextImpl.getBindings(PolyglotContextImpl.java:1043)
at com.oracle.truffle.polyglot.PolyglotContextDispatch.getBindings(PolyglotContextDispatch.java:98)
at org.graalvm.polyglot.Context.getBindings(Context.java:560)
at com.r3.conclave.enclave.espressotests.Main.main(Main.java:42)
Caused by: Attached Guest Language Frames (0)
Internal GraalVM error, please report at https://github.com/oracle/graal/issues/.
Have I verified this issue still happens when using the latest snapshot?
I have not.
Code
The code consists of two parts, the host and the client.
The "host" code uses espresso and the polyglot API to load and execute a method from a jar file:
package espressohost;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
public class Main {
static final String usage = "Usage: program_name <jar_file> <main_class>";
static Context espressoExecutionContext(String classpath) {
String javaHome = System.getProperty("java.home");
if (javaHome == null) {
javaHome = System.getenv("JAVA_HOME");
}
if (javaHome == null) {
System.out.println("Failed to determine java home!");
System.exit(1);
}
return Context.newBuilder("java")
.option("java.Classpath", classpath)
.option("java.JavaHome", javaHome)
.allowAllAccess(true)
.build();
}
static public void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println(usage);
System.exit(1);
}
final String jarFilePath = args[0];
final String mainClassName = args[1];
System.out.println("Jar file to load: " + jarFilePath);
System.out.println("Main class name: " + mainClassName);
Context polyglot = espressoExecutionContext(jarFilePath);
Value mainClass = polyglot.getBindings("java").getMember(mainClassName);
if (mainClass == null) {
System.out.println("Failed to load the main class!");
System.exit(1);
}
mainClass.invokeMember("main", (Object) new String[0]);
}
}
The "client" code (for the purposes of reproducing this error) is just a trivial hello world program:
package hello;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
When the host code is built as a native image and executed, the aforementioned error occurs at line 42 (Value mainClass = polyglot.getBindings("java").getMember(mainClassName);
) when attempting to get the main class.
Steps to reproduce the issue
- Set JAVA_HOME & GRAALVM_HOME so that they point to a graalvm installation with truffle/espresso and native image support.
- Create a runnable "client" jar containing the above trivial hello world program (let's call it "hello.jar").
- Create a runnable "host" jar containing the code above (let's call this one "espressoHost.jar").
- Run the host jar normally:
LD_DEBUG=unused $GRAALVM_HOME/bin/java -jar espressoHost.jar hello.jar hello.Main
and observe that espresso successfully runs the hello world program. - Now create a native image of the host jar:
native-image -jar espressoHost.jar --no-fallback --language:java
, and execute it with the same arguments. this should result in the aforementioned error.
Expected behaviour
Expected the program to execute as it does before compilation as a native image.
Additional context
As part of a project I'm working on, I'm attempting to create a native image which can dynamically load and execute methods from an arbitrary jar file, specified at runtime. I'm unsure as to whether or not the error I'm getting is because of an internal graalvm bug, or a direct result of something I've done improperly.
I haven't been able to find anything regarding "espressoHome" on the graalvm docsite or in the graalvm demo code (Most of my experimentation with espresso has so far been accomplished by reverse engineering the espresso jshell demo). I'm currently reading the espresso source to try and better understand where this error comes from.