简体   繁体   中英

How do i patch a jar with another jar via the classpath (replacing classfiles)

I have two jarfiles, one is the minecraft server spigot.jar , the other is my own jar ( patch.jar ) with several small custom classes in it.

The classes in patch.jar need to replace classes in spigot.jar , i have read numerous "tutorials" and questions about doing this, but none of them provide even basic information on how to actually do it, aside from using the classpath option in the startup script.

I have tried:

  • - cp Spigot.jar Patch.jar : throws error "no main manifest" for patch, as it has no Main().
  • - cp Spigot.jar; Patch.jar : outputs a list of jvm -options and closes the program.
  • - jar Spigot.jar; Patch.jar : cannot find jar Spigot.jar;Patch.jar (i know, it was a long shot)

I made a public void main() and Main-Class in Patch.jar manifest, and it ran the main() properly, then exited without loading spigot. If i switched the order to load spigot first, it loads normally and never touches my patch.

Does anyone know how to do this, or something similar? My requirement is that NO MODIFICATIONS are made to the spigot jar, for legal and technical reasons. I have already manually replaced the classes inside a copy of spigot.jar to verify they work as intended, there is no issue with my code.

PS This cannot be made part of a plugin, it is a mod for a reason, and is part of supporting a very large plugin that changes dozens of NMS classes at runtime. This is for patching a few things that cannot be modified at runtime with reflection or simply changing variable classes in a rational way.

First, if you want Java to load your classes in preference to the original ones, the patch.jar file must come before the original jar file in the classpath.

And you must use the correct command line:

java -cp patch.jar;spigot.jar the.main.class.of.Spigot

To know what the main class is, open spigot.jar, anc check its manifest file: the main class is listed in it.

It's also possible for the file to list other jar files as dependencies. In that case, you'll have to add them to the classpath as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM