简体   繁体   English

在 Eclipse 中添加 external.jar 文件

[英]Adding external .jar file in Eclipse

I'm having trouble adding a.jar file I downloaded for my Java project.我在添加为我的 Java 项目下载的文件时遇到问题。 This is really the first time I've used eclipse, so please bear with me and for some reason (I have no clue why), I just find it somewhat confusing.这真的是我第一次使用 eclipse,所以请多多包涵,出于某种原因(我不知道为什么),我只是觉得有点混乱。

I know that in order reference different class files you simply need to create a class library and add it to the build path.我知道为了引用不同的 class 文件,您只需创建一个 class 库并将其添加到构建路径中。 From there, all which needs to be done (unless I'm misunderstanding this for whatever reason) is use the "import" keyword to import whatever.jar, .java, or.class/.interface file necessary into the project.从那里,所有需要做的事情(除非我出于某种原因误解了这一点)是使用“import”关键字将任何东西导入到项目中。jar、.java 或 .class/.interface 文件。

I've tried that with my.jar.我已经用 my.jar 试过了。 I have it referenced in the build path (all I did was just copy the jar to the project directory, and then use the build path option to add it externally), but when ever try to call the object "Delegator", which obviously is a part of the.jar file, it won't read.我在构建路径中引用了它(我所做的只是将 jar 复制到项目目录,然后使用构建路径选项将其添加到外部),但是当尝试调用 object “委托人”时,这显然是.jar 文件的一部分,它不会读取。

Am I missing something here?我在这里错过了什么吗? Seriously, anyone who knows the answer to this - you're relieving a mother of a headache.说真的,任何知道这个答案的人 - 你正在缓解一个头痛的母亲。 And before anyone asks - yes, I've searched this one to death.在任何人问之前 - 是的,我已经把这个找死了。 I've found similar questions, but nothing which quite hit what I was looking for.我发现了类似的问题,但没有什么能完全符合我的要求。 Either that, or I really just lack the common sense.要么,要么我真的缺乏常识。

Right click on project->BuildPath->Libraries->Addexternaljar and then press ok and if it doesnot worked then you should go to the右键单击项目->BuildPath->Libraries->Addexternaljar ,然后按确定,如果它不起作用,那么你应该 go 到

Order and Export  tab
and checked the jar you have just added in your project. 并检查了您刚刚添加到项目中的 jar。 It will solved your problem. 它会解决你的问题。

There are several possible reasons, for the question hasn't mentioned the specific failure, and where it has occurred.有几个可能的原因,因为问题没有提到具体的失败,以及它发生在哪里。 The following is a list of possible reasons I could think of, but this may not be exhaustive:以下是我能想到的可能原因列表,但这可能并不详尽:

  • You can import a class, in a different package only if the class is public.仅当 class 为公共时,您才能在不同的 package 中导入 class。 The only exception is when you are using the class in the same package.唯一的例外是当您在同一个 package 中使用 class 时。 If the class is an inner class marked as private, then you're well and truly out of luck.如果 class 是标记为私有的内部 class,那么您真的很不走运。 The Delegator class in question might not be public, and that's why you may be unable to use it.有问题的委托人 class 可能不公开,这就是您可能无法使用它的原因。 This issue ought to be caught by the compiler.这个问题应该被编译器捕获。
  • The directory structure within the JAR might not match your package import statements in your classes. JAR 中的目录结构可能与您的类中的 package 导入语句不匹配。 This might not be necessary, for Eclipse ought to provide possible fixes, but it is better to verify that nevertheless.这可能不是必需的,因为 Eclipse 应该提供可能的修复,但最好还是验证一下。 Again, the compiler should complain if this is the case.同样,如果是这种情况,编译器应该抱怨。
  • If the issue is at runtime, then, it is most likely that the JAR is not available in the runtime classpath.如果问题出现在运行时,则很可能 JAR 在运行时类路径中不可用。 You'll need to configure the Runtime configuration, to add the JAR to the runtime classpath.您需要配置运行时配置,将 JAR 添加到运行时类路径。 Refer to the Eclipse documentation on run configurations , if you need to know how to change the runtime classpath.如果您需要了解如何更改运行时类路径,请参阅有关运行配置的 Eclipse 文档

Note:笔记:

  1. Exporting the build classpath entries would matter to other projects that depend on the pertinent project;导出构建类路径条目对依赖相关项目的其他项目很重要; unexported entries will have to be re-imported if required in other projects.如果其他项目需要,则必须重新导入未导出的条目。 This would not apply to a run configuration.这不适用于运行配置。

Update更新

Every Java application needs a main(String[] args] method to start execution. This is the entrypoint for the application. From the comment, it appears that the main method is in a different class. If so, the said class ought to be used to start the application. In Eclipse, a "Run configuration" might be used for the class that lacks this entrypoint, resulting in the described error. One can rectify this by creating a new Run configuration for the class with the said entrypoint. This may be done by one of the following:每个 Java 应用程序都需要一个main(String[] args]方法来开始执行。这是应用程序的入口点。从评论中,似乎 main 方法位于不同的 class 中。如果是这样,则所述 ZA2F2ED4F298EBC2CBB1DZC2 应该是用于启动应用程序。在 Eclipse 中,“运行配置”可能用于缺少此入口点的 class,导致所描述的错误。可以通过为 ZA2F2ED4F8EBC2CBBDZC21A2 创建新的运行配置来纠正此问题。可以通过以下方式之一完成:

  • editing the existing Run configuration to use the desired Class (the one with the main method).编辑现有的运行配置以使用所需的 Class(具有 main 方法的那个)。 See the above link, in the third bullet point.请参阅上面的链接,在第三个要点中。 Edit the value of the class to be launched.编辑要启动的 class 的值。
  • creating a new Run configuration for the desired Class.为所需的 Class 创建新的运行配置。 Usually, you'll need to traverse to the desired class, and run your application (using the Alt+Shift+X+J shortcut) from the said class.通常,您需要遍历所需的 class,并从所述 class 运行您的应用程序(使用Alt+Shift+X+J快捷方式)。

i was facing similar issue with spring jar files but then tried with different jar files and it work so I think, classes defined in jar files were private and not available outside of jar hence you were not able to access the file. i was facing similar issue with spring jar files but then tried with different jar files and it work so I think, classes defined in jar files were private and not available outside of jar hence you were not able to access the file.

thanks, Raju Rathi谢谢, 拉朱拉蒂

Right click on the project--->Build Path--->Configure Build Path...--->In left side you have to choose Java Build Path--->Libraries--->Add External JARs--->ok--->ok右键单击项目--->构建路径--->配置构建路径...--->在左侧你必须选择Java构建路径--->库--->添加外部JARs--- >好的--->好的

Steps to add jar file in eclipse 1. right click on project 2. click on Bulid Path->configure path 3. click on java Build path 4. Click on libraries tab 5. click on add external jar tab 6. choose jar file 7 click on ok Steps to add jar file in eclipse 1. right click on project 2. click on Bulid Path->configure path 3. click on java Build path 4. Click on libraries tab 5. click on add external jar tab 6. choose jar file 7点击确定

  1. Copy the.jar file in libs folder which you want to add in your project.将.jar 文件复制到要添加到项目中的 libs 文件夹中。
  2. Right click on.jar file -> Add Build Path右击.jar文件->添加构建路径

Done.完毕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM