在jar文件中隐藏一些类文件或包 [英] hide some class file or package in jar file

查看:298
本文介绍了在jar文件中隐藏一些类文件或包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何导出不带我不希望其他人使用但不属于项目的类或程序包的jar,这意味着我无法删除它们.

How can I export a jar without some class or package which I don't want others to use,but which is needed in project,means I can't delete them.

推荐答案

这没有什么意义.

如果需要使用这些类来编译项目,则其他类中可能会存在与它们的静态依赖关系. (否则,您可以将其删除).

If the classes are needed to compile the project, then there are likely to be static dependencies on them in other classes. (Otherwise ... you would be able to just delete it / them.)

但是,如果这些类存在静态依赖关系,那么除非JAR文件中存在这些类,否则您将无法运行应用程序.如果您将类排除在JAR之外以阻止人们使用它们(或其他任何东西),则您的应用程序将在启动时因未找到类"异常而死亡.

But if there are static dependencies on those classes, then you won't be able to run the applications unless those classes are present in the JAR file. If you leave the classes out of the JAR to stop people using them (or whatever), your application will die on startup with "class not found" exceptions.

如果要阻止人们直接使用这些类,可以尝试以下操作:

If you want to stop people using the classes directly, you could try the following:

  • 将类访问权限更改为"package private".这并不是不可能使用它们,而是使其变得更加困难.

  • Change the classes access to "package private". This doesn't make it impossible to use them, but it makes it more difficult.

更改您的项目,以使对类的依赖关系完全是动态的.例如通过Class.forName(...)或依赖项注入.然后,您可以根据需要从JAR中排除这些类.但是再一次,如果您的应用程序需要使用这些类,则必须在运行时(以某种方式)可以访问它们,因此其他人有可能(以某种方式)获取它们.

Change your project so that the dependencies on the classes are entirely dynamic; e.g. via Class.forName(...) or dependency injection. Then you can exclude the classes from the JAR as required. But once again, if your application needs to use the classes, they have to be accessible at runtime (somehow), and it will therefore be possible (somehow) for other people to get hold of them.

更改您的项目以允许您完全删除这些类.这是唯一可以肯定的解决方案.

Change your project to allow you remove the classes entirely. This is the only sure-fire solution.

最后,在解决所有麻烦之前,您应该问自己为什么还要打扰.您为什么不让人们继续使用课程呢?是什么阻止他们从其他地方获取类……或从头开始实现自己的版本?

Finally, before you go to all of this trouble you should ask yourself why you are even bothering to do this. Why don't you just let people use the classes anyway? What is to stop them getting the classes from somewhere else ... or implementing their own versions from scratch?

这篇关于在jar文件中隐藏一些类文件或包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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