Is it safe to replace a single class file in a jar file?
Do the class files reference each other in such a way that this is not safe?
Grae
Yes, you can run jar uf mJarFile.jar com\example\mClass.class
.
However, be aware that if the class defined constant static final
primitive or String variables, then those values will have been in-lined into other classes (and so the values contained in your newer class won't be used).
jar command (provided in Sun JDK bin) to replace class.
However, you need to understand following.
jar uf myJar com\test\MyClass.class
Here your class resides in com.test package
If you class has any sub classes, then generally those classes are also created as MyClass$MySubclass.class
Apart from MyClass.class you all need to replace/add those sub classes.class. such as
jar uf myJar com\test\MyClass$MySubclass.class
It is also important to understand if your changes involved in any method prototype change, you must re-compile those dependent classes and use it.
Above all, if your jar is signed, you must resign your jar. Because signature will be lost if you change anything in jar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With