Skip to content

Commit ebf46c1

Browse files
committed
Add Java 9+ module support (JPMS) for jlink compatibility
1 parent cb3ea58 commit ebf46c1

4 files changed

Lines changed: 160 additions & 2 deletions

File tree

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,51 @@ on the current release):
225225
```
226226

227227

228+
### Java 9+ Module Support (JPMS)
229+
---------
230+
231+
wolfCrypt JNI/JCE supports the Java Platform Module System (JPMS) introduced
232+
in Java 9. This enables use with `jlink` for creating custom, minimal Java
233+
runtimes.
234+
235+
**Module Information:**
236+
237+
- Module name: `com.wolfssl.wolfcrypt`
238+
- Exported packages: `com.wolfssl.wolfcrypt`, `com.wolfssl.provider.jce`
239+
- Service provider: `java.security.Provider` (WolfCryptProvider)
240+
241+
**Conditional Compilation:**
242+
243+
The `module-info.java` is conditionally compiled based on the JDK version used
244+
to build:
245+
246+
| JDK Used to Build | Resulting JAR |
247+
|-------------------|---------------|
248+
| Java 8 | Standard JAR (no module-info.class) |
249+
| Java 9+ | Modular JAR (includes module-info.class) |
250+
251+
When building with Java 8, the `module-info.java` is automatically excluded
252+
from compilation, and the resulting JAR works as a standard classpath JAR.
253+
254+
**Using with jlink:**
255+
256+
When built with Java 9+, the wolfCrypt JNI/JCE JAR can be used with `jlink` to
257+
create a custom Java runtime that includes the wolfCrypt module:
258+
259+
```
260+
$ jlink \
261+
--module-path lib/wolfcrypt-jni.jar:$JAVA_HOME/jmods \
262+
--add-modules com.wolfssl.wolfcrypt \
263+
--output custom-runtime \
264+
--no-header-files \
265+
--no-man-pages
266+
267+
$ ./custom-runtime/bin/java --list-modules
268+
```
269+
270+
**Note:** The native wolfCrypt JNI shared library (`libwolfcryptjni.so/dylib`)
271+
must still be available on the native library path at runtime.
272+
228273
### Example / Test Code
229274
---------
230275

build.xml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
<!-- set properties for this build -->
2525
<property name="src.dir" value="src/main/java/" />
26+
<property name="src.java9.dir" value="src/java9/" />
2627
<property name="jni.dir" value="jni/include/" />
2728
<property name="lib.dir" value="lib/" />
2829
<property name="build.dir" value="build" />
@@ -48,6 +49,18 @@
4849

4950
<property environment="env" />
5051

52+
<!-- Detect Java 9+ for module-info.java compilation -->
53+
<condition property="isJava9Plus">
54+
<not>
55+
<or>
56+
<equals arg1="${ant.java.version}" arg2="1.5"/>
57+
<equals arg1="${ant.java.version}" arg2="1.6"/>
58+
<equals arg1="${ant.java.version}" arg2="1.7"/>
59+
<equals arg1="${ant.java.version}" arg2="1.8"/>
60+
</or>
61+
</not>
62+
</condition>
63+
5164
<!-- Detect if running on Windows host -->
5265
<condition property="isWindows">
5366
<os family="windows" />
@@ -161,8 +174,24 @@
161174
</copy>
162175
</target>
163176

177+
<!-- Compile module-info.java for Java 9+ module support.
178+
Only runs when building with Java 9 or later. When building with
179+
Java 8 or earlier, this target is skipped and the resulting JAR
180+
will be a standard (non-modular) JAR file. -->
181+
<target name="compile-module-info" if="isJava9Plus"
182+
description="Compile module-info.java for Java 9+ (skipped on Java 8)">
183+
<javac srcdir="${src.java9.dir}"
184+
destdir="${build.dir}"
185+
release="9"
186+
modulepath="${build.dir}"
187+
includeantruntime="false">
188+
<include name="module-info.java"/>
189+
</javac>
190+
<echo message="Compiled module-info.java for Java 9+ module support"/>
191+
</target>
192+
164193
<!-- create JAR with ONLY JNI classes, not to be used with JCE -->
165-
<target name="jar-jni" depends="compile-nativeheaderdir, compile-javah">
194+
<target name="jar-jni" depends="compile-nativeheaderdir, compile-javah, compile-module-info">
166195
<jar jarfile="${lib.dir}/wolfcrypt-jni.jar">
167196
<manifest>
168197
<attribute name="Implementation-Title"
@@ -174,12 +203,13 @@
174203
</manifest>
175204
<fileset dir="${build.dir}">
176205
<include name="com/wolfssl/wolfcrypt/*.class"/>
206+
<include name="module-info.class"/>
177207
</fileset>
178208
</jar>
179209
</target>
180210

181211
<!-- create JAR with JNI and JCE classes, use this when wanting JCE -->
182-
<target name="jar-jce" depends="compile-nativeheaderdir, compile-javah">
212+
<target name="jar-jce" depends="compile-nativeheaderdir, compile-javah, compile-module-info">
183213
<jar jarfile="${lib.dir}/wolfcrypt-jni.jar" basedir="${build.dir}">
184214
<manifest>
185215
<attribute name="Implementation-Title"

pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,42 @@
6969
</plugin>
7070
</plugins>
7171
</build>
72+
73+
<!-- Profile for Java 9+ module support.
74+
Automatically activates when building with JDK 9 or later.
75+
Compiles module-info.java and includes it in the JAR. -->
76+
<profiles>
77+
<profile>
78+
<id>java9-module</id>
79+
<activation>
80+
<jdk>[9,)</jdk>
81+
</activation>
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<groupId>org.apache.maven.plugins</groupId>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<version>3.11.0</version>
88+
<executions>
89+
<!-- Compile module-info.java with Java 9 release -->
90+
<execution>
91+
<id>compile-module-info</id>
92+
<phase>compile</phase>
93+
<goals>
94+
<goal>compile</goal>
95+
</goals>
96+
<configuration>
97+
<release>9</release>
98+
<compileSourceRoots>
99+
<compileSourceRoot>${project.basedir}/src/java9</compileSourceRoot>
100+
</compileSourceRoots>
101+
<multiReleaseOutput>false</multiReleaseOutput>
102+
</configuration>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
</profile>
109+
</profiles>
72110
</project>

src/java9/module-info.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* module-info.java
2+
*
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/**
23+
* wolfCrypt JNI/JCE Module
24+
*
25+
* This module provides:
26+
* - JNI bindings to the native wolfCrypt cryptography library
27+
* (com.wolfssl.wolfcrypt)
28+
* - A JCE provider implementation (com.wolfssl.provider.jce)
29+
*
30+
* Note: This module-info.java is only compiled when building with Java 9+.
31+
* When building with Java 8, this file is excluded and the resulting JAR
32+
* will be a standard (non-modular) JAR that works on the classpath.
33+
*/
34+
module com.wolfssl.wolfcrypt {
35+
/* Required modules */
36+
requires java.logging;
37+
38+
/* Export public API packages */
39+
exports com.wolfssl.wolfcrypt;
40+
exports com.wolfssl.provider.jce;
41+
42+
/* Register wolfJCE as a security provider */
43+
provides java.security.Provider
44+
with com.wolfssl.provider.jce.WolfCryptProvider;
45+
}

0 commit comments

Comments
 (0)