README.md
1## AWS CRT Java
2
3Java Bindings for the AWS Common Runtime
4
5## License
6
7This library is licensed under the Apache 2.0 License.
8
9*__Jump To:__*
10
11* [Platform Specific Building](#platform)
12 * [Linux/Unix](#linuxunix)
13 * [OSX](#osx)
14 * [Windows](#windows)
15 * [Android](./android/ANDROID.md)
16* [Java CRT Documentation](https://awslabs.github.io/aws-crt-java/)
17* [Installing](#installing)
18* [Platform-Specific JARs](#platform-specific-jars)
19* [System Properties](#system-properties)
20* [TLS Behavior](#tls-behavior)
21* [Testing](#testing)
22* [IDEs](#ides)
23* [Debugging](#debugging)
24
25
26## Platform
27
28### Linux/Unix
29Requirements:
30* Clang 3.9+ or GCC 4.4+
31* cmake 3.1+
32* Java: Any JDK8 or above, ensure `JAVA_HOME` is set
33* Maven
34
35Building:
361) `apt-get install cmake3 maven openjdk-8-jdk-headless -y`
372) `git clone https://github.com/awslabs/aws-crt-java.git`
383) `cd aws-crt-java`
394) `git submodule update --init --recursive`
405) `mvn compile`
41
42### OSX
43Requirements:
44* cmake 3.1
45* ninja
46* Java: Any JDK8 or above, ensure `JAVA_HOME` is set
47* Maven
48
49Building:
501) `brew install maven cmake` (if you have homebrew installed, otherwise install these manually)
512) `git clone https://github.com/awslabs/aws-crt-java.git`
523) `cd aws-crt-java`
534) `git submodule update --init --recursive`
545) `mvn compile`
55
56### Windows
57Requirements:
58* Visual Studio 2015 or above
59* CMake 3.1
60* Java: Any JDK8 or above, ensure `JAVA_HOME` is set
61* Maven
62
63Building:
641) `choco install maven` (if you have chocolatey installed), otherwise install maven and the JDK manually
652) `git clone https://github.com/awslabs/aws-crt-java.git`
663) `cd aws-crt-java`
674) `git submodule update --init --recursive`
685) `mvn compile`
69
70NOTE: Make sure you run this from a VS Command Prompt or have run `VCVARSALL.BAT` in your current shell so
71CMake can find Visual Studio.
72
73## Documentation
74[Java CRT Documentation](https://awslabs.github.io/aws-crt-java/)
75
76## Installing
77From the aws-crt-java directory:
78```mvn install```
79From maven: (https://search.maven.org/artifact/software.amazon.awssdk.crt/aws-crt/)
80
81## Platform-Specific JARs
82
83The `aws-crt` JAR in Maven Central is a large "uber" jar that contains compiled C libraries for many different platforms (Windows, Linux, etc). If size is an issue, you can pick a smaller platform-specific JAR by setting the `<classifier>`.
84
85``` xml
86 <!-- Platform-specific Linux x86_64 JAR -->
87 <dependency>
88 <groupId>software.amazon.awssdk.crt</groupId>
89 <artifactId>aws-crt</artifactId>
90 <version>0.20.5</version>
91 <classifier>linux-x86_64</classifier>
92 </dependency>
93```
94
95``` xml
96 <!-- "Uber" JAR that works on all platforms -->
97 <dependency>
98 <groupId>software.amazon.awssdk.crt</groupId>
99 <artifactId>aws-crt</artifactId>
100 <version>0.20.5</version>
101 </dependency>
102```
103
104### Available platform classifiers
105
106- linux-armv6 (no auto-detect)
107- linux-armv7 (no auto-detect)
108- linux-aarch_64
109- linux-x86_32
110- linux-x86_64
111- linux-x86_64-musl (no auto-detect)
112- linux-armv7-musl (no auto-detect)
113- linux-aarch_64-musl (no auto-detect)
114- osx-aarch_64
115- osx-x86_64
116- windows-x86_32
117- windows-x86_64
118
119### Auto-detect
120
121The [os-maven-plugin](https://github.com/trustin/os-maven-plugin) can automatically detect your platform's classifier at build time.
122
123**NOTES**: The auto-detected `linux-arm_32` platform classifier is not supported, you must specify `linux-armv6` or `linux-armv7`.
124Additionally, musl vs glibc detection is not supported either. If you are deploying to a musl-based system and wish to use
125a classifier-based jar, you must specify the classifier name yourself.
126
127``` xml
128<build>
129 <extensions>
130 <!-- Generate os.detected.classifier property -->
131 <extension>
132 <groupId>kr.motd.maven</groupId>
133 <artifactId>os-maven-plugin</artifactId>
134 <version>1.7.0</version>
135 </extension>
136 </extensions>
137 </build>
138
139 <dependencies>
140 <dependency>
141 <groupId>software.amazon.awssdk.crt</groupId>
142 <artifactId>aws-crt</artifactId>
143 <version>0.20.5</version>
144 <classifier>${os.detected.classifier}</classifier>
145 </dependency>
146 <dependencies>
147```
148
149## System Properties
150
151- To enable logging, set `aws.crt.log.destination` or `aws.crt.log.level`:
152 - `aws.crt.log.level` - Log level. May be: "None", "Fatal", "Error", "Warn" (default), "Info", "Debug", "Trace".
153 - `aws.crt.log.destination` - Log destination. May be: "Stderr" (default), "Stdout", "File", "None".
154 - `aws.crt.log.filename` - File to use when `aws.crt.log.destination` is "File".
155- `aws.crt.libc` - (Linux only) Set to "musl" or "glibc" if CRT cannot properly detect which to use.
156- `aws.crt.lib.dir` - Set directory where CRT may extract its native library (by default, `java.io.tmpdir` is used)
157- `aws.crt.memory.tracing` - May be: "0" (default, no tracing), "1" (track bytes), "2" (more detail).
158 Allows the CRT.nativeMemory() and CRT.dumpNativeMemory() functions to report native memory usage.
159
160## TLS Behavior
161
162The CRT uses native libraries for TLS, rather than Java's typical
163Secure Socket Extension (JSSE), KeyStore, and TrustStore.
164On [Windows](https://learn.microsoft.com/en-us/windows/win32/security) and
165[Apple](https://developer.apple.com/documentation/security) devices,
166the built-in OS libraries are used.
167On Linux/Unix/etc [s2n-tls](https://github.com/aws/s2n-tls) is used.
168
169If you need to add certificates to the trust store, add them to your OS trust store.
170The CRT does not use the Java TrustStore. For more customization options, see
171[TlsContextOptions](https://awslabs.github.io/aws-crt-java/software/amazon/awssdk/crt/io/TlsContextOptions.html) and
172[TlsConnectionOptions](https://awslabs.github.io/aws-crt-java/software/amazon/awssdk/crt/io/TlsConnectionOptions.html).
173
174### Mac-Only TLS Behavior
175
176Please note that on Mac, once a private key is used with a certificate, that certificate-key pair is imported into the Mac Keychain. All subsequent uses of that certificate will use the stored private key and ignore anything passed in programmatically. Beginning in v0.6.6, when a stored private key from the Keychain is used, the following will be logged at the "info" log level:
177
178```
179static: certificate has an existing certificate-key pair that was previously imported into the Keychain. Using key from Keychain instead of the one provided.
180```
181
182## Testing
183Many tests require environment variables to be set. These environment variables are translated at runtime to system properties for use by the tests. These tests will be quietly skipped if the properties they require are not set.
184
185Environment variables can be set like so:
186```
187export ENV_VARIABLE_NAME="<variable value>"
188```
189Many tests require that you have [set up](https://console.aws.amazon.com/iot) an AWS IoT Thing.
190
191Partial list of environment variables:
192- `AWS_TEST_MQTT311_IOT_CORE_HOST`: AWS IoT service endpoint hostname for MQTT3
193- `AWS_TEST_MQTT311_IOT_CORE_RSA_CERT`: Path to the IoT thing certificate for MQTT3
194- `AWS_TEST_MQTT311_IOT_CORE_RSA_KEY`: Path to the IoT thing private key for MQTT3
195- `AWS_TEST_MQTT311_IOT_CORE_ECC_CERT`: Path to the IoT thing with EC-based certificate for MQTT3
196- `AWS_TEST_MQTT311_IOT_CORE_ECC_KEY`: Path to the IoT thing with ECC private key for MQTT3 (The ECC key file should only contains the ECC Private Key section to working on MacOS.)
197- `AWS_TEST_MQTT311_ROOT_CA`: Path to the root certificate
198- `AWS_TEST_HTTP_PROXY_HOST`: Hostname of proxy
199- `AWS_TEST_HTTP_PROXY_PORT`: Port of proxy
200- `NETWORK_TESTS_DISABLED`: Set this if tests are running in a constrained environment where network access is not guaranteed/allowed.
201
202Other Environment Variables that can be set can be found in the `SetupTestProperties()` function in [CrtTestFixture.java](https://github.com/awslabs/aws-crt-java/blob/main/src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java)
203
204These can be set persistently via Maven settings (usually in `~/.m2/settings.xml`):
205```xml
206<settings>
207 ...
208 <profiles>
209 <profile>
210 <activation>
211 <activeByDefault>true</activeByDefault>
212 </activation>
213 <properties>
214 <crt.test.endpoint>XXXXXXXXXX-ats.iot.us-east-1.amazonaws.com</crt.test.endpoint>
215 <crt.test.certificate>/path/to/XXXXXXXX-certificate.pem.crt</crt.test.certificate>
216 <crt.test.privatekey>/path/to/XXXXXXXX-private.pem.key</crt.test.privatekey>
217 <crt.test.rootca>/path/to/AmazonRootCA1.pem</crt.test.rootca>
218 ... etc ...
219 </properties>
220 </profile>
221 </profiles>
222</settings>%
223```
224
225## IDEs
226* CMake is configured to export a compilation database at target/cmake-build/compile_commands.json
227* CLion: Build once with maven, then import the project as a [Compilation Database Project](https://www.jetbrains.com/help/clion/compilation-database.html)
228* VSCode: will detect that this is both a java project and if you have the CMake extension, you can point that at CMakeLists.txt and the compilation database
229
230## Debugging
231Tests can be debugged in Java/Kotlin via the built-in tooling in VSCode and IntelliJ. If you need to debug the native code, it's a bit trickier.
232
233To debug native code with VSCode or CLion or any other IDE:
2341. Find your ```mvn``` launch script(e.g. ```realpath $(which mvn)```) and pull the command line at the bottom from it. This changes between versions
235 of maven, so it is difficult to give consistent directions.
236
237 As an example, for Maven 3.6.0 on Linux:
238 ```/path/to/java -classpath /usr/share/java/plexus-classworlds-2.5.2.jar -Dclassworlds.conf=/usr/share/maven/bin/m2.conf -Dmaven.home=/usr/share/maven -Dlibrary.jansi.path=/usr/share/maven/lib/jansi-native -Dmaven.multiModuleProjectDirectory=. org.codehaus.plexus.classworlds.launcher.Launcher test -DforkCount=0 -Ddebug.native -Dtest=HttpClientConnectionManager#testMaxParallelConnections```
239
240 The important parts are:
241 * ```-DforkCount=0``` - prevents the Maven process from forking to run tests, so your debugger will be attached to the right process. You can ignore this if
242 you configure your debugger to attach to child processes.
243 * ```-Ddebug.native``` - Makes CMake compile the JNI bindings and core libraries in debug. By default, we compile in release with symbols, which will help
244 for call stacks, but less so for live debugging.
2452. Set the executable to launch to be your java binary (e.g. ```/usr/bin/java```)
2463. Set the parameters to be the ones used by the ```mvn``` script, as per above
2474. Set the working directory to the `aws-crt-java` directory
2485. On windows, you will need to manually load the PDB via the Modules window in Visual Studio, as it is not embedded in the JAR. It will be in the ```target/cmake-build/lib/windows/<arch>``` folder.
249