Java – change string constants in compiled classes
I need to change the string constants in the deployed Java program, that is, compiled Class file It can be restarted, but it is not easy to recompile (although this is an inconvenient option if this question does not produce any answer) Is that possible?
Update: I just use the hex editor to view the file. It looks like I can easily change the string Does that work not invalidate some kind of signature on the document? The old and new strings are alphanumeric and can be the same length if necessary
Update 2: I fixed it because the specific class I need to change is very small and has not changed in the new version of the project. I can compile it and take out new classes from there Still interested in answers that do not involve compilation, but for educational purposes
Solution
If you have the source of this class, my approach is:
>Get jar files > get the source code of a single class > compile the source code using jar in the classpath (so you don't have to compile anything else; it won't hurt that jar already contains binaries) You can use the latest java version for this; Just downgrade the compiler with - source and - target. > Use the jar u or ant task to replace the class file in the jar with a new jar
Ant task example:
<jar destfile="${jar}" compress="true" update="true" duplicate="preserve" index="true" manifest="tmp/Meta-INF/MANIFEST.MF" > <fileset dir="build/classes"> <filter /> </fileset> <zipfileset src="${origJar}"> <exclude name="Meta-INF/*"/> </zipfileset> </jar>
I also update the list here First put in the new class, and then add all the files in the original jar Duplicate = "preserve" will ensure that the new code is not overwritten
If the code is not signed, you can also try to replace bytes if the length of the new string is exactly the same as that of the old character Java makes some checks on the code, but there is no check sum in the class files.
You must keep the length; Otherwise, the class loader will become confused