🔨
Java Utility Package
🔨
Java Utility Package
  • Java Utility Package (Open Source)
  • Examples
    • 📖Logging
    • 🔧Utility Class
    • 🔑Password Vault
    • 🖥️HTTP / Socket Server
    • ⚒️HTTP Client
    • 🖥️Socket Client
    • 🗃️JDBC Database
    • ✉️SMTP Mailer
    • ⚔️Java Thread
    • 🗄️File Tools
    • ⌚Timer
    • 💡Tips / FAQ
  • Downloads
    • ⬇️Package ch.k43.util
    • ⬇️Sample Code
    • 📖JavaDoc API
    • 👨‍💻GitHub
Powered by GitBook
  1. Examples

Tips / FAQ

Last updated 19 days ago

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 toolkit 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 native executables by including the necessary Java reflection definitions within the JAR file.

Prerequisites

  • macOS:

  • Windows: or later

Compile / Create Executable / Sign / Run (macOS)

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

Example: GetCurrentVersion (macOS/arm64 executable)


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.

% 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

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

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

Convert PEM certificate to P12

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

Create JKS file from P12 certificate

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

Add Client Certificate to JKS file

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

Display Content of JKS file

% keytool -list -v -keystore keyfile.jks

Simple TLS Server

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

TLS Connection Test

% openssl s_client -connect hostname:443

See the on how to use JShell

GraalVM
Xcode
Visual Studio Build Tools 2022
GraalVM
Java Shell User Guide
💡
13MB
GetCurrentVersion.zip
archive
Page cover image