# Java Utility Package (Open Source)

### A user-friendly library for building small- to medium-scale back-end applications.

Provides classes for logging, PBKDF2 hashing, JDBC access, SMTP and HTTP communication, FIFO/LIFO queues, threading, file operations, and various utilities.

<a href="downloads/package-ch-k43-util" class="button primary">Free Download</a>

{% hint style="success" %}
*Follow the development at Mastodon* [*#JavaUtil*](https://swiss.social/tags/javautil)*.*

*If you have any questions or feedback, feel free to contact me at* [*andy.brunner@k43.ch*](mailto:andy.brunner@k43.ch)
{% endhint %}

### Features

* [**Advanced Logging Capabilities**](https://java-util.k43.ch/examples/logging)\
  Simplify logging with an easy-to-use framework based on the standard Java Logger. Includes enhanced logging handlers (JDBC and SMTP) and versatile logging formatters (Tabular Text, CSV, XML, JSON, and YAML).
* [**Password Vault**](https://java-util.k43.ch/examples/password-vault)\
  Securely hash, store, and verify passwords using PBKDF2 with the PBKDF2WithHmacSHA512 algorithm.
* [**Database Integration**](https://java-util.k43.ch/examples/jdbc-database)\
  Seamlessly access any JDBC-compliant database and retrieve data in multiple formats, including JSON, YAML, CSV, XML, tabular, or raw Java objects.
* [**Email Functionality**](https://java-util.k43.ch/examples/smtp-mailer)\
  Create and send MIME multipart SMTP messages with support for text, HTML content, and file attachments.
* [**HTTP / Socket Support**](https://java-util.k43.ch/examples/http-socket-server)\
  Create raw TCP/IP socket applications or HTTP server applications (for web or REST services).
* [**FIFO / LIFO Queues**](https://java-util.k43.ch/examples/fifo-lifo-queue)\
  Simple to use, thread-safe and named queues with first-in-first-out and last-in-first-out modes.
* [**Command Argument Parser**](https://java-util.k43.ch/examples/command-argument-parser)\
  A simple command line argument parser to define, parse and retrieve command options and arguments.
* [**Thread Management**](https://java-util.k43.ch/examples/java-thread)\
  Leverage a convenient class for starting and terminating Java threads with ease.
* [**File Operations**](https://java-util.k43.ch/examples/file-tools)\
  Read and write files in various formats, including byte streams, character streams, Java Properties, and JSON data.
* [**Utility Methods**](https://java-util.k43.ch/examples/utility-class)\
  Access a large collection of static utility functions for common programming tasks.
* [**GraalVM Support**](https://java-util.k43.ch/examples/tips-faq#graalvm-native-image-support)\
  The generation of native executable code with GraalVM is supported by including the necessary reflection definitions in the distributed JAR file.

### Logging Example

<details>

<summary>Code / Debug Output</summary>

```java
try {
   int result = 10 / 0;
   } catch (Exception e) {
      KLog.error("Unexpected error occured", e);
   }
}
```

```
2025-09-03T16:48:28.946 D main[1]:ch.k43.util.KLog:<clinit>:199                        ===== Application started 2025-09-03T16:48:28.904 =====
2025-09-03T16:48:28.947 D main[1]:ch.k43.util.KLog:<clinit>:200                        Java Utility Package (Open Source/Freeware) Version 2025.09.02
2025-09-03T16:48:28.947 D main[1]:ch.k43.util.KLog:<clinit>:201                        Homepage java-util.k43.ch - Please send any feedback to andy.brunner@k43.ch
2025-09-03T16:48:28.947 D main[1]:ch.k43.util.KLog:<clinit>:204                        KLog properties read from file KLog.properties
2025-09-03T16:48:28.969 D main[1]:ch.k43.util.KLog:<clinit>:220                        Network host abmacbookpro (10.0.0.104)
2025-09-03T16:48:28.969 D main[1]:ch.k43.util.KLog:<clinit>:224                        OS platform Mac OS X Version 26.0/aarch64
2025-09-03T16:48:28.972 D main[1]:ch.k43.util.KLog:<clinit>:229                        OS disk space total 3.63 TiB, free 2.28 TiB, usable 2.28 TiB
2025-09-03T16:48:28.973 D main[1]:ch.k43.util.KLog:<clinit>:235                        Java version 23 (Java HotSpot(TM) 64-Bit Server VM - Oracle Corporation)
2025-09-03T16:48:28.973 D main[1]:ch.k43.util.KLog:<clinit>:240                        Java directory /Library/Java/JavaVirtualMachines/graalvm-jdk-23.0.1+11.1/Contents/Home
2025-09-03T16:48:28.973 D main[1]:ch.k43.util.KLog:<clinit>:245                        Java CPUs 10, de/CH, UTF-8, UTC +02:00 (Europe/Zurich)
2025-09-03T16:48:28.973 D main[1]:ch.k43.util.KLog:<clinit>:255                        Java heap maximum 16.00 GiB, current 1.01 GiB, used 9.94 MiB, free 1022.06 MiB
2025-09-03T16:48:28.973 D main[1]:ch.k43.util.KLog:<clinit>:262                        Java classpath ../bin/:../lib/angus-mail-2.0.3.jar:../lib/jakarta.mail-api-2.1.3.jar:../lib/org.json.20230618.jar:../lib/h2-2.2.224.jar:../lib/jakarta.activation-api-2.1.3.jar:../lib/angus-activation-2.0.2.jar
2025-09-03T16:48:28.974 D main[1]:ch.k43.util.KLog:<clinit>:266                        Current user andybrunner, language de, home directory /Users/andybrunner/
2025-09-03T16:48:28.974 D main[1]:ch.k43.util.KLog:<clinit>:272                        Current directory /Users/andybrunner/Documents/Eclipse-Workspace/Java-Utility-Package/src/
2025-09-03T16:48:28.974 D main[1]:ch.k43.util.KLog:<clinit>:276                        Temporary directory /var/folders/9s/tbyqn_vn7bs9rf3f1rc2jpxw0000gn/T/
2025-09-03T16:48:28.974 D main[1]:ch.k43.util.K:getLocalData:1598                      Local data for thread main created
2025-09-03T16:48:28.974 D main[1]:ch.k43.util.K:saveError:2535                         Error message saved: Unexpected error occured
2025-09-03T16:48:28.974 E main[1]:Test:main:13                                         ===> Unexpected error occured
2025-09-03T16:48:28.975 E main[1]:Test:main:13                                         ===> Java Exception: java.lang.ArithmeticException: / by zero
2025-09-03T16:48:28.975 E main[1]:Test:main:13                                         ===> Stack Trace[1]: Test.main(Test.java:11)
```

</details>

### HTTP Example

<details>

<summary>Code / Output</summary>

```java
import ch.k43.util.KHTTPClient;
import ch.k43.util.KLog;

public class HTTPGet {

   public static void main(String[] args) {

      KHTTPClient http = new KHTTPClient();

      if (!http.get("https://reqbin.com/echo/get/json")) {
         KLog.error("Error: {}", http.getLastError());
      } else {
         System.out.println(http.getResponseDataAsString());
      }
   }
}
```

```
{"success":"true"}
```

</details>

### JDBC Example

<details>

<summary>Code / Output</summary>

```java
import ch.k43.util.KDB;
import ch.k43.util.KLog;

public class Database {
	
   public static void main(String[] args) {

      try (KDB db = new KDB(KDB.JDBC_H2, "jdbc:h2:mem:mydb", "", "")) {

         KLog.abort(!db.isConnected(), "Error: {}", db.getErrorMessage());
		
         db.exec("CREATE TABLE addresses (sequence INT AUTO_INCREMENT, lastname VARCHAR(20), firstname VARCHAR(20))");
		   
         db.prepare("INSERT INTO addresses (lastname, firstname) VALUES (?, ?)");
         db.execPrepare("Smith", "Joe");
         db.execPrepare("Miller", "Bob");
         db.execPrepare("Johnson", "Evelyn");
         db.exec("SELECT * FROM addresses");

         System.out.println(db.getDataAsJSON());
      }
   }
}
```

```json
{
  "ADDRESSES": [
    {
      "SEQUENCE": 1,
      "LASTNAME": "Smith",
      "FIRSTNAME": "Joe"
    },
    {
      "SEQUENCE": 2,
      "LASTNAME": "Miller",
      "FIRSTNAME": "Bob"
    },
    {
      "SEQUENCE": 3,
      "LASTNAME": "Johnson",
      "FIRSTNAME": "Evelyn"
    }
  ]
}
```

</details>

### Prerequisites

* [Java SE 1.8](https://adoptium.net) (Java 8) or higher

The Java Utility Package does not require additional libraries with the exception of these classes:

| Class                        | Library                                                                                 |
| ---------------------------- | --------------------------------------------------------------------------------------- |
| KDB                          | [JDBC Driver](https://java-util.k43.ch/examples/jdbc-database#prerequisites)            |
| KSMTPMailer, KLogSMTPHandler | [Java Mail API/Jakarta EE](https://java-util.k43.ch/examples/smtp-mailer#prerequisites) |
| KFile                        | [org.json.JSONObject](https://mvnrepository.com/artifact/org.json/json)                 |

### Installation / Usage

* [Download](https://java-util.k43.ch/downloads/package-ch-k43-util) the latest Java JAR file
* Add *import ch.k43.util.\** statement in your Java code
* Make sure that the file ch.k43.util.jar can be found at compile time and in the classpath at runtime

### Test Installation

You may test the package by running the HelloWorld program, which is part of the distribution jar.

```
% java -jar ch.k43.util.jar
Java Utility Package (Freeware) Version 2025.02.19
Note: To enable logging, place a valid KLog.properties file in the current directory
JVM version 23 running on Mac OS X Version 15.3.1/aarch64
```

{% hint style="info" %}
See the section [Tips / FAQ](https://java-util.k43.ch/examples/tips-faq#graalvm-native-image-support) on how to create a native binary executable with GraalVM.
{% endhint %}

### Motivation for this library

In my professional life as an administrator and developer, I have benefited many times from countless freeware and open source products. It is therefore natural for me to also contribute to this community (see my other [freeware projects](https://k43.ch/freeware-tools/)).

This collection of Java classes was created in the course of various projects and will be further developed (see [Design Goals](https://java-util.k43.ch/examples/tips-faq#design-goals)). I hope that this tool will also serve you well.

### Freeware / Open Source / Unlicensed

This software is [freeware](https://en.wikipedia.org/wiki/Freeware), [open source](https://en.wikipedia.org/wiki/Open_source) and [unlicensed](https://unlicense.org). It was created with love and passion in the beautiful country of 🇨🇭 Switzerland. This software shall be used for Good not Evil. As far as I know, no animal was harmed in the making of this software 😊
