SoFunction
Updated on 2025-05-07

How to modify the implementation method of source code in jar package

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:

  1. Rewrite Java classes

    • Method A: Direct replacement.class

    • Method B: Load the custom implementation using runtime classpath priority

  2. Modify resource files (such as MyBatis XML)

  3. Repackage JAR

With these three steps, you can have a complete JAR with custom Java behavior and tweaked mapped files.

1. Preparation

  • tool

    • JDK (includingjavacjar

    • ZIP tools: 7-Zip, WinRAR or command lineunzip/zip

  • Project structure

    • Local working directoryworkspace/

    • Original JAR:workspace/lib/

    • Output directory:workspace/out/

2. Open and extract JAR as ZIP

  1. Open Archive

    • Right-click in the file manager → 7-Zip → Open Archive
  2. Browse and Extract

    • Positioned to:

com/example/service/  
org/mybatis/mapping/
  • "Extract" them separately toworkspace/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/
  1. Overwrite the class in the original JAR with 7-Zip

    • Open , drag inworkspace/out/classes/com/example/service/

    • Confirm the replacement

  2. 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 preferredtarget/classesclass in, no changes are required

    advantage: 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.xmlFormal 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" toworkspace/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:anyMETA-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.jarTool 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

    • Alwayslib/Change toout/
  • 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!