Page cover image

Java Utility Package (Open Source)

A high-performance and user-friendly toolkit tailored for developing small to medium-sized back-end applications

Free Download

Features

  • Advanced Logging Capabilities 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 Securely hash, store, and verify passwords using PBKDF2 with the PBKDF2WithHmacSHA512 algorithm.

  • Database Integration Seamlessly access any JDBC-compliant database and retrieve data in multiple formats, including JSON, YAML, CSV, XML, tabular, or raw Java objects.

  • Email Functionality Create and send MIME multipart SMTP messages with support for text, HTML content, and file attachments.

  • HTTP / Socket Support Create raw TCP/IP socket applications or HTTP server applications (for web or REST services).

  • Thread Management Leverage a convenient class for starting and terminating Java threads with ease.

  • File Operations Read and write files in various formats, including byte streams, character streams, Java Properties, and JSON data.

  • Utility Methods Access a large collection of static utility functions for common programming tasks.

  • GraalVM Support The generation of native executable code with GraalVM is supported by including the necessary reflection definitions in the distributed JAR file.

HTTP Example

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());
      }
   }
}
Output
{"success":"true"}
Debug Log
2025-02-16T10:20:47.583 D main[1]:ch.k43.util.KLog:<clinit>:121                        ===== Application started 2025-02-16T10:20:47.553 =====
2025-02-16T10:20:47.583 D main[1]:ch.k43.util.KLog:<clinit>:122                        Java Utility Package (Freeware) Version 2025.02.16
2025-02-16T10:20:47.584 D main[1]:ch.k43.util.KLog:<clinit>:123                        Homepage java-util.k43.ch - Please send any feedback to [email protected]
2025-02-16T10:20:47.584 D main[1]:ch.k43.util.KLog:<clinit>:126                        KLog properties read from file KLog.properties
2025-02-16T10:20:47.628 D main[1]:ch.k43.util.KLog:<clinit>:134                        Network host ab-macbook-pro (10.0.0.100)
2025-02-16T10:20:47.628 D main[1]:ch.k43.util.KLog:<clinit>:138                        OS platform Mac OS X Version 15.3.1/aarch64
2025-02-16T10:20:47.629 D main[1]:ch.k43.util.KLog:<clinit>:143                        OS disk space total 3.63 TiB, free 2.25 TiB, usable 2.25 TiB
2025-02-16T10:20:47.629 D main[1]:ch.k43.util.KLog:<clinit>:149                        Java version 23 (Java HotSpot(TM) 64-Bit Server VM - Oracle Corporation)
2025-02-16T10:20:47.629 D main[1]:ch.k43.util.KLog:<clinit>:154                        Java directory /Library/Java/JavaVirtualMachines/graalvm-jdk-23.0.1+11.1/Contents/Home
2025-02-16T10:20:47.629 D main[1]:ch.k43.util.KLog:<clinit>:159                        Java CPUs 10, de/CH, UTF-8, UTC +01:00 (Europe/Zurich)
2025-02-16T10:20:47.629 D main[1]:ch.k43.util.KLog:<clinit>:169                        Java heap maximum 16.00 GiB, current 1.01 GiB, used 8.66 MiB, free 1023.34 MiB
2025-02-16T10:20:47.630 D main[1]:ch.k43.util.KLog:<clinit>:176                        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-02-16T10:20:47.630 D main[1]:ch.k43.util.KLog:<clinit>:180                        User andybrunner, language de, directory /Users/andybrunner/
2025-02-16T10:20:47.630 D main[1]:ch.k43.util.KLog:<clinit>:186                        Current directory /Users/andybrunner/Documents/Eclipse-Workspace/ch.k43.util/src/
2025-02-16T10:20:47.630 D main[1]:ch.k43.util.KLog:<clinit>:190                        Temporary directory /var/folders/9s/tbyqn_vn7bs9rf3f1rc2jpxw0000gn/T/
2025-02-16T10:20:47.631 D main[1]:ch.k43.util.KLog:<clinit>:194                        KLog initialization complete (64 ms)
2025-02-16T10:20:47.631 D main[1]:ch.k43.util.KHTTPClient:xmit:303                     HTTP URL: https://reqbin.com/echo/get/json
2025-02-16T10:20:47.716 D main[1]:ch.k43.util.KHTTPClient:xmit:314                     HTTP method: GET
2025-02-16T10:20:47.716 D main[1]:ch.k43.util.KHTTPClient:xmit:324                     HTTP timeout: 5 sec
2025-02-16T10:20:47.722 D main[1]:ch.k43.util.KHTTPClient:xmit:372                     HTTP request header: Date: Sun, 16 Feb 2025 09:20:47 GMT
2025-02-16T10:20:47.723 D main[1]:ch.k43.util.KHTTPClient:xmit:372                     HTTP request header: User-Agent: ch.k43.util.KHTTPClient/2025.02.16
2025-02-16T10:20:47.723 D main[1]:ch.k43.util.KHTTPClient:xmit:372                     HTTP request header: Host: ab-macbook-pro
2025-02-16T10:20:47.723 D main[1]:ch.k43.util.KHTTPClient:xmit:372                     HTTP request header: Content-Length: 0
2025-02-16T10:20:47.850 D main[1]:ch.k43.util.KHTTPClient:xmit:392                     HTTP connection successful
2025-02-16T10:20:47.850 D main[1]:ch.k43.util.KHTTPClient:xmit:398                     HTTP connection: Direct (No proxy)
2025-02-16T10:20:47.850 D main[1]:ch.k43.util.KHTTPClient:xmit:404                     HTTP connection: Secured (TLS)
2025-02-16T10:20:47.850 D main[1]:ch.k43.util.KHTTPClient:xmit:405                     HTTP ciphers used: TLS_AES_256_GCM_SHA384
2025-02-16T10:20:47.851 D main[1]:ch.k43.util.KHTTPClient:xmit:410                     HTTP peer certificates: CN=reqbin.com
2025-02-16T10:20:47.851 D main[1]:ch.k43.util.KHTTPClient:xmit:410                     HTTP peer certificates: CN=WE1, O=Google Trust Services, C=US
2025-02-16T10:20:47.851 D main[1]:ch.k43.util.KHTTPClient:xmit:410                     HTTP peer certificates: CN=GTS Root R4, O=Google Trust Services LLC, C=US
2025-02-16T10:20:48.000 D main[1]:ch.k43.util.KHTTPClient:xmit:418                     HTTP response code: 200
2025-02-16T10:20:48.001 D main[1]:ch.k43.util.KHTTPClient:xmit:433                     HTTP response header: HTTP/1.1 200 OK
2025-02-16T10:20:48.001 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Server: cloudflare
2025-02-16T10:20:48.001 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: CF-RAY: 912c76bb8e32bc05-ZRH
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Access-Control-Allow-Origin: https://reqbin.com
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: x-frame-options: SAMEORIGIN
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Connection: keep-alive
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Last-Modified: Sun, 16 Feb 2025 09:07:42 GMT
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Date: Sun, 16 Feb 2025 09:20:48 GMT
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Accept-Ranges: bytes
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: CF-Cache-Status: HIT
2025-02-16T10:20:48.002 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
2025-02-16T10:20:48.003 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: x-content-type-options: nosniff
2025-02-16T10:20:48.003 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: x-xss-protection: 1; mode=block
2025-02-16T10:20:48.003 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v4?s=vT9oK84PxHNI5tqNZo7enHHbn52JdMsJHxzmY789KPeveyYHiHvHDZ5b7LWDzuV5cQRwgQGgGuJ6wX6lDQ88Sn4Al4akUwodUpnQ3usDW0DLylhCil%2Fh0LwJZIw%3D"}],"group":"cf-nel","max_age":604800}
2025-02-16T10:20:48.003 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: referrer-policy: same-origin
2025-02-16T10:20:48.003 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: server-timing: cfL4;desc="?proto=TCP&rtt=3877&min_rtt=3322&rtt_var=1642&sent=6&recv=7&lost=0&retrans=0&sent_bytes=3106&recv_bytes=807&delivery_rate=1307645&cwnd=228&unsent_bytes=0&cid=0cd4b05637bd7b69&ts=194&x=0"
2025-02-16T10:20:48.004 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Content-Length: 19
2025-02-16T10:20:48.004 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: alt-svc: h3=":443"; ma=86400
2025-02-16T10:20:48.004 D main[1]:ch.k43.util.KHTTPClient:xmit:436                     HTTP response header: Content-Type: application/json
2025-02-16T10:20:48.004 D main[1]:ch.k43.util.KHTTPClient:xmit:472                     HTTP data read (19 B)
2025-02-16T10:20:48.004 D main[1]:ch.k43.util.KHTTPClient:xmit:484                     HTTP response time: 373 ms

JDBC Example

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());
      }
   }
}
Output
{
  "ADDRESSES": [
    {
      "SEQUENCE": 1,
      "LASTNAME": "Smith",
      "FIRSTNAME": "Joe"
    },
    {
      "SEQUENCE": 2,
      "LASTNAME": "Miller",
      "FIRSTNAME": "Bob"
    },
    {
      "SEQUENCE": 3,
      "LASTNAME": "Johnson",
      "FIRSTNAME": "Evelyn"
    }
  ]
}
Debug Log
2025-02-16T14:24:26.342 D main[1]:ch.k43.util.KLog:<clinit>:121                        ===== Application started 2025-02-16T14:24:26.301 =====
2025-02-16T14:24:26.342 D main[1]:ch.k43.util.KLog:<clinit>:122                        Java Utility Package (Freeware) Version 2025.02.16
2025-02-16T14:24:26.342 D main[1]:ch.k43.util.KLog:<clinit>:123                        Homepage java-util.k43.ch - Please send any feedback to [email protected]
2025-02-16T14:24:26.343 D main[1]:ch.k43.util.KLog:<clinit>:126                        KLog properties read from file KLog.properties
2025-02-16T14:24:26.384 D main[1]:ch.k43.util.KLog:<clinit>:134                        Network host ab-macbook-pro (10.0.0.100)
2025-02-16T14:24:26.384 D main[1]:ch.k43.util.KLog:<clinit>:138                        OS platform Mac OS X Version 15.3.1/aarch64
2025-02-16T14:24:26.384 D main[1]:ch.k43.util.KLog:<clinit>:143                        OS disk space total 3.63 TiB, free 2.25 TiB, usable 2.25 TiB
2025-02-16T14:24:26.385 D main[1]:ch.k43.util.KLog:<clinit>:149                        Java version 23 (Java HotSpot(TM) 64-Bit Server VM - Oracle Corporation)
2025-02-16T14:24:26.385 D main[1]:ch.k43.util.KLog:<clinit>:154                        Java directory /Library/Java/JavaVirtualMachines/graalvm-jdk-23.0.1+11.1/Contents/Home
2025-02-16T14:24:26.385 D main[1]:ch.k43.util.KLog:<clinit>:159                        Java CPUs 10, de/CH, UTF-8, UTC +01:00 (Europe/Zurich)
2025-02-16T14:24:26.386 D main[1]:ch.k43.util.KLog:<clinit>:169                        Java heap maximum 16.00 GiB, current 1.01 GiB, used 9.94 MiB, free 1022.06 MiB
2025-02-16T14:24:26.386 D main[1]:ch.k43.util.KLog:<clinit>:176                        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-02-16T14:24:26.386 D main[1]:ch.k43.util.KLog:<clinit>:180                        User andybrunner, language de, directory /Users/andybrunner/
2025-02-16T14:24:26.386 D main[1]:ch.k43.util.KLog:<clinit>:186                        Current directory /Users/andybrunner/Documents/Eclipse-Workspace/ch.k43.util/src/
2025-02-16T14:24:26.387 D main[1]:ch.k43.util.KLog:<clinit>:190                        Temporary directory /var/folders/9s/tbyqn_vn7bs9rf3f1rc2jpxw0000gn/T/
2025-02-16T14:24:26.387 D main[1]:ch.k43.util.KLog:<clinit>:194                        KLog initialization complete (61 ms)
2025-02-16T14:24:26.387 D main[1]:ch.k43.util.KDB:<init>:139                           JDBC driver org.h2.Driver
2025-02-16T14:24:26.387 D main[1]:ch.k43.util.K:loadClass:1660                         Loading Java class org.h2.Driver
2025-02-16T14:24:26.389 D main[1]:ch.k43.util.KDB:<init>:150                           JDBC URL jdbc:h2:mem:mydb
2025-02-16T14:24:26.389 D main[1]:ch.k43.util.KDB:<init>:155                           JDBC connect as anonymous user
2025-02-16T14:24:26.499 D main[1]:ch.k43.util.KDB:<init>:161                           JDBC connection established (208 ms)
2025-02-16T14:24:26.500 D main[1]:ch.k43.util.KDB:exec:305                             SQL dynamic statement transaction started
2025-02-16T14:24:26.532 D main[1]:ch.k43.util.KDB:exec:324                             SQL dynamic statement transaction completed (33 ms, 0 rows)
2025-02-16T14:24:26.532 D main[1]:ch.k43.util.KDB:prepare:929                          SQL prepare statement started
2025-02-16T14:24:26.534 D main[1]:ch.k43.util.KDB:prepare:938                          SQL prepare statement completed (2 ms)
2025-02-16T14:24:26.535 D main[1]:ch.k43.util.KDB:execPrepare:379                      SQL prepared statement transaction started
2025-02-16T14:24:26.536 D main[1]:ch.k43.util.KDB:execPrepare:470                      SQL prepared statement transaction completed (1 ms, 1 rows)
2025-02-16T14:24:26.536 D main[1]:ch.k43.util.KDB:execPrepare:379                      SQL prepared statement transaction started
2025-02-16T14:24:26.537 D main[1]:ch.k43.util.KDB:execPrepare:470                      SQL prepared statement transaction completed (1 ms, 1 rows)
2025-02-16T14:24:26.537 D main[1]:ch.k43.util.KDB:execPrepare:379                      SQL prepared statement transaction started
2025-02-16T14:24:26.537 D main[1]:ch.k43.util.KDB:execPrepare:470                      SQL prepared statement transaction completed (0 ms, 1 rows)
2025-02-16T14:24:26.538 D main[1]:ch.k43.util.KDB:exec:305                             SQL dynamic statement transaction started
2025-02-16T14:24:26.548 D main[1]:ch.k43.util.KDB:exec:324                             SQL dynamic statement transaction completed (10 ms, 3 rows)
2025-02-16T14:24:26.549 D main[1]:ch.k43.util.KDB:getDataAsJSON:645                    JSON string generated from result set (288 B, 1 ms)

Prerequisites

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

Class
Library

KSMTPMailer, KLogSMTPHandler

Installation / Usage

  • Download 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

See the section Tips / FAQ on how to create a native binary executable with GraalVM.

Motivation for this toolkit

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).

This collection of Java classes was created in the course of various projects and will be further developed (see Design Goals). I hope that this tool will also serve you well.

Freeware / Open Source / Unlicensed

This software is freeware, open source and unlicensed. 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 😊

Last updated