替换java类? [英] Replacing java class?

查看:166
本文介绍了替换java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的java防病毒工作沙箱功能,我遇到了一个问题:一个类上的指定包对于编译是否重要?

I'm working on a sandbox feature for my java antivirus, and I've come into a question: Does the specified package on a class matter for compilation?

示例:
我正在运行一个程序,想要使用 Runtime.getRuntime()。exec(),当类加载器尝试加载它来运行一个方法,是否检查文件中包含的包,是否存在?我宁愿不尝试更改JVM中的文件,而只是从不同的包中加载一些文件。我可以完成装载等等,但是我唯一的困境,它会崩溃和燃烧吗?在java中,它将被注册为 java.lang.Runtime ,但编译后的代码会说例如 pkg.pkg.Runtime 并且它是否需要扩展旧的运行时?我的猜测是扩展旧的运行时只会破坏它。有人对这个有了解吗?我正在努力制作一个可测试的例子,但我仍然有点想要得到一些答案,这可能会让一些人受益。

Example: I'm running a program that wants to use Runtime.getRuntime().exec(), when the classloader attempts to load that to run a method, does it check the package qualified in the file, if they exist? I would prefer not to try and change files in the JVM, but to simply load ones from a different package. I can accomplish the loading and such, but my only dilemma, will it crash and burn? Inside the java, it would be registered as say, java.lang.Runtime, but the compiled code will say for example pkg.pkg.Runtime and will it need to extend the old runtime? My guess is that extending the old runtime would just break it. Does anyone know anything about this? I'm working on making a testable example, but I'm still a bit away and wanted to get some answers, as well as this might benefit some people.

推荐答案


类上指定的包是否适用于编译?

Does the specified package on a class matter for compilation?

是的,这很重要。无法加载名为 pkg.pkg.Runtime()的类,就好像 java.lang.Runtime

Yes it does matter. A class called pkg.pkg.Runtime() cannot be loaded as if it was java.lang.Runtime.

此外,如果我的内存是正确的,JVM还有一些额外的安全措施,以防止普通应用程序将类注入核心包,如 java.lang

Furthermore, if my memory is correct, the JVM has some additional security measures in it to prevent normal applications from injecting classes into core packages such as java.lang.

如果需要更改 java.lang的行为.Runtime class(用于实验目的!)然后我认为您需要将修改后的版本放在引导类路径上,而不是rt.jar文件。

If you need to change the behaviour of the java.lang.Runtime class (for experimental purposes!) then I think you will need to put your modified version on the boot classpath, ahead of the "rt.jar" file.

然而:


  • 这种程度的修补很容易导致JVM不稳定;即难以诊断的硬JVM崩溃。

  • This level of tinkering can easily result in JVM instability; i.e. hard JVM crashes that are difficult to diagnose.

如果您的目标是生产生产质量工具,那么您会发现涉及修补的事情使用JVM不被认为是可以接受的。人们会非常怀疑安装说明,例如将其添加到已安装的JVM的bootclasspath。

If your aim is to produce a "production quality" tool, then you will find that things that involve tinkering with the JVM are not considered acceptable. People are going to be very suspicious of installation instructions that say things like "add this to your installed JVM's bootclasspath".

分发修补的JVM可能会违反Oracle的Java许可协议。

Distributing a "tinkered with" JVM may fall foul of Oracle's Java licensing agreement.

我的建议是寻找一种不那么干扰的方式来做你想做的事情。例如,如果您尝试进行病毒检查,请在JVM之外或自定义应用程序类加载器中进行。

My advice would be to look for a less intrusive way of doing what you are trying to do. For instance, if you are trying to do virus checking, either do it outside of the JVM, or in a custom application classloader.

你注释了:


我有一个自定义类加载器,我的问题是:如果我编译一个标记为say的类,pkg .pkg.Runtime,我可以在我的类加载器中注册为java.lang.Runtime吗?

I have a custom classloader, my question is: If I compile a class that is labelled as say, pkg.pkg.Runtime, can I register in my classloader as java.lang.Runtime?

正如我上面所说,不,你不能。字节码文件中嵌入了类名。如果你试图通过加载一个具有不同名称的类来拉动一个swifty,JVM将抛出一个错误

As I said above, no you can't. A bytecode file has the classname embedded in it. If you attempt to "pull a swifty" by loading a class with a different name, the JVM will throw an Error.

并且:


如果没有,那么我该如何替换这个类?如果编译的包名必须等于引用命名的请求,那么我可以修改.class文件以匹配,或者可能将其编译为就像它在java.lang包中一样吗?

If not, then how can I replace the class? If the compiled package name has to equal the request referenced naming, then can I modify the .class file to to match, or perhaps compile it as if it were in the java.lang package?

这就是你必须要做的。您需要在源代码中命名 java.lang.Runtime 并进行编译。

That's what you would have to do. You need to name the class java.lang.Runtime in the source code and compile it as such.

但根据我的建议,我的意思是你应该在类加载器中使用病毒检查。 忘记尝试替换/修改 Runtime 的行为。由于我上面列出的原因,这是一个坏主意。

But what I meant by my advice above is that you should use do the virus checking in the class loader. Forget about trying to replace / modify the behaviour of Runtime. It is a bad idea for the reasons I listed above.

这篇关于替换java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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