🔨
ch.k43.util
🔨
ch.k43.util
  • Java Utility Package (Freeware)
  • Examples
    • 📖Logging
    • 🔧Utility Class
    • ⚒️HTTP Client
    • 🖥️TCP Client
    • 🖥️TCP Server
    • 🗃️JDBC Database
    • ✉️SMTP Mailer
    • ⚔️Java Thread
    • 🗄️File Tools
    • ⌚Timer
  • Hints and Tips
    • 💡Tips / FAQ
    • 🪪TLS Certificates
  • Downloads
    • ⬇️Package ch.k43.util
    • ⬇️Jakarta/Angus Mail
    • ⬇️JDBC Database Drivers
    • ⬇️Sample Code
  • Documentation
    • JavaDoc Documentation
Powered by GitBook

Java Utility Package (Freeware)

A fast and easy to use programming toolkit for the Java backend developer

Follow the development at Mastodon #JavaUtil.

If you have any questions or feedback, feel free to contact me at andy.brunner@k43.ch

Features

  • Easy to use logging functions based on the standard Java Logger framework with additional logging handlers (JDBC and SMTP) and logging formatters (tabular text, CSV, XML, JSON, YAML)

  • Access any JDBC compliant database and retrieve data in various formats (JSON, YAML, CSV, XML, tabular or raw Java object format)

  • Create and send MIME multi part SMTP message with text, HTML and file attachment

  • Call HTTP/HTTPS services with support for all major HTTP methods

  • Start a TCP server (TLS or non-TLS) to handle client requests with user written class

  • Connect as a TCP client to a server (TLS or non-TLS) to read and write data

  • Convenient class to start and terminate Java threads

  • Read and write complete files (byte, character, Java Properties or JSON data)

  • Several utility functions as static methods

HTTP Example

KHTTPClient http = new KHTTPClient();
	      
if (!http.get("http://time.jsontest.com")) {
   KLog.abort("HTTP transaction failed - " + http.getLastError());
}

KFile.writeFile(http.getResponseDataAsString(), "test.json");
Output
{
   "date": "06-24-2024",
   "milliseconds_since_epoch": 1719229049829,
   "time": "11:37:29 AM"
}
Debug Log
2024-06-24T13:37:29.684 D main[1]:ch.k43.util.KLog:open:454                            ===== Application started 2024-06-24T13:37:29.650 =====
2024-06-24T13:37:29.685 D main[1]:ch.k43.util.KLog:open:456                            Java Utility Package (Freeware) ch.k43.util Version 2024.06.24
2024-06-24T13:37:29.685 D main[1]:ch.k43.util.KLog:open:459                            Please send any feedback to andy.brunner@k43.ch
2024-06-24T13:37:29.702 D main[1]:ch.k43.util.KLog:open:461                            Host ab-macbook-pro (10.0.0.105)
2024-06-24T13:37:29.703 D main[1]:ch.k43.util.KLog:open:462                            OS platform Mac OS X (14.5)
2024-06-24T13:37:29.703 D main[1]:ch.k43.util.KLog:open:463                            Java version 17 (OpenJDK 64-Bit Server VM - Eclipse Adoptium)
2024-06-24T13:37:29.705 D main[1]:ch.k43.util.KLog:open:467                            Java heap maximum 16.00 GB, current 1.00 GB, used 4.81 MB, free 1019.19 MB
2024-06-24T13:37:29.705 D main[1]:ch.k43.util.KLog:open:471                            Java locale de/CH, time UTC +02:00
2024-06-24T13:37:29.705 D main[1]:ch.k43.util.KLog:open:474                            Java classpath ../bin/:../lib/org.json.20230618.jar:../lib/jakarta-mail-1.6.7.jar:../lib/h2-2.2.224.jar:../lib/jakarta-activation-1.2.1.jar
2024-06-24T13:37:29.706 D main[1]:ch.k43.util.KHTTPClient:xmit:285                     HTTP URL: http://time.jsontest.com
2024-06-24T13:37:29.712 D main[1]:ch.k43.util.KHTTPClient:xmit:295                     HTTP method: GET
2024-06-24T13:37:29.720 D main[1]:ch.k43.util.KHTTPClient:xmit:352                     HTTP request header: Date: Mon, 24 Jun 2024 11:37:29 GMT
2024-06-24T13:37:29.720 D main[1]:ch.k43.util.KHTTPClient:xmit:352                     HTTP request header: User-Agent: ch.k43.util.KHTTPClient/2024.06.24
2024-06-24T13:37:29.720 D main[1]:ch.k43.util.KHTTPClient:xmit:352                     HTTP request header: Host: ab-macbook-pro
2024-06-24T13:37:29.721 D main[1]:ch.k43.util.KHTTPClient:xmit:352                     HTTP request header: Content-Length: 0
2024-06-24T13:37:29.745 D main[1]:ch.k43.util.KHTTPClient:xmit:372                     HTTP connection successful
2024-06-24T13:37:29.746 D main[1]:ch.k43.util.KHTTPClient:xmit:378                     HTTP connection: Direct (No proxy)
2024-06-24T13:37:29.746 D main[1]:ch.k43.util.KHTTPClient:xmit:393                     HTTP connection: Not secured (non-TLS)
2024-06-24T13:37:29.919 D main[1]:ch.k43.util.KHTTPClient:xmit:398                     HTTP response code: 200
2024-06-24T13:37:29.920 D main[1]:ch.k43.util.KHTTPClient:xmit:416                     HTTP response header: X-Cloud-Trace-Context: a8556d2ec2dec631bf3e096d24e40dc7
2024-06-24T13:37:29.921 D main[1]:ch.k43.util.KHTTPClient:xmit:413                     HTTP response header: HTTP/1.1 200 OK
2024-06-24T13:37:29.921 D main[1]:ch.k43.util.KHTTPClient:xmit:416                     HTTP response header: Server: Google Frontend
2024-06-24T13:37:29.922 D main[1]:ch.k43.util.KHTTPClient:xmit:416                     HTTP response header: Access-Control-Allow-Origin: *
2024-06-24T13:37:29.922 D main[1]:ch.k43.util.KHTTPClient:xmit:416                     HTTP response header: Content-Length: 100
2024-06-24T13:37:29.923 D main[1]:ch.k43.util.KHTTPClient:xmit:416                     HTTP response header: Date: Mon, 24 Jun 2024 11:37:29 GMT
2024-06-24T13:37:29.923 D main[1]:ch.k43.util.KHTTPClient:xmit:416                     HTTP response header: Content-Type: application/json
2024-06-24T13:37:29.924 D main[1]:ch.k43.util.KHTTPClient:xmit:452                     HTTP data read (100 bytes)
2024-06-24T13:37:29.925 D main[1]:ch.k43.util.KHTTPClient:xmit:464                     HTTP response time: 219 ms
2024-06-24T13:37:29.927 D main[1]:ch.k43.util.KFile:writeFile:372                      File test.json written (100 bytes)

JDBC Example

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": "John"
    },
    {
      "SEQUENCE": 2,
      "LASTNAME": "Miller",
      "FIRSTNAME": "Bob"
    },
    {
      "SEQUENCE": 3,
      "LASTNAME": "Johnson",
      "FIRSTNAME": "Eve"
    }
  ]
}
Debug Log
2024-10-28T08:29:55.391 D main[1]:ch.k43.util.KLog:open:441                            ===== Application started 2024-10-28T08:29:55.332 =====
2024-10-28T08:29:55.392 D main[1]:ch.k43.util.KLog:open:442                            Java Utility Package (Freeware) ch.k43.util Version 2024.10.26
2024-10-28T08:29:55.392 D main[1]:ch.k43.util.KLog:open:445                            Homepage java-util.k43.ch - Please send any feedback to andy.brunner@k43.ch
2024-10-28T08:29:55.413 D main[1]:ch.k43.util.KLog:open:448                            Host ab-macbook-pro (10.0.0.105)
2024-10-28T08:29:55.413 D main[1]:ch.k43.util.KLog:open:449                            OS platform Mac OS X (Version 15.0.1/aarch64)
2024-10-28T08:29:55.414 D main[1]:ch.k43.util.KLog:open:453                            OS disk space total 3.63 TB, free 2.36 TB, usable 2.36 TB
2024-10-28T08:29:55.414 D main[1]:ch.k43.util.KLog:open:456                            Java version 21 (OpenJDK 64-Bit Server VM - Eclipse Adoptium)
2024-10-28T08:29:55.414 D main[1]:ch.k43.util.KLog:open:459                            Java CPUs 10
2024-10-28T08:29:55.415 D main[1]:ch.k43.util.KLog:open:463                            Java heap maximum 16.00 GB, current 1.00 GB, used 7.71 MB, free 1016.29 MB
2024-10-28T08:29:55.415 D main[1]:ch.k43.util.KLog:open:467                            Java locale de/CH, time UTC +01:00 (Europe/Zurich)
2024-10-28T08:29:55.415 D main[1]:ch.k43.util.KLog:open:470                            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
2024-10-28T08:29:55.415 D main[1]:ch.k43.util.KDB:<init>:139                           JDBC driver org.h2.Driver
2024-10-28T08:29:55.416 D main[1]:ch.k43.util.K:loadClass:1265                         Loading Java class org.h2.Driver
2024-10-28T08:29:55.416 D main[1]:ch.k43.util.KDB:<init>:150                           JDBC URL jdbc:h2:mem:mydb
2024-10-28T08:29:55.416 D main[1]:ch.k43.util.KDB:<init>:155                           JDBC connect as anonymous user
2024-10-28T08:29:55.504 D main[1]:ch.k43.util.KDB:<init>:161                           JDBC connection established (187 ms)
2024-10-28T08:29:55.505 D main[1]:ch.k43.util.KDB:exec:305                             SQL dynamic statement transaction started
2024-10-28T08:29:55.553 D main[1]:ch.k43.util.KDB:processSQLResult:998                 SQL statement modified 0 rows
2024-10-28T08:29:55.553 D main[1]:ch.k43.util.KDB:exec:324                             SQL dynamic statement transaction completed (49 ms)
2024-10-28T08:29:55.553 D main[1]:ch.k43.util.KDB:prepare:920                          SQL prepare statement started
2024-10-28T08:29:55.555 D main[1]:ch.k43.util.KDB:prepare:929                          SQL prepare statement completed (2 ms)
2024-10-28T08:29:55.555 D main[1]:ch.k43.util.KDB:execPrepare:379                      SQL prepared statement transaction started
2024-10-28T08:29:55.556 D main[1]:ch.k43.util.KDB:processSQLResult:998                 SQL statement modified 1 rows
2024-10-28T08:29:55.556 D main[1]:ch.k43.util.KDB:execPrepare:470                      SQL prepared statement transaction completed (1 ms)
2024-10-28T08:29:55.556 D main[1]:ch.k43.util.KDB:execPrepare:379                      SQL prepared statement transaction started
2024-10-28T08:29:55.557 D main[1]:ch.k43.util.KDB:processSQLResult:998                 SQL statement modified 1 rows
2024-10-28T08:29:55.557 D main[1]:ch.k43.util.KDB:execPrepare:470                      SQL prepared statement transaction completed (1 ms)
2024-10-28T08:29:55.557 D main[1]:ch.k43.util.KDB:execPrepare:379                      SQL prepared statement transaction started
2024-10-28T08:29:55.557 D main[1]:ch.k43.util.KDB:processSQLResult:998                 SQL statement modified 1 rows
2024-10-28T08:29:55.557 D main[1]:ch.k43.util.KDB:execPrepare:470                      SQL prepared statement transaction completed (0 ms)
2024-10-28T08:29:55.557 D main[1]:ch.k43.util.KDB:exec:305                             SQL dynamic statement transaction started
2024-10-28T08:29:55.569 D main[1]:ch.k43.util.KDB:processSQLResult:1053                SQL fetched and processed 3 rows
2024-10-28T08:29:55.569 D main[1]:ch.k43.util.KDB:exec:324                             SQL dynamic statement transaction completed (12 ms)
2024-10-28T08:29:55.569 D main[1]:ch.k43.util.KDB:getDataAsJSON:646                    JSON string generated from result set (288 B, 0 ms)
2024-10-28T08:29:55.570 D main[1]:ch.k43.util.KDB:close:218                            JDBC connection closed

Prerequisites

  • Java SE 1.8 (Java 8) or higher

  • JDBC Driver (used only by the KDB class)

  • Java Mail API/Jakarta EE (used only by the KSMTPMailer class)

  • org.json.JSONObject (used only by the KFile class)

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

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. I hope that this tool will also serve you well.

Freeware / Unlicensed

This software is freeware 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 😊

Author

If you encounter any issue or if you have a suggestion, please let me know. You may contact me via my email address andy.brunner@k43.ch.

Credits

Foto by iMattSmart (unsplash.com)

Last updated 13 days ago

Page cover image