在已编译的类中更改字符串常量 [英] Change string constant in a compiled class

查看:214
本文介绍了在已编译的类中更改字符串常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在已部署的Java程序中更改字符串常量,即编译的 .class -files中的值。它可以重新启动,但不容易重新编译(虽然如果这个问题没有答案,这是一个不方便的选择)。这可能吗?

I need to change a string constant in a deployed Java program, i.e. the value inside the compiled .class-files. It can be restarted, but not easily recompiled (though it's an inconvenient option if this question yields no answers). Is this possible?

更新:我只是用十六进制编辑器查看了文件,看起来我可以轻松地在那里更改字符串。这会起作用,即不会使文件的某种签名失效吗?旧字符串和新字符串都是字母数字,如果需要可以是相同的长度。

Update: I just looked at the file with a hex editor and it looks like I can easily change the string there. Would that work, i.e. won't that invalidate some kind of signature of the file? The old and new string are both alphanumeric, and can be the same length if needed.

更新2:我修复了它。因为我需要更改的特定类非常小并且在新版本的项目中没有更改,所以我可以编译它并从那里获取新类。出于教育目的,仍然对不涉及编译的答案感兴趣。

Update 2: I fixed it. Because the specific class I needed to change is very small and didn't change in the new version of the project, I could just compile that and take the new class from there. Still interested in an answer that doesn't involve compilation though, for educational purposes.

推荐答案

如果你有这个类的来源,然后我的方法是:

If you have the sources for this class, then my approach is:


  • 获取JAR文件

  • 获取单个类的来源

  • 在类路径上使用JAR编译源代码(这样,您不必编译任何其他内容; JAR已经包含二进制文件并不会造成损害)。您可以使用最新的Java版本;只需使用 -source -target 降级编译器。

  • 替换JAR中的类文件,新的使用 jar u 或Ant任务

  • Get the JAR file
  • Get the source for the single class
  • Compile the source with the JAR on the classpath (that way, you don't have to compile anything else; it doesn't hurt that the JAR already contains the binary). You can use the latest Java version for this; just downgrade the compiler using -source and -target.
  • Replace the class file in the JAR with the new one using jar u or an Ant task

Ant任务的示例:

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

这里我还更新清单。首先放置新类,然后添加原始JAR中的所有文件。 duplicate =preserve将确保不会覆盖新代码。

Here I also update the manifest. Put the new classes first and then add all the files from the original JAR. duplicate="preserve" will make sure that the new code will not be overwritten.

如果代码不是'如果新字符串与旧字符串的长度完全相同,您也可以尝试替换字节。 Java会对代码进行一些检查,但 .class文件中没有校验和

If the code isn't signed, you can also try to replace the bytes if the new string has the exact same length as the old one. Java does some checks on the code but there is no checksum in the .class files.

你必须保留长度;否则类加载器会混淆。

You must preserve the length; otherwise the class loader will get confused.

这篇关于在已编译的类中更改字符串常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆