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
+44-44Lines changed: 44 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -16,49 +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
-
Next, you should install the `musl` toolchain, compile, and install `zlib` into the toolchain.
40
-
2. Download the `musl` toolchain from [musl.libc.org](https://musl.libc.org/)
41
-
We recommend [musl-1.2.4](ttps://musl.libc.org/releases/musl-1.2.4.tar.gz).
42
-
Extract the toolchain to a directory of your choice.
43
-
Create a new environment variable, named `$TOOLCHAIN_DIR`, and set it to this directory.
44
-
```bash
45
-
export TOOLCHAIN_DIR=/path/to/musl-1.2.4
46
-
```
47
-
48
-
3. Download the latest `zlib` library sources from [zlib.net](https://zlib.net/) and extract them. (This example application was tested with [zlib-1.2.13](https://zlib.net/fossils/zlib-1.2.13.tar.gz).)
49
-
50
-
4. Create a new environment variable, named `CC`:
51
-
```bash
52
-
CC=$TOOLCHAIN_DIR/bin/gcc
53
-
```
36
+
To be able to create static native applications with Native Image, a `musl` toolchain with the `zlib` library are required on the system.
37
+
For the best compatibility, use [musl-1.2.4](https://musl.libc.org/releases/musl-1.2.4.tar.gz) or later.
38
+
We recommend building `musl` from [source](https://musl.libc.org/) as shown below:
5. Change into the `zlib` directory, and then run the following commands to compile and install `zlib` into the toolchain:
56
-
```bash
57
-
cd zlib-1.2.13
58
-
./configure --prefix=$TOOLCHAIN_DIR --static
59
-
make
60
-
make install
61
-
```
71
+
With the requirements set up, create the demo.
62
72
63
73
## Build a Static Native Executable
64
74
@@ -82,27 +92,17 @@ Create a new environment variable, named `$TOOLCHAIN_DIR`, and set it to this di
82
92
```
83
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.
84
94
85
-
2. Ensure the directory named `$TOOLCHAIN_DIR/bin` is present on your `PATH`.
86
-
To verify this, run the following command:
87
-
```bash
88
-
x86_64-linux-musl-gcc
89
-
```
90
-
You should see output similar to the following:
91
-
```
92
-
x86_64-linux-musl-gcc: fatal error: no input files
93
-
compilation terminated.
94
-
```
95
-
3. Compile the file:
95
+
2.Compile the application:
96
96
```shell
97
97
javac EnvMap.java
98
98
```
99
99
100
-
4. Build a static native executable by running this command:
100
+
3.Build a staticnative executable by running this command:
101
101
```shell
102
102
native-image --static--libc=musl EnvMap
103
103
```
104
104
This produces a native executable with statically linked system libraries.
105
-
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`.
106
106
107
107
## Build a Mostly-Static Native Executable
108
108
@@ -119,7 +119,7 @@ To build a mostly-static native executable for the above `EnvMap` demo, run:
119
119
native-image --static-nolibc EnvMap
120
120
```
121
121
122
-
This produces a native executable that statically links all involved libraries (including JDKshared libraries) except for`libc`.
122
+
This produces a native executable that statically links all involved libraries (including JDK-sharedstatic libraries) except for `libc`.
123
123
This includes `zlib`.
124
124
Also, depending on the user's code, it may link `libstdc+` and `libgcc`.
125
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