# Tips / FAQ

### Design Goals

1. **Ease of use**: The classes and methods must be flexible and simple to use.
2. **No UI calls**: Do everything without user interface to allow this library to be used for background tasks or server processes.
3. **Fast**: Write the code as performant as possible.
4. **Favor memory usage over I/O:** In today's world, memory is no longer a limiting factor. Therefore, many operations can be done in memory where (temporary) files were used in the past (e.g. KDB creates a data structure from SQL SELECT, KFile operations are mostly in memory).
5. **Use extensive logging**: The KLog.debug() and KLog.error() functions are used throughout the code to help debugging your code. Also use the toString() methods found in each class to show the internal field values of the objects during development.
6. **Platform independence**: Write everything platform independent.
7. **Minimize prerequisites**: Adhere to standard Java SE libraries, maintain compatibility with Java 8 and later, and limit the use of external JAR files to essential cases only (e.g., KSMTPMailer, JDBC drivers).

***

### GraalVM Native Image Support

The Java Utility Package supports the creation of [GraalVM](https://www.graalvm.org) native executables by including the necessary Java reflection definitions within the JAR file.

**Prerequisites**

* macOS: [Xcode](https://apps.apple.com/ch/app/xcode/id497799835?mt=12)
* Windows: [Visual Studio Build Tools 2022](https://visualstudio.microsoft.com/) or later
* [GraalVM](https://www.graalvm.org/latest/getting-started/)

**Compile / Create Executable / Sign / Run (macOS)**

```bash
% javac YourApplication.java
% native-image -cp ch.k43.util.jar:. YourApplication
% codesign --sign "Your Code Signature (XXXXXXXXXX)" YourApplication
% ./YourApplication
```

**Example: GetCurrentVersion (macOS/arm64 executable)**

{% file src="<https://1523050323-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnLy540wu2J4F0Txdp4tM%2Fuploads%2F8FJ56WACR5nMLXhh5yuu%2FGetCurrentVersion.zip?alt=media&token=43c7c78d-032d-4edf-96c9-ebb8f028ac3d>" %}

***

### JShell

For a quick test of any ch.k43.util function, utilize the interactive JShell command included in the JDK. Ensure the ch.k43.util JAR file is either located in the current directory or accessible via the directory specified in the class-path argument.

{% code lineNumbers="true" fullWidth="false" %}

```sh
% jshell --class-path "./*"
jshell> import ch.k43.util.*;

jshell> K.queryDNS("MX","yahoo.com");
$2 ==> String[3] { "mta5.am0.yahoodns.net", "mta6.am0.yahoodns.net", "mta7.am0.yahoodns.net" }

jshell> K.getUniqueID();
$3 ==> "36973229-FB14-4D95-9AA7-EEBFA2CC84FD"

jshell> K.getTimeISO8601();
$4 ==> "2025-02-22T08:30:34.145"

jshell> K.isInteger("12.3");
$5 ==> false

jshell> KHTTPClient http = new KHTTPClient();
http ==> KHTTPClient [gHTTPResponseHeaders=null, gHTTPResp ... ode=-1, gHTTPTimeOutSec=5]

jshell> http.get("https://reqbin.com/echo/get/json")
$7 ==> true

jshell> System.out.println(http.getResponseDataAsString());
{"success":"true"}

jshell> /exit
```

{% endcode %}

{% hint style="info" %}
See the [Java Shell User Guide](https://docs.oracle.com/en/java/javase/21/jshell/introduction-jshell.html) on how to use *JShell*
{% endhint %}

***

### Java Stores

There are two keystore files used in Java:

* **Key Store**: These files contain certificates to proof authentication with a private key. Certificates can be server certificates (e.g. [www.acme.com](http://www.acme.com)), intermediate certificates or client certificates (e.g. <john.doe@acme.com>).
* **Trust Store**: These files contain public certificates of trusted identities. Certificates are trusted root certificates (e.g. Verisign, Microsoft, etc.), intermediates certificates or trusted server and client certificates. A default file (usually named cacerts with the default password changeit) is shipped with the JVM.

**Create Self Signed Certificate**

```sh
% openssl req -x509 -newkey rsa:2048 -sha256 -keyout key.pem -out cert.pem -days 365 -nodes
```

**Convert PEM certificate to P12**

```sh
% openssl pkcs12 -export -in certificate.pem -out certificate.p12 
```

**Create JKS file from P12 certificate**

```sh
% keytool -importkeystore -srckeystore certificate.p12 -srcstoretype pkcs12 -destkeystore keyfile.jks
```

**Add Client Certificate to JKS file**

```sh
% keytool -import -trustcacerts -file clientcert.pem -keypass clientCertPass -storepass keyfilePass -keystore keyfile.jks
```

**Display Content of JKS file**

```sh
% keytool -list -v -keystore keyfile.jks
```

**Simple TLS Server**

```bash
% openssl s_server -key key.pem -cert cert.pem -accept 443
```

**TLS Connection Test**

```bash
% openssl s_client -connect hostname:443
```
