简体   繁体   中英

How can I replace a class file in jar files?

A coworker of mine wrote a class. His class uses my class. Rather than tell the JVM where my class lives every time his class is compiled, he packaged both classes into a jar file that I can run like so: java -jar hisclass.jar

Now I have decided to make changes to my class. Therefore, since my class was precompiled into his jar, his class (which I still need to use) uses my old class. My coworker left, and I don't know how he packaged everything into a jar file for me to use.

Now, when I try java -jar hisclass.jar , when his class calls my class, it calls an old version of my class which produces an error, as that old version of my class no longer works correctly. Could anyone help me recompile his class into a jar or just update the one I have?

In Eclipse i think it should be enough to export your code as jar file.

http://viralpatel.net/blogs/create-jar-file-in-java-eclipse/

So basically you get the old jar, extract, load as project in eclipse and export as jar file. And you're done.

  • for spcified class file
jar -uvf0 <YOUR_JAR_NAME>.jar io/some/package/service/TestService.class
  • for whole dir.
jar -uvf0 <YOUR_JAR_NAME>.jar io/some/package/service/

Im not sure if you can get away with it or not, but you might want to just try compiling your class to a jar file, then opening his jar file up in some compression agent like WinRAR, and simply overwriting the old jar file contained in his jar file with your newly compiled version. A better way (if he class isnt obfuscated) would be to open his jar file in a Java IDE, and create a new project from it. Then reference the the project you created from his jar file in your project in the same IDE. This way, you can make changes to both in the same environment, and compiling the code after changes will be easier.

You can extract your.jar file and change your compiled classes.

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