Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to replace single class file in a jar file?

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

like image 942
GC_ Avatar asked Dec 21 '22 23:12

GC_


2 Answers

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).

like image 57
lrAndroid Avatar answered Jan 11 '23 23:01

lrAndroid


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.

like image 28
neo Avatar answered Jan 11 '23 22:01

neo