Running Java Programs from the Command Line by Adding Third-Party Jars to the Classpath
When running a Java program directly from the command line, if you need to include third-party jar packages, the common approach is to use Maven/Gradle to build a fat jar (bundling all dependencies). However, in lightweight scenarios — such as writing a small script, doing quick prototype validation, or temporarily running Java code in a CI environment — manually setting the classpath is often more straightforward. What Is Classpath?Classpath is the search path the JVM uses to locate c...
Fixing the SecurityException: Invalid Signature File Digest for Manifest Main Attributes in Fat Jars
The ProblemRecently I encountered an error when trying to run a fat jar: 1234567891011121314151617181920Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:287) at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:240) at java.util.jar.JarVerifier.processEntry(JarVerifier.java:317) at java.util.jar.JarVerifier.updat...
Understanding Redis Cluster Through Practice
Redis Cluster is an official clustering solution provided by Redis. It has been available as a stable release since version 3.0, is widely adopted, and has stood the test of time. Personally, I believe it can fully replace other clustering solutions like Codis and Twemproxy. Cluster PrinciplesCluster implements data sharding. A Redis Cluster contains 16,384 hash slots. Any key can be mapped to a specific slot using the formula CRC16(key) % 16384. Which slot belongs to which node is predetermi...
An Approach to Storing Sets in HBase
HBase’s storage model is a simple key-value store (rowkey → column family → qualifier → value). Unlike Redis, it doesn’t natively support data structures like Set, List, or Hash. Yet in real-world applications, we often need to store collection-type data — user tag sets, product attribute sets, friend lists in social graphs, to name a few. How can we elegantly implement a Set in HBase? This article introduces an efficient approach that leverages HBase’s column (qualifier) characteristics. R...
Log4j2 XML Configuration Examples and Differences from Log4j
Complete Configuration Example1234567891011121314151617181920212223242526272829303132333435<?xml version="1.0" encoding="UTF-8" ?><configuration status="warn"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="[%p] %d %c %l - %m%n"/> </Console> <RollingFile name="activity" fileName="/opt/fox.log" ...
Comparing Java Lists Without Implementing equals
Problem StatementIn Java, there are several ways to compare whether two Lists have the same values regardless of order, such as sorting followed by using equals, or executing containsAll in both directions, etc. All of these methods require us to implement the equals and hashCode methods for the element classes in the list. However, there are special cases where it’s not convenient for us to implement the equals method of a class — for example, when it comes from an ancient third-party JAR pa...
Implementing Redis hmsetnx Command Using Lua Scripts and Jedis
In Redis’s Hash operations, HSETNX ensures that a field is only written when it does not already exist. However, HMSET (which sets multiple fields in batch) has no corresponding HMSETNX command. If you need to batch-initialize a cache without overwriting existing data, issuing N HSETNX commands would require N network round-trips — terrible performance in batch scenarios. This can be solved elegantly with a Lua script — pushing the logic to the Redis server side and requiring only a single ne...
Embedding Hawtio Monitoring in a Standalone Java Application
Hawtio (hawt.io) is an open-source monitoring system that provides multiple startup methods. It can run as a standalone JAR or WAR package and remotely connect to other applications for monitoring, or it can be directly embedded into our own applications. This article introduces how to embed Hawtio in a standalone Java application, corresponding to the official documentation (http://hawt.io/getstarted/index.html) section “Using hawtio inside a stand alone Java application”. However, that se...
Java Serialization Performance Comparison: kryo/protobuf/protostuff vs Json
Why Compare Serialization SolutionsFor serializing a Java object, I wanted to test the differences in time and space performance between using JSON and using general-purpose serialization tools. For JSON, I chose fastjson. For serialization tools, I used protostuff and kryo. Why not protobuf? Because for an existing Java class with hundreds of properties, creating a matching proto file feels somewhat inhumane. Protostuff is an improved version of protobuf — it can directly serialize a Java ...
Java Distributed Lock Practice for Beginners
This article covers usage only, no theory. Simple and straightforward. A distributed lock is, as the name suggests, a lock for distributed systems, used in scenarios where multiple instances need coordination. For example, if a service is deployed on multiple machines and may operate on the same record in a database, a distributed lock becomes necessary. There are many ways to implement distributed locks. Generally, a lock requires an entity to represent it — when acquiring the lock, this ent...













