在jar文件中隐藏一些类文件或包

发布于 2024-10-05 23:21:04 字数 63 浏览 0 评论 0原文

如何导出没有某些类或包的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白首有我共你 2024-10-12 23:21:05

这没有多大意义。

如果需要这些类来编译项目,那么其他类中可能存在对它们的静态依赖关系。 (否则......您将能够删除它/它们。)

但是,如果这些类存在静态依赖关系,那么您将无法运行应用程序,除非这些类存在于 JAR 文件中。如果您将这些类保留在 JAR 之外以阻止人们使用它们(或其他方式),您的应用程序将在启动时因“找不到类”异常而终止。

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

  • 将类访问权限更改为“package private”。这并不意味着无法使用它们,但会使其变得更加困难。

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

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

最后,在遇到所有这些麻烦之前,您应该问问自己为什么要费心这样做。无论如何,你为什么不让人们使用这些课程呢?什么可以阻止他们从其他地方获取类......或从头开始实现自己的版本?

This doesn't make a lot of sense.

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

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:

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

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

流年里的时光 2024-10-12 23:21:05

如果您的程序需要该类才能运行,则不能从 JAR 中省略它。

您可以将类放入名称中带有“internal”之类内容的包中(例如com.example.internal),以表明这些类不适合其他人使用或依赖。它不能阻止任何人出于自己的目的使用您的类,但它至少表明他们这样做需要自己承担风险,您的内部接口可能会在程序的未来版本中发生变化,等等。

If the class is needed for your program to work, you can't omit it from the JAR.

You can put classes in a package that has something like "internal" in its name — e.g. com.example.internal — to indicate that the classes aren't meant for others to use or rely on. It can't prevent anyone from using your class for their own purposes, but it at least indicates that they do so at their own risk, that your internal interfaces might change in future versions of the program, etc.

屌丝范 2024-10-12 23:21:05

一种简单的方法是对类使用 package private 访问修饰符。这将使其他人难以访问这些类,但也会影响您从其他包中使用它们的能力。

更高级的方法是使用 OSGi 并仅发布那些您希望其他人使用的类。这将使其他人无法访问这些类,同时又不限制您对它们的访问。

A simple way is to use the package private access modifier for classes. That will make it difficult for others to access the classes, but it will also affect your ability to use them from other packages.

A more advanced way would be to use OSGi and only publish those classes you want others to use. That will make it impossible for others to access the classes while not restricting your access to them.

梦醒时光 2024-10-12 23:21:05

将它们放入单独的 jar 文件中,将该 jar 文件(作为单个文件,可能具有不起眼的名称)包含到您的 jar 文件中,创建自定义类加载器以从代码中打开该嵌入的 jar 文件,使用反射引导入口点(因为你不能对它有静态依赖)进入新的类加载器。

或者只是接受这样一个事实:由于代码就在某个地方,任何程度的混淆都会给每个人带来不便,并且不能真正隐藏任何东西。

Put them into a separate jar file, include that jar file (as a single file, maybe with a nondescript name) into your jar file, create a custom class loader to open that embedded jar file from your code, bootstrap an entry point with reflection (because you cannot have static dependencies on it) into that new class loader .

Or just accept the fact that since the code is in there somewhere, any amount of obfuscation is just an inconvenience to everyone and cannot really hide anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文