以编程方式将库添加到 Eclipse 项目
如何为任何 *.jar 文件创建新的构建路径条目并将该类路径条目添加到 Eclipse 项目的构建路径中。
我有一个应该自动设置我的目标项目的插件。所以这个项目需要有一些库导入,我想使用向导自动添加这些导入。用户只需选择某个SDK的位置,然后一些库就必须与目标项目链接。
但是,我找到了一些参考资料:
如何将文件夹添加到 java 构建路径作为库,其中包含多个 jar 或条目?
不幸的是,我未能实现第二个解决方案,因为我找不到类 IClasspathContainer、JavaCore 和 IJavaProject。
我正在使用 Eclipse Helios 和 JDK。我是否需要任何其他库来更改构建路径,或者是否有更简单的解决方案以编程方式导入 jar 库?
问候, 弗洛里安
How can I create a new build path entry for any *.jar file and add this classpath entry to the build path of an Eclipse project.
I have a plugin that should automatically setup my target project. So this project needs to have some library imports and I want to add this imports automatically using a wizard. The user just selects the location of a certain SDK and then some libraries have to be linked with the target project.
However, I found some references:
Importing libraries in Eclipse programmatically
How to add a folder to java build path as library, having multiple jars or entries in it?
Unfortunately, I failed to implement the second solution as I cannot find the classes IClasspathContainer, JavaCore and IJavaProject.
I'm using Eclipse Helios and JDK. Do I need any additional libraries to make changes to the build path or is there a simpler solution to import a jar library programmatically?
Regards,
Florian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您正在创建一个插件,并且需要您的插件来管理添加到类路径中的额外 jar。
正如您提到的,您需要创建一个自定义类路径容器。首先,通过扩展此扩展点来创建类路径容器扩展:
然后,创建一个实现 org.eclipse.jdt.core.IClasspathContainer 的类,并将其与刚刚创建的扩展点关联起来。
您提到找不到 org.eclipse.jdt.core.IClasspathContainer 接口。您需要确保您的插件在其 MANIFEST.MF 中引用 org.eclipse.jdt.core 插件。
I'm assuming that you are creating a plugin and need your plugin to manage the extra jars added to the classpath.
As you mention, you need to create a custom classpath container. First, create the classpath container extension by exending this extension point:
Then, you create a class that implements org.eclipse.jdt.core.IClasspathContainer and associate it with the extension point you just created.
You mention that you cannot find the org.eclipse.jdt.core.IClasspathContainer interface. You need to make sure that your plugin references the org.eclipse.jdt.core plugin in its MANIFEST.MF.
在这里您可以找到一些示例,了解如何为 java 项目定义新的类路径条目和类路径容器。我认为这对于阅读这个问题的人来说会很方便。
Here you can find some examples, how to define new classpath entries and classpath containers to java projects. I think it would handy for someone reading this question.
为了访问 IJavaProject 等,请转到您的plugin.xml 并将
org.eclipse.jdt.core
添加到类路径。此后,您可以将这些包导入到您的项目中。In order to get access to IJavaProject etc, goto your plugin.xml and add
org.eclipse.jdt.core
to the classpath. Thereafter you can import those packages into your project.请注意,正如 Andrew Eisenberg 提到的,您需要在插件的 MANIFEST.MF 中包含 org.eclipse.jdt.core 插件依赖项。
请注意,您可能还需要以编程方式刷新项目。
Note that as Andrew Eisenberg mentioned, you need to include the
org.eclipse.jdt.core
plugin dependency in your plugin's MANIFEST.MF.Note that you may also need to programmatically refresh the project too.