外部图书馆如何运作 [英] How external libraries work

查看:142
本文介绍了外部图书馆如何运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解外部库如何在java中工作。在任何地方我看都不清楚。

I'm just to get my head around how external libraries work in java. Everywhere I look things are unclear.

通常,我理解使用外部库涉及设置所需类,JAR等的类路径。

Normally, i understand using a external library involves settings a classpath to the desired class, JAR, etc.

我的问题是(如果我将项目构建到JAR中)是否可以访问该库,如果它是在另一台计算机上使用的话?外部JAR会包含在MY JAR文件中吗?否则,它将如何访问外部JAR?

My question is how (if I were to build my project into a JAR) would it be able to access the library if it were, to say, be used on another computer? Would the external JAR be included in MY JAR file? Otherwise, how would it be able to access the external JAR?

示例将是如何使用bukkit插件,插件如何知道在哪里引用bukkit.jar文件?

Example would be how this works with bukkit plugins, how does the plugin know where to reference the bukkit.jar file?

推荐答案

每个jar都有一个清单文件。在那里你可以参考罐子外面的其他罐子。这些将包含在您的类路径中。 您可以在此处查看如何执行此操作。作为示例,你可以把它放到你的清单文件中:

Every jar has a manifest file. In there you can refer to other jars outside the jar. These will be included in your classpath. You can see how to do that here. As an example, you can put this into your manifest file:

Class-Path: jar1-name jar2-name directory-name/jar3-name

正如你在这里看到的,它使用了相对路径。如果它们是绝对路径,它们会说类似 C:\ ... \ _jar1-name 。使用相对路径意味着,只要罐子停留在主罐子的相同位置 relative ,这段代码就能正常运行。

As you can see here, it uses relative paths. If they were absolute paths, they would say something like C:\...\jar1-name. Using relative paths means, as long as the jars stay in this same place relative to the main jar, this code will run correctly.

或者,您可以提供运行 java 命令的脚本,并在类路径中包含您需要的所有jar文件。

Alternatively, you can provide a script that runs a java command and includes all the jars you need on the classpath.

另一种选择,您可以使用名为 uberjar 将所有这些东西放在一个罐子里然后你可以分发它。

And for another alternative, you can use something called uberjar which will put all these things together into one jar and then you can just distribute that.

另一种选择,您可以使用 jar类加载器将从嵌入式jar中加载类。

And for another alternative, you can use a jar classloader that will load classes from embedded jars.

出于教育的考虑,你可能想看看我的回答描述了很多分发java应用程序的方法。

For education's sake, you might want to check out my answer here that describes a lot of ways to distribute java applications.

这篇关于外部图书馆如何运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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