我可以在运行时修改Java方法的字节码吗? [英] Can I modify the byte code of a Java method in the runtime?

查看:621
本文介绍了我可以在运行时修改Java方法的字节码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写另一个大型Java程序的插件。

I am writing a plugin of another large java program .

我想在运行时修改Java程序的某些Java方法的某些字节代码,以便可以拦截方法调用(即,向方法中注入一些挂钩代码)。

I want to modify some byte code of some java method of the java program during runtime, so that I can intercept the method calls (namely, inject some hooking code into the method).

有什么方法可以实现这一目标?

Any way can achieve this?

PS:

我已经检查了以下方法:

I've checked the following approaches:

1。更改java程序的类加载器。 (我们无法更改)。

2.使用Java代理。 (我们不能使用Java代理,因为Java代理会创建一个新的代理对象。我们不使用代理对象。我们需要钩住Java程序的对象,并使用该对象)。

3. use -javaagent选项(我们无法为Java程序添加命令行选项。)

1.change the classloader of the java program. (we CANNOT change it)
2.use java proxy. (We CANNOT use java proxy, because java proxy would create a new proxy object. We DON'T use the proxy object. We need to hook the java program's object, and Use that object)
3. use -javaagent option ( we CANNOT add the commandline option for the java program.)

PS更多 [再次编辑]

我的类由ext类加载器加载(我将jar文件放在JAVA_HOME\lib\ext文件夹中)。
大型Java程序是浏览器加载的applet程序。当浏览器启动小程序时,它也加载了我的类。

PS more :
My classes was loaded by ext class loader (I put my jar files in JAVA_HOME\lib\ext folder). The large java program is an applet program loaded by Browser. When the browser start the applet, it also loads my classes.

PS更多, [再次编辑]

尽管它在Applet中运行。我可以有完全的权限。因为我可以修改java.policy和java.security文件。

PS more more :
Although it's running in Applet. I can have full permission. Because I can modify java.policy and java.security file.

感谢,

卡尔文

Thanks,
Calvin

推荐答案

只需使用-javaagent opiton,它用于在运行时修改字节码。您可以从此链接或从此链接

Just use -javaagent opiton, which is used to modify the bytecode at runtime. You can find more about -javaagent from This Link or from This Link

这篇关于我可以在运行时修改Java方法的字节码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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