以编程方式将库添加到 Eclipse 项目

发布于 2024-10-05 23:29:59 字数 592 浏览 8 评论 0原文

如何为任何 *.jar 文件创建新的构建路径条目并将该类路径条目添加到 Eclipse 项目的构建路径中。

我有一个应该自动设置我的目标项目的插件。所以这个项目需要有一些库导入,我想使用向导自动添加这些导入。用户只需选择某个SDK的位置,然后一些库就必须与目标项目链接。

但是,我找到了一些参考资料:

以编程方式在 Eclipse 中导入库

如何将文件夹添加到 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 技术交流群。

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

发布评论

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

评论(4

禾厶谷欠 2024-10-12 23:29:59

我假设您正在创建一个插件,并且需要您的插件来管理添加到类路径中的额外 jar。

正如您提到的,您需要创建一个自定义类路径容器。首先,通过扩展此扩展点来创建类路径容器扩展:

org.eclipse.jdt.core.classpathContainerInitializer

然后,创建一个实现 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:

org.eclipse.jdt.core.classpathContainerInitializer

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.

等风来 2024-10-12 23:29:59

在这里您可以找到一些示例,了解如何为 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.

記柔刀 2024-10-12 23:29:59

为了访问 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.

清眉祭 2024-10-12 23:29:59
String projectName = "MyProject"; // project to add a library to
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IJavaProject jProject = JavaCore.create(project);

for(File file : new File("path-to-some-directory-of-libraries-to-add").listFiles()){
    if(file.isFile() && file.getName().endsWith(".jar")){
        addProjectLibrary(jProject, file);
    }
}

private static void addProjectLibrary(IJavaProject jProject, File jarLibrary) throws IOException, URISyntaxException, MalformedURLException, CoreException {
    // copy the jar file into the project
    InputStream jarLibraryInputStream = new BufferedInputStream(new FileInputStream(jarLibrary));
    IFile libFile = jProject.getProject().getFile(jarLibrary.getName());
    libFile.create(jarLibraryInputStream, false, null);

    // create a classpath entry for the library
    IClasspathEntry relativeLibraryEntry = new org.eclipse.jdt.internal.core.ClasspathEntry(
        IPackageFragmentRoot.K_BINARY,
        IClasspathEntry.CPE_LIBRARY, libFile.getLocation(),
        ClasspathEntry.INCLUDE_ALL, // inclusion patterns
        ClasspathEntry.EXCLUDE_NONE, // exclusion patterns
        null, null, null, // specific output folder
        false, // exported
        ClasspathEntry.NO_ACCESS_RULES, false, // no access rules to combine
        ClasspathEntry.NO_EXTRA_ATTRIBUTES);

    // add the new classpath entry to the project's existing entries
    IClasspathEntry[] oldEntries = jProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = relativeLibraryEntry;
    jProject.setRawClasspath(newEntries, null);
}

请注意,正如 Andrew Eisenberg 提到的,您需要在插件的 MANIFEST.MF 中包含 org.eclipse.jdt.core 插件依赖项。

请注意,您可能还需要以编程方式刷新项目

String projectName = "MyProject"; // project to add a library to
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IJavaProject jProject = JavaCore.create(project);

for(File file : new File("path-to-some-directory-of-libraries-to-add").listFiles()){
    if(file.isFile() && file.getName().endsWith(".jar")){
        addProjectLibrary(jProject, file);
    }
}

private static void addProjectLibrary(IJavaProject jProject, File jarLibrary) throws IOException, URISyntaxException, MalformedURLException, CoreException {
    // copy the jar file into the project
    InputStream jarLibraryInputStream = new BufferedInputStream(new FileInputStream(jarLibrary));
    IFile libFile = jProject.getProject().getFile(jarLibrary.getName());
    libFile.create(jarLibraryInputStream, false, null);

    // create a classpath entry for the library
    IClasspathEntry relativeLibraryEntry = new org.eclipse.jdt.internal.core.ClasspathEntry(
        IPackageFragmentRoot.K_BINARY,
        IClasspathEntry.CPE_LIBRARY, libFile.getLocation(),
        ClasspathEntry.INCLUDE_ALL, // inclusion patterns
        ClasspathEntry.EXCLUDE_NONE, // exclusion patterns
        null, null, null, // specific output folder
        false, // exported
        ClasspathEntry.NO_ACCESS_RULES, false, // no access rules to combine
        ClasspathEntry.NO_EXTRA_ATTRIBUTES);

    // add the new classpath entry to the project's existing entries
    IClasspathEntry[] oldEntries = jProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = relativeLibraryEntry;
    jProject.setRawClasspath(newEntries, null);
}

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.

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