Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Native Image] unable to run the program packed by native-image #10121

Open
1 of 2 tasks
quantrpeter opened this issue Nov 20, 2024 · 3 comments
Open
1 of 2 tasks

[Native Image] unable to run the program packed by native-image #10121

quantrpeter opened this issue Nov 20, 2024 · 3 comments
Assignees

Comments

@quantrpeter
Copy link

quantrpeter commented Nov 20, 2024

Describe the Issue

I have packed my project into native image using maven, by the official tutorial. It success give me the executable, but when I run it. I got:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: awt | java.library.path = [.]
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.NativeLibraries.loadLibraryRelative(NativeLibraries.java:141)
	at [email protected]/java.lang.ClassLoader.loadLibrary(ClassLoader.java:108)
	at [email protected]/java.lang.Runtime.loadLibrary0(Runtime.java:916)
	at [email protected]/java.lang.System.loadLibrary(System.java:2066)
	at [email protected]/java.awt.Toolkit$2.run(Toolkit.java:1384)
	at [email protected]/java.awt.Toolkit$2.run(Toolkit.java:1382)
	at [email protected]/java.security.AccessController.executePrivileged(AccessController.java:132)
	at [email protected]/java.security.AccessController.doPrivileged(AccessController.java:319)
	at [email protected]/java.awt.Toolkit.loadLibraries(Toolkit.java:1381)
	at [email protected]/java.awt.Toolkit.initStatic(Toolkit.java:1419)
	at [email protected]/java.awt.Toolkit.<clinit>(Toolkit.java:1393)
	at [email protected]/java.awt.Component.<clinit>(Component.java:624)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.invoke.DirectMethodHandle.ensureInitialized(DirectMethodHandle.java:328)
	at [email protected]/java.lang.invoke.DirectMethodHandle.internalMemberNameEnsureInit(DirectMethodHandle.java:335)

Using the latest version of GraalVM can resolve many issues.

GraalVM Version

java version "23.0.1" 2024-10-15
Java(TM) SE Runtime Environment Oracle GraalVM 23.0.1+11.1 (build 23.0.1+11-jvmci-b01)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 23.0.1+11.1 (build 23.0.1+11-jvmci-b01, mixed mode, sharing)

Operating System and Version

Darwin quantr-iMac.local 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:54:10 PST 2023; root:xnu-10002.61.3~2/RELEASE_X86_64 x86_64

Troubleshooting Confirmation

Run Command

./qemu-log-panel

Expected Behavior

expected it works but not

Actual Behavior

exception

Steps to Reproduce

  1. ./qemu-log-panel

Additional Context

No response

Run-Time Log Output and Error Messages

No response

@oubidar-Abderrahim
Copy link
Member

Please make sure to:

  • Run the agent to generate Metadata
  • Check our troubleshooting guide.
  • add -Djava.awt.headless=false to your buildArgs

@oubidar-Abderrahim oubidar-Abderrahim self-assigned this Nov 21, 2024
@quantrpeter
Copy link
Author

Dear @oubidar-Abderrahim I have use headless:

/Library/Java/JavaVirtualMachines/graalvm-jdk-23.0.1+11.1/Contents/Home/bin/native-image -Djava.awt.headless=false -jar qemu-log-panel-1.0-jar-with-dependencies.jar
========================================================================================================================
GraalVM Native Image: Generating 'qemu-log-panel-1.0-jar-with-dependencies' (executable)...
========================================================================================================================
[1/8] Initializing...                                                                                    (8.8s @ 0.26GB)
 Java version: 23.0.1+11, vendor version: Oracle GraalVM 23.0.1+11.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred
 C compiler: cc (apple, x86_64, 15.0.0)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
 1 user-specific feature(s):
 - com.oracle.svm.thirdparty.gson.GsonFeature
------------------------------------------------------------------------------------------------------------------------
 2 experimental option(s) unlocked:
 - '-H:ResourceConfigurationResources' (origin(s): 'META-INF/native-image/org.jline/jline-terminal-jna/native-image.properties' in 'file:///Users/peter/workspace/qemu-log-panel/target/qemu-log-panel-1.0-jar-with-dependencies.jar', 'META-INF/native-image/org.jline/jline-terminal-jansi/native-image.properties' in 'file:///Users/peter/workspace/qemu-log-panel/target/qemu-log-panel-1.0-jar-with-dependencies.jar', 'META-INF/native-image/org.jline/jline-terminal/native-image.properties' in 'file:///Users/peter/workspace/qemu-log-panel/target/qemu-log-panel-1.0-jar-with-dependencies.jar')
 - '-H:ReflectionConfigurationResources' (origin(s): 'META-INF/native-image/org.jline/jline-terminal-jna/native-image.properties' in 'file:///Users/peter/workspace/qemu-log-panel/target/qemu-log-panel-1.0-jar-with-dependencies.jar', 'META-INF/native-image/org.jline/jline-terminal-jansi/native-image.properties' in 'file:///Users/peter/workspace/qemu-log-panel/target/qemu-log-panel-1.0-jar-with-dependencies.jar', 'META-INF/native-image/org.jline/jline-terminal/native-image.properties' in 'file:///Users/peter/workspace/qemu-log-panel/target/qemu-log-panel-1.0-jar-with-dependencies.jar')
------------------------------------------------------------------------------------------------------------------------
Build resources:
 - 26.49GB of memory (41.4% of 64.00GB system memory, determined at start)
 - 20 thread(s) (100.0% of 20 available processor(s), determined at start)
[2/8] Performing analysis...  [*****]                                                                   (15.9s @ 1.66GB)
   12,924 reachable types   (86.0% of   15,032 total)
   23,675 reachable fields  (55.8% of   42,460 total)
   76,865 reachable methods (62.9% of  122,260 total)
    3,982 types,    25 fields, and   807 methods registered for reflection
       65 types,    72 fields, and    55 methods registered for JNI access
        5 native libraries: -framework CoreServices, -framework Foundation, dl, pthread, z
[3/8] Building universe...                                                                               (3.1s @ 1.32GB)
[4/8] Parsing methods...      [***]                                                                      (6.6s @ 1.55GB)
[5/8] Inlining methods...     [****]                                                                     (1.0s @ 1.87GB)
[6/8] Compiling methods...    [*******]                                                                 (45.2s @ 2.66GB)
[7/8] Laying out methods...   [***]                                                                      (8.8s @ 3.22GB)
[8/8] Creating image...       [**]                                                                       (4.4s @ 3.51GB)
  46.05MB (61.01%) for code area:    45,378 compilation units
  28.88MB (38.25%) for image heap:  361,181 objects and 287 resources
 575.00kB ( 0.74%) for other data
  75.49MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
  14.87MB java.base                                           11.15MB byte[] for code metadata
   9.70MB qemu-log-panel-1.0-jar-with-dependencies.jar         4.42MB byte[] for java.lang.String
   8.96MB java.desktop                                         2.65MB java.lang.String
   4.98MB java.xml                                             2.57MB java.lang.Class
   4.46MB svm.jar (Native Image)                               1.19MB byte[] for embedded resources
 588.13kB java.security.jgss                                 856.38kB byte[] for general heap data
 347.74kB java.rmi                                           791.84kB byte[] for reflection metadata
 302.40kB java.naming                                        605.81kB com.oracle.svm.core.hub.DynamicHubCompanion
 250.87kB com.oracle.svm.svm_enterprise                      417.14kB heap alignment
 225.50kB jdk.zipfs                                          376.09kB c.o.svm.core.hub.DynamicHub$ReflectionMetadata
   1.09MB for 26 more packages                                 3.91MB for 2585 more object types
                            Use '--emit build-report' to create a report with more details.
------------------------------------------------------------------------------------------------------------------------
Security report:
 - Binary includes Java deserialization.
 - Use '--enable-sbom' to assemble a Software Bill of Materials (SBOM).
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 AWT:  Use the tracing agent to collect metadata for AWT.
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
 QBM:  Use the quick build mode ('-Ob') to speed up builds during development.
------------------------------------------------------------------------------------------------------------------------
                       6.9s (7.1% of total time) in 1112 GCs | Peak RSS: 4.56GB | CPU load: 12.69
------------------------------------------------------------------------------------------------------------------------
Build artifacts:
 /Users/peter/workspace/qemu-log-panel/target/qemu-log-panel-1.0-jar-with-dependencies (executable)
========================================================================================================================
Finished generating 'qemu-log-panel-1.0-jar-with-dependencies' in 1m 36s.

But still doesn't work, error:

/Users/peter/workspace/qemu-log-panel/target>./qemu-log-panel-1.0-jar-with-dependencies 
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: awt | java.library.path = [.]
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.NativeLibraries.loadLibraryRelative(NativeLibraries.java:141)
	at [email protected]/java.lang.ClassLoader.loadLibrary(ClassLoader.java:108)
	at [email protected]/java.lang.Runtime.loadLibrary0(Runtime.java:916)
	at [email protected]/java.lang.System.loadLibrary(System.java:2066)
	at [email protected]/java.awt.Toolkit$2.run(Toolkit.java:1384)
	at [email protected]/java.awt.Toolkit$2.run(Toolkit.java:1382)
	at [email protected]/java.security.AccessController.executePrivileged(AccessController.java:132)
	at [email protected]/java.security.AccessController.doPrivileged(AccessController.java:319)
	at [email protected]/java.awt.Toolkit.loadLibraries(Toolkit.java:1381)
	at [email protected]/java.awt.Toolkit.initStatic(Toolkit.java:1419)
	at [email protected]/java.awt.Toolkit.<clinit>(Toolkit.java:1393)
	at [email protected]/java.awt.Component.<clinit>(Component.java:624)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.Class.ensureInitialized(DynamicHub.java:650)
	at [email protected]/java.lang.invoke.DirectMethodHandle.ensureInitialized(DirectMethodHandle.java:328)
	at [email protected]/java.lang.invoke.DirectMethodHandle.internalMemberNameEnsureInit(DirectMethodHandle.java:335)

@Karm
Copy link
Contributor

Karm commented Dec 16, 2024

Hello, @quantrpeter, you most certainly need the tracing agent to collect reflection and JNI info. e.g.

java -agentlib:native-image-agent=config-output-dir=AGENT -jar target/Apka.jar
native-image -H:ConfigurationFileDirectories=./AGENT -jar target/Apka.jar

...and it could very likely require manual attention too, depending on what exactly your application does with the Gui.
Last but not least, there will be additional libraries copied alongside your executable, these are needed.

My experience is Linux, not Mac, but there also could be some external dependency on e.g. fonts packages being available.
Oh, and you might need to set a JAVA_HOME or substitute e.g. the Fonts.java code that is attempting to look for fonts in Java home.

Providing a small reproducer repo would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants