Skip to content

Commit 0d92d84

Browse files
committed
Add Java 9+ module support (JPMS) for jlink compatibility
1 parent 60ff7ee commit 0d92d84

4 files changed

Lines changed: 194 additions & 1 deletion

File tree

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,85 @@ an application can include this as a dependency in the application's
231231
</project>
232232
```
233233

234+
## Java 9+ Module Support (JPMS)
235+
236+
wolfSSL JNI/JSSE supports the Java Platform Module System (JPMS) introduced in
237+
Java 9. This allows the library to be used with `jlink` to create custom Java
238+
runtimes.
239+
240+
### How It Works
241+
242+
The build system uses conditional compilation to include module support only
243+
when building with Java 9 or later:
244+
245+
| JDK Used to Build | Resulting JAR |
246+
| --- | --- |
247+
| Java 8 | Standard JAR (classpath only) |
248+
| Java 9+ | Modular JAR (works with both classpath and module path) |
249+
250+
When building with Java 9+, a `module-info.class` is included in the JAR that:
251+
- Declares the module as `com.wolfssl`
252+
- Exports `com.wolfssl` and `com.wolfssl.provider.jsse` packages
253+
- Registers `WolfSSLProvider` as a `java.security.Provider` service
254+
255+
### Building a Modular JAR
256+
257+
To build a modular JAR, simply use Java 9 or later when building. Both Ant and
258+
Maven builds support automatic module-info compilation.
259+
260+
**Using Ant:**
261+
262+
```
263+
$ export JAVA_HOME=/path/to/jdk11 # or any JDK 9+
264+
$ ./java.sh
265+
$ ant
266+
```
267+
268+
**Using Maven:**
269+
270+
```
271+
$ export JAVA_HOME=/path/to/jdk11 # or any JDK 9+
272+
$ ./java.sh
273+
$ mvn package
274+
```
275+
276+
Maven uses a profile (`java9-module`) that automatically activates on Java 9+
277+
to compile and include `module-info.class` in the JAR.
278+
279+
You can verify module support with:
280+
281+
```
282+
$ jar --describe-module --file=lib/wolfssl-jsse.jar
283+
com.wolfssl jar:file:///path/to/lib/wolfssl-jsse.jar/!module-info.class
284+
exports com.wolfssl
285+
exports com.wolfssl.provider.jsse
286+
requires java.logging
287+
provides java.security.Provider with com.wolfssl.provider.jsse.WolfSSLProvider
288+
```
289+
290+
### Using with jlink
291+
292+
Once built with Java 9+, the JAR can be used with `jlink` to create custom
293+
Java runtimes:
294+
295+
```
296+
$ jlink \
297+
--module-path lib/wolfssl-jsse.jar \
298+
--add-modules com.wolfssl \
299+
--output custom-runtime \
300+
--no-header-files \
301+
--no-man-pages
302+
```
303+
304+
This creates a minimal Java runtime with wolfJSSE included, which can be
305+
deployed independently.
306+
307+
### Java 8 Compatibility
308+
309+
Java 8 users can still build and use wolfSSL JNI/JSSE normally. When building
310+
with Java 8, the `module-info.java` is automatically excluded from compilation,
311+
and the resulting JAR works as a standard classpath JAR.
312+
234313
## Examples
235314

236315
Examples of using wolfssljni can be found in the `./examples` subdirectory.

build.xml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<!-- set properties for this build -->
2020
<property name="src.dir" value="src/java/"/>
21+
<property name="src.java9.dir" value="src/java9/"/>
2122
<property name="native.dir" value="native"/>
2223
<property name="lib.dir" value="lib/"/>
2324
<property name="build.dir" value="build"/>
@@ -38,6 +39,18 @@
3839
<!-- check for SNIHostName class to determine if JDK version >= 1.8 -->
3940
<available property="have-SNIHostName" classname="javax.net.ssl.SNIHostName" />
4041

42+
<!-- Detect Java 9+ for module-info.java compilation -->
43+
<condition property="isJava9Plus">
44+
<not>
45+
<or>
46+
<equals arg1="${ant.java.version}" arg2="1.5"/>
47+
<equals arg1="${ant.java.version}" arg2="1.6"/>
48+
<equals arg1="${ant.java.version}" arg2="1.7"/>
49+
<equals arg1="${ant.java.version}" arg2="1.8"/>
50+
</or>
51+
</not>
52+
</condition>
53+
4154
<!-- Detect if running on Windows host -->
4255
<condition property="isWindows">
4356
<os family="windows" />
@@ -98,7 +111,7 @@
98111
</delete>
99112
</target>
100113

101-
<target name="build" depends="init, compile-nativeheaderdir, compile-javah, jar, jar-jsse, javah, javadoc"/>
114+
<target name="build" depends="init, compile-nativeheaderdir, compile-javah, compile-module-info, jar, jar-jsse, javah, javadoc"/>
102115
<target name="build-jacoco" depends="init, compile-nativeheaderdir, compile-javah, jar, jar-jsse, javah, javadoc, examples, test-jacoco, coverage-report"/>
103116

104117
<target name="compile-nativeheaderdir" if="have-nativeheaderdir">
@@ -148,6 +161,22 @@
148161
</copy>
149162
</target>
150163

164+
<!-- Compile module-info.java for Java 9+ module support.
165+
Only runs when building with Java 9 or later. When building with
166+
Java 8 or earlier, this target is skipped and the resulting JAR
167+
will be a standard (non-modular) JAR file. -->
168+
<target name="compile-module-info" if="isJava9Plus"
169+
description="Compile module-info.java for Java 9+ (skipped on Java 8)">
170+
<javac srcdir="${src.java9.dir}"
171+
destdir="${build.dir}"
172+
release="9"
173+
modulepath="${build.dir}"
174+
includeantruntime="false">
175+
<include name="module-info.java"/>
176+
</javac>
177+
<echo message="Compiled module-info.java for Java 9+ module support"/>
178+
</target>
179+
151180
<target name="javah" unless="have-nativeheaderdir">
152181
<javah destdir="${native.dir}" force="yes" classpathref="classpath">
153182
<class name="com.wolfssl.WolfSSL"/>

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* module-info.java
2+
*
3+
* Copyright (C) 2006-2026 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+
* wolfSSL JNI/JSSE Module
24+
*
25+
* This module provides:
26+
* - JNI bindings to the native wolfSSL SSL/TLS library (com.wolfssl)
27+
* - A JSSE provider implementation (com.wolfssl.provider.jsse)
28+
*
29+
* Note: This module-info.java is only compiled when building with Java 9+.
30+
* When building with Java 8, this file is excluded and the resulting JAR
31+
* will be a standard (non-modular) JAR that works on the classpath.
32+
*/
33+
module com.wolfssl {
34+
/* Required modules */
35+
requires java.logging;
36+
37+
/* Export public API packages */
38+
exports com.wolfssl;
39+
exports com.wolfssl.provider.jsse;
40+
41+
/* Declare service usage for ServiceLoader.load(Provider.class) */
42+
uses java.security.Provider;
43+
44+
/* Register wolfJSSE as a security provider */
45+
provides java.security.Provider
46+
with com.wolfssl.provider.jsse.WolfSSLProvider;
47+
}

0 commit comments

Comments
 (0)