You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference-manual/native-image/guides/build-static-and-mostly-static-executable.md
+47-41Lines changed: 47 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -16,44 +16,59 @@ However, you can create a statically linked or mostly-statically linked native e
16
16
A static native executable is easy to distribute and deploy on a slim or distroless container (a scratch container).
17
17
You can create a static native executable by statically linking it against [musl-libc](https://musl.libc.org/), a lightweight, fast and simple `libc` implementation.
18
18
19
-
**A mostly-static native executable** is a binary that links everything (`zlib`, JDKshared libraries) except the standard C library, `libc`. This is an alternative option to statically linking everything. Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
20
-
This approach is ideal for deployment on a distroless container image.
19
+
**A mostly-static native executable** is a binary that links everything (`zlib`, JDK-shared static libraries) except the standard C library, `libc`. This is an alternative option to statically linking everything. Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
20
+
This approach is useful for deployment on a distroless container image.
21
21
22
22
> Note: This currently only works when linked against `libc`.
23
23
24
24
This guide shows how you can take advantage of Native Image linking options including fully dynamic, fully static, and mostly static (except `libc`) to generate an executable ideal for your deployment scenario.
25
25
26
26
## Prerequisites and Preparation
27
27
28
-
The following prerequisites should be met:
29
-
30
-
- Linux AMD64 operating system
31
-
- GraalVM distribution for Java 17 of higher
28
+
- Linux x64 operating system
29
+
- GraalVM distribution for Java 17 or higher
32
30
- A 64-bit `musl` toolchain, `make`, and `configure`
33
31
- The latest `zlib` library
34
32
35
-
1. Install a GraalVM JDK.
36
-
The easiest way to get started is with [SDKMAN!](https://sdkman.io/jdks#graal).
33
+
The easiest way to install GraalVM is with [SDKMAN!](https://sdkman.io/jdks#graal).
37
34
For other installation options, visit the [Downloads section](https://www.graalvm.org/downloads/).
38
35
39
-
2. Next, you should install the `musl` toolchain, compile and install `zlib` into the toolchain.
40
-
Download the `musl` toolchain from [musl.cc](https://musl.cc/).
3. Download the latest `zlib` library sources from [zlib.net](https://zlib.net/) and extract them. (This documentation uses `zlib-1.2.11`.)
45
-
46
-
4. Create a new environment variable, named `CC`:
47
-
```bash
48
-
CC=$TOOLCHAIN_DIR/bin/gcc
49
-
```
50
-
51
-
5. Change into the `zlib` directory, and then run the following commands to compile and install `zlib` into the toolchain:
52
-
```bash
53
-
./configure --prefix=$TOOLCHAIN_DIR --static
54
-
make
55
-
make install
56
-
```
71
+
With the requirements set up, create the demo.
57
72
58
73
## Build a Static Native Executable
59
74
@@ -77,27 +92,17 @@ Extract the toolchain to a directory of your choice. This directory will be refe
77
92
```
78
93
This application iterates over your environment variables and prints out the ones that contain the `String` of characters passed as a command line argument.
79
94
80
-
2. Ensure the directory named `$TOOLCHAIN_DIR/bin` is present on your `PATH`.
81
-
To verify this, run the following command:
82
-
```bash
83
-
x86_64-linux-musl-gcc
84
-
```
85
-
You should see output similar to the following:
86
-
```
87
-
x86_64-linux-musl-gcc: fatal error: no input files
88
-
compilation terminated.
89
-
```
90
-
3. Compile the file:
95
+
2.Compile the application:
91
96
```shell
92
97
javac EnvMap.java
93
98
```
94
99
95
-
4. Build a static native executable by running this command:
100
+
3.Build a staticnative executable by running this command:
96
101
```shell
97
102
native-image --static--libc=musl EnvMap
98
103
```
99
104
This produces a native executable with statically linked system libraries.
100
-
You can pass other arguments before a class or JAR file.
105
+
You can pass other arguments before a classor JAR file. Run it with `./envmap`.
101
106
102
107
## Build a Mostly-Static Native Executable
103
108
@@ -114,9 +119,10 @@ To build a mostly-static native executable for the above `EnvMap` demo, run:
114
119
native-image --static-nolibc EnvMap
115
120
```
116
121
117
-
This produces a native executable that statically links all involved libraries (including JDK shared libraries) except for`libc`.
118
-
This includes `zlib`. Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
119
-
One way to check what dynamic libraries your application depends on is to run `ldd` with the native executable, for example, `ldd helloworld`.
122
+
This produces a native executable that statically links all involved libraries (including JDK-shared static libraries) except for `libc`.
123
+
This includes `zlib`.
124
+
Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
125
+
One way to check what dynamic libraries your application depends on is to run `ldd` with the native executable, for example, `ldd envmap`.
0 commit comments