Preface
When deep customization cannot be achieved through source-level plug-ins or extension mechanisms, third-party JARs can be directly transformed. This guide covers two major parts:
-
Rewrite Java classes
Method A: Direct replacement
.class
Method B: Load the custom implementation using runtime classpath priority
Modify resource files (such as MyBatis XML)
Repackage JAR
With these three steps, you can have a complete JAR with custom Java behavior and tweaked mapped files.
1. Preparation
-
tool
JDK (including
javac
、jar
)ZIP tools: 7-Zip, WinRAR or command line
unzip
/zip
-
Project structure
Local working directory
workspace/
Original JAR:
workspace/lib/
Output directory:
workspace/out/
2. Open and extract JAR as ZIP
-
Open Archive
- Right-click in the file manager
→ 7-Zip → Open Archive
- Right-click in the file manager
-
Browse and Extract
Positioned to:
com/example/service/ org/mybatis/mapping/
- "Extract" them separately to
workspace/src_classes/
andworkspace/src_resources/
This step is only used to obtain the original file samples and paths, and subsequent transformations can be completed completely locally.
3. Rewrite Java classes
Method A: Directly replace the compiled .class
- Create the same package path locally
workspace/project/src/main/java/com/example/service/
- Write or paste the rewritten source code
// package ; public class MyService { @Override public String process(String input) { // Custom logic return "[Custom] " + (); } }
Compile and generate.class
javac -d workspace/out/classes \ workspace/project/src/main/java/com/example/service/
-
Overwrite the class in the original JAR with 7-Zip
Open , drag in
workspace/out/classes/com/example/service/
Confirm the replacement
-
Close archive, JAR already contains custom Java implementation
principle: When the JVM loads the class, it is directly read from the relative path in the JAR.
.class
, overwritten files will be used first.
Method B: Classpath is loaded first at runtime
-
Put the source code into the project
- The same package path and file as above: src/main/java/com/example/service/
-
Ensure the runtime classpath order
- Project output (including custom classes) must be before a third-party JAR
- For example, the command line starts:
java -cp target/classes:lib/
-
JVM loading is preferred
target/classes
class in, no changes are requiredadvantage: No need to modify the JAR, suitable for rapid coverage during development iteration or testing.
4. Modify MyBatis (or other) XML resources
MyBatis mapping files are usually.xml
Formal embedding of JAR cannot be passed through the Java mechanism Override and must be physically replaced:
-
Positioning XML
In 7-Zip, open the path:
org/mybatis/mapping/
- Extract and edit
"Extract" to
workspace/src_resources/org/mybatis/mapping/
Use an editor to modify SQL, namespace, etc., for example:
<select resultType=""> SELECT id, name, status FROM CUSTOM_ENTITY_TABLE WHERE id = #{id} </select>
-
Overwrite the modified XML back to the JAR
- Drag back to the 7-Zip window and confirm the replacement
Similarly:any
META-INF/
、Resources such as Spring XML can be replaced in this way.
5. Repackage JAR (command line)
If you prefer scripting, no GUI is required, you can use JDK to bring it.jar
Tool reconstruction:
- Prepare the output directory
workspace/out/classes/ ← Java .class workspace/out/resources/ ← XML、Configuration File
- Execute package
cd workspace/out jar cf \ -C classes . \ -C resources .
- Check content
jar tf
confirmcom/example/service/
andorg/mybatis/mapping/
All have been updated.
6. Verification and transformation
-
Replace the runtime library
- Always
lib/
Change toout/
- Always
-
Start the application and test it
Call the rewritten method to confirm that the Java logic takes effect
Perform an operation involving MyBatis to verify that the new SQL or map is correct
This is the article about how to modify the implementation method of source code in the jar package. For more related changes to the source code content in the jar package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!