This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

preface

During development, there are always third-party dependency packages introduced in the project to do some real business by directly calling methods in jar packages. But sometimes jar package encapsulation method may have some discrepancy with our actual demand, need to modify the repackaged to use after business logic, but the jar package may not be like ordinary maven project to import the idea of a tool such as modify and then directly using the maven command to complete, a lot of times you need to install some decompiled tools to open, And cannot be directly modified in the decompiler tool. This article mainly aims at the file types in jar package, from two ways to achieve the replacement of files in JAR package and generation of class bytecode files.

background

Recently, according to business needs, the project needs to implement the function of file preview. As the kkFileView third-party JAR package is used, some services need to adjust themselves, and then use the newly generated JAR package for deployment. Combined with the actual project situation and according to the file type to be modified in the JAR package, this paper mainly introduces two implementation methods of file type: One is to replace non-Java type files in the JAR package, that is, files that do not need to generate class bytecode, such as files with the FTL suffix, or files that need to generate class bytecode, such as Entity, Service, controller, and so on. Without further ado, please refer to the operation procedure.

Operation process

1. Modify jar packages without generating bytecode files

Step 1: Find the JAR package to be modified, right-click and select the opening method, and then select the decompile plug-in JD-GUI (this application needs to be installed by yourself). After opening, find the corresponding FTL file, double-click to open it, and then CTRL + C to copy the contents of the file and paste it into the Notepad software. Modify the corresponding content and save it to the specified directory (note: the name should be consistent with the original file name)

Step 2: Find the jar package just now, right-click and choose the way to open it, then choose 7.zip or WinRAR to open it, then find the location of the modified file, and drag and drop the file in the first step to replace it

Step 3: Run the JAR package, the program calls the method in the JAR, and see the effect

Modify before:

Revised:

At this time, the official QQ group information in the corresponding JAR at the bottom of the preview error page has been deleted.

2. Modify the JAR package that generates the bytecode file

I now need to modify the file corresponding to the entity class to add a new attribute. What should I do?

The first step: Same as above, find the JAR package to be modified, right-click on the opening method, and then select the decompile plug-in JD-GUI (this application needs to be installed by yourself), open it, find the corresponding file, double-click to open it, then CTRL + C copy the contents of the file, and paste it into the Notepad software. After modifying the corresponding content, save the jar package to the same directory as the original file name (note: the name should be consistent with the original file name, and try to use the same directory to generate calss file by using the javac command). Here, it is only used for testing, and a new attribute can be added, as shown in the figure

The second step: Because of the dependencies of some files in the SpringBoot project, the package package may not be found when using the javac command to generate the bytecode file (for example, the cn.keking.config package does not exist), so here find any jar package in the project (for example, the filters-2.0.235.jar). Put it in the same directory as the jar package to be modified (such as kkFileView-3.6.0.jar), then use 7.zip to open the two jar packages and delete all files in the first jar package. Then use copy or drag to copy the files in the second JAR to the other jar, such as the CN directory, as shown in the figure

Step 3: Enter CMD above the directory where the JAR package is located, and press Enter to enter the command line mode, as shown in the figure

Step 4: Run the following command: The code is as follows

Javac - cp C: \ Users \ rongke \ Desktop \ test \ filters - 2.0.235 jar C: \ Users \ rongke \ Desktop \ test \ FileAttribute JavaCopy the code

As shown in figure

Press Enter to see that the class bytecode file has been generated and the contents have changed, as shown in the figure

Open kkFileView-3.6.0.jar using 7.zip, find the specified location of the bytecode file, and then drag and drop the new class file to replace the original class bytecode file.

Well, that’s all for today’s sharing. If it helps you, remember to give xiaobian a thumbs-up! If you have a better way to achieve, also can comment below, discuss together!