如何使用Java API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4504209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 18:07:21  来源:igfitidea点击:

How to use Java API's?

javaapiclass

提问by ritch

Probably really simple but I just cannot work out how to use any API's with Java (Using Eclipse).

可能真的很简单,但我就是不知道如何在 Java 中使用任何 API(使用 Eclipse)。

Where do they go?

他们去哪里?

For example I want to use a Twitter APIand I import it using:

例如,我想使用 aTwitter API并使用以下方法导入它:

import net.unto.twitter.Api;

I then include the API file in the same dictionary as my class file. (This is what I do in python)

然后我将 API 文件包含在与我的类文件相同的字典中。(这就是我在python中所做的)

Anyone care to tell me the simple answer?

有人愿意告诉我简单的答案吗?

采纳答案by LINEMAN78

You need to include any external JARs in the build path. In eclipse right click on the project and go to 'Build Path' > 'Configure Build Path' then go to the 'Libraries' tab and 'Add JARs'. Also, when using eclipse it is easier not to maintain the import statements yourself, but instead use 'Organize Imports' (ctrl + shft + o) or Import the selected object (ctrl + shft + m). If a matching object is available in the classpath it will be imported.

您需要在构建路径中包含任何外部 JAR。在 Eclipse 中右键单击项目并转到“构建路径”>“配置构建路径”,然后转到“库”选项卡和“添加 JAR”。此外,在使用 eclipse 时,不自己维护导入语句更容易,而是使用“组织导入”(ctrl + shft + o)或导入所选对象(ctrl + shft + m)。如果在类路径中存在匹配的对象,它将被导入。

回答by hvgotcodes

Typically APIs are packaged in so-called JARs, which stands for Java ARchive. What you should do is:

API 通常打包在所谓的 JAR 中,JAR 代表 Java ARchive。你应该做的是:

  1. Download the jar.
  2. Put it in some sort of 'lib' directory in your project structure. Do not put it with your source code.
  3. Add the jar to your classpath. Some IDEs you have to add jar by jar, others you tell the IDE which directories are library directories and it will modify the classpath for you to include all the jars in the directory.
  4. Import the relevant package in your files where you use the API.
  1. 下载罐子。
  2. 将它放在项目结构中的某种“lib”目录中。不要把它和你的源代码放在一起。
  3. 将 jar 添加到您的类路径中。某些 IDE 必须逐个添加 jar,而其他 IDE 则告诉 IDE 哪些目录是库目录,它会为您修改类路径以包含目录中的所有 jar。
  4. 在使用 API 的文件中导入相关包。

Once you have the jar added to your classpath and import the relevant classes, Eclipse should automatically pick it up and allow you to use autocomplete features against the API.

将 jar 添加到类路径并导入相关类后,Eclipse 应该会自动选取它并允许您针对 API 使用自动完成功能。

It is also a good idea to get the source jar for any libraries you download. Most IDEs let you attach src jars, so you can click into the API and see how the code is written.

为您下载的任何库获取源 jar 也是一个好主意。大多数 IDE 允许您附加 src jar,因此您可以单击进入 API 并查看代码是如何编写的。

回答by kdabir

You should have it included in the classpath (build path) of the project.

您应该将它包含在项目的类路径(构建路径)中。

回答by DwB

Java APIs are distributed in JAR files (which are just zip files with a specifiec directory structure). Download the jar in question, put it in a known location on your disk (known location as in, pay attention to the name of the direcctory where you store the downloaded jar).

Java API 分布在 JAR 文件中(这些文件只是具有特定目录结构的 zip 文件)。下载有问题的 jar,将其放在磁盘上的已知位置(已知位置为 in,注意存储下载的 jar 的目录的名称)。

There are two times you will need to reference a Jar file:

有两次您需要引用 Jar 文件:

  • During project compilation.This includes development of the project using an IDE like eclipse (they all compile the code to find errors).
    1. Option 1,add an external jar:
      1. Open the project properties in Eclipse
      2. Select Java Build Path
      3. Select the Libraries tab
      4. Click the Add External Jar button
      5. Browse to the location of the JAR in question.
      6. Select the JAR.
      7. Click the Open button.
    2. Option 2,add the JAR to your web project's WEB-INF/lib directory.
  • During project execution.This will be after you deploy your application to a web container (perhaps tomcat). Store the JAR in question in the classpath of the appliction post deploy. For simple web applications, you can put the jar in the project WEB-INF/lib directory.
  • 在项目编译期间。这包括使用 Eclipse 之类的 IDE 开发项目(它们都编译代码以查找错误)。
    1. 选项1,添加一个外部jar:
      1. 在 Eclipse 中打开项目属性
      2. 选择 Java 构建路径
      3. 选择库选项卡
      4. 单击添加外部 Jar 按钮
      5. 浏览到相关 JAR 的位置。
      6. 选择 JAR。
      7. 单击打开按钮。
    2. 选项 2,将 JAR 添加到您的 Web 项目的 WEB-INF/lib 目录中。
  • 在项目执行期间。这将是在您将应用程序部署到 Web 容器(可能是 tomcat)之后。将有问题的 JAR 存储在部署后应用程序的类路径中。对于简单的web应用,可以将jar放在项目WEB-INF/lib目录下。