RFR: 8339480: Build static-jdk image with a statically linked launcher [v8]

Jiangli Zhou jiangli at openjdk.org
Sat Nov 2 01:08:40 UTC 2024


On Fri, 1 Nov 2024 22:24:28 GMT, Jiangli Zhou <jiangli at openjdk.org> wrote:

> > I finally noticed that you are testing a precompiled HelloWorld class, and I have been running with a source file argument to have java compile it on the fly.
> > When I try using a pre-compiled HelloWorld, the linux port works for me too.
> > @jianglizhou Can you please verify if you can run this with a .java file directly? To be clear: this works fine on my mac with this PR, and (as I said), I'm pretty certain it used to work at least at some point during development on this PR.
> > ```
> > ihse at sthihse:/localhome/git/jdk-ALT$ cat > HelloWorld.java
> > public class HelloWorld {
> >   public static void main(String[] args) {
> >     System.out.println("Hello, world!");
> >   }
> > }
> > ihse at sthihse:/localhome/git/jdk-ALT$ javac HelloWorld.java 
> > ihse at sthihse:/localhome/git/jdk-ALT$ ./build/linux-x64/images/static-jdk/bin/java HelloWorld 
> > Hello, world!
> > ihse at sthihse:/localhome/git/jdk-ALT$ ./build/linux-x64/images/static-jdk/bin/java HelloWorld.java
> > Error: Unable to load main class com.sun.tools.javac.launcher.SourceLauncher in module jdk.compiler
> > Caused by: java.lang.UnsatisfiedLinkError: no jimage in system library path: /localhome/git/jdk-ALT/build/linux-x64/images/static-jdk/bin
> > Runtime.exit(1) logging failed: Could not initialize class jdk.internal.module.SystemModuleFinders$SystemImage
> > ihse at sthihse:/localhome/git/jdk-ALT$ 
> > ```
> 
> I can reproduce the issue with running a `.java` directly using static-jdk/bin/java built from your branch. The issue does not exist with the `bin/javastatic` built from https://github.com/openjdk/leyden/tree/hermetic-java-runtime (I just tested). I have a hunch of where is problem might be. I'll do some debugging.
> 
> ```
> $ bin/java /usr/local/google/home/jianglizhou/tests/HelloWorld.java
> Error: Unable to load main class com.sun.tools.javac.launcher.SourceLauncher in module jdk.compiler
> Caused by: java.lang.UnsatisfiedLinkError: no jimage in system library path: /usr/local/google/home/jianglizhou/github/jdk_pull_20837/build/linux-x86_64-server-slowdebug/images/static-jdk/bin
> Runtime.exit(1) logging failed: Could not initialize class jdk.internal.module.SystemModuleFinders$SystemImage
> ```

Ok, I've found the cause. It's because `<jdk_build_path>/static-jdk/bin` instead of `<jdk_build_path>/static-jdk/lib` is set as the `_sun_boot_library_path` by `Arguments::set_dll_dir()`, during `os::init_system_properties_values()`. The path is retrieved via `StaticProperty.sunBootLibraryPath()` and used by `ClassLoader.loadLibrary()` to locate `jimage`. Hence the failure.

Looking at why it only fails with running with `.java` but not with `.class`, a quick looks shows me that the following call path is only taken when running with `.java`:


	at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2410)
	at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:927)
	at java.base/java.lang.System.loadLibrary(System.java:2078)
	at java.base/jdk.internal.jimage.NativeImageBuffer$1.run(NativeImageBuffer.java:43)
	at java.base/jdk.internal.jimage.NativeImageBuffer$1.run(NativeImageBuffer.java:40)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
	at java.base/jdk.internal.jimage.NativeImageBuffer.<clinit>(NativeImageBuffer.java:39)
	at java.base/jdk.internal.jimage.BasicImageReader.<init>(BasicImageReader.java:97)
	at java.base/jdk.internal.jimage.ImageReader$SharedImageReader.<init>(ImageReader.java:229)
	at java.base/jdk.internal.jimage.ImageReader$SharedImageReader.open(ImageReader.java:243)
	at java.base/jdk.internal.jimage.ImageReader.open(ImageReader.java:67)
	at java.base/jdk.internal.jimage.ImageReader.open(ImageReader.java:71)
	at java.base/jdk.internal.jimage.ImageReaderFactory$1.apply(ImageReaderFactory.java:70)
	at java.base/jdk.internal.jimage.ImageReaderFactory$1.apply(ImageReaderFactory.java:67)
	at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1714)
	at java.base/jdk.internal.jimage.ImageReaderFactory.get(ImageReaderFactory.java:61)
	at java.base/jdk.internal.jimage.ImageReaderFactory.getImageReader(ImageReaderFactory.java:85)
	at java.base/jdk.internal.module.SystemModuleFinders$SystemImage.<clinit>(SystemModuleFinders.java:385)
	at java.base/jdk.internal.module.SystemModuleFinders$SystemModuleReader.findImageLocation(SystemModuleFinders.java:429)
	at java.base/jdk.internal.module.SystemModuleFinders$SystemModuleReader.read(SystemModuleFinders.java:483)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:809)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassInModuleOrNull(BuiltinClassLoader.java:741)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClass(BuiltinClassLoader.java:621)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:640)
	at java.base/java.lang.Class.forName(Class.java:673)
	at java.base/java.lang.Class.forName(Class.java:648)
	at java.base/sun.launcher.LauncherHelper.loadModuleMainClass(LauncherHelper.java:798)
	at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:733)


I'm going to look into a fix and update in this comment thread.

While investigating, I also just found there a new issue in https://github.com/openjdk/leyden/tree/hermetic-java-runtime. Let's focus on resolving the `.java` run issue with your PR to make progress first.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/20837#issuecomment-2452777288


More information about the core-libs-dev mailing list