gpt4 book ai didi

java - Eclipse API : How to batch resource changes for IJavaProject creation

转载 作者:行者123 更新时间:2023-12-01 13:14:56 27 4
gpt4 key购买 nike

(注意:解决方案如下)

我尝试在批处理资源更改事件时创建一个 IJavaProject,但出现以下异常:

Java 模型异常:Java 模型状态 [无法将“JavaProject/src”嵌套在“JavaProject”内。要启用嵌套,请从“JavaProject”中排除“src/”]

在我第二次设置原始类路径的行上抛出异常(在下面的代码中注释到最后)。

如果没有批处理(即:删除 IWorkspaceRunnable 和相关代码),我的 IJavaProject 就可以正常初始化。那么,如何创建具有批量资源更改事件的 IJavaProject?

这是我的代码,我创建 IJavaProject 的方法主要取自 here :

public static IJavaProject getJavaProjectBatched(final IProject project,
final String sourceFolderPath,
final String binFolderPath)
throws CoreException
{
IWorkspaceRunnable r = new IWorkspaceRunnable()
{
@Override
public void run(@Nullable IProgressMonitor monitor) throws CoreException
{
if (!project.exists())
ResourceUtility.createProject(project);

IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] {JavaCore.NATURE_ID});
project.setDescription(description, null);

IFolder binFolder = project.getFolder(binFolderPath);
binFolder.create(false, true, null);

final IFolder sourceFolder = project.getFolder(sourceFolderPath);
sourceFolder.create(false, true, null);

final IJavaProject javaProject = JavaCore.create(project);
javaProject.setOutputLocation(binFolder.getFullPath(), null);

List<IClasspathEntry> entries = new ArrayList<>();
IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
for (LibraryLocation element: locations)
{
entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
}
IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);
IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
// add libs to project class path
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]),
null);
javaProject.setRawClasspath(newEntries, null);// Exception is thrown HERE!!!
}
};

project.getWorkspace().run(r, null, IWorkspace.AVOID_UPDATE, null);
@SuppressWarnings("null") @NonNull IJavaProject jp = JavaCore.create(project);
return jp;
}

最终解决方案

nitind 在下面的答案中的帮助下,我制定了适合我需要的批处理版本。简而言之,这就是如何创建一个模仿 Eclipse 内部资源树表示的 IJavaProject。

这是我更广泛的问题的一部分,发布于 here谢谢您的帮助。

public static IJavaProject getJavaProjectBatched(final IProject project, final String sourceFolderPath,
final String binFolderPath) throws CoreException
{
IWorkspaceRunnable r = new IWorkspaceRunnable()
{
@Override public void run(@Nullable IProgressMonitor monitor) throws CoreException
{
if (!project.exists())
ResourceUtility.createProject(project);

IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] {JavaCore.NATURE_ID});
project.setDescription(description, null);

IFolder binFolder = project.getFolder(binFolderPath);
binFolder.create(false, true, null);
final IFolder sourceFolder = project.getFolder(sourceFolderPath);
sourceFolder.create(false, true, null);

final IJavaProject javaProject = JavaCore.create(project);
javaProject.setOutputLocation(binFolder.getFullPath(), null);

IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);
IClasspathEntry[] newEntries = new IClasspathEntry[1];
newEntries[0] = JavaCore.newSourceEntry(root.getPath());
javaProject.setRawClasspath(newEntries, null);
}
};

project.getWorkspace().run(r, null,IWorkspace.AVOID_UPDATE, null);
@SuppressWarnings("null") @NonNull IJavaProject javaProject = JavaCore.create(project);
return javaProject;
}

最佳答案

它告诉您,您不能将项目本身及其文件夹之一放在 Java 构建路径上,除非您从代表整个项目的条目中排除该文件夹。由于您正在创建项目,因此请跳过 oldEntries 部分。

关于java - Eclipse API : How to batch resource changes for IJavaProject creation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com