3

I have tried to create one source folder in a java project with the below code.

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(projectName);
    project.create(null);
    project.open(null);
    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    IJavaProject javaProject = JavaCore.create(project); 
    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, 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());
    javaProject.setRawClasspath(newEntries, null);

But it is giving Java Model Exception from the last line : javaProject.setRawClasspath(newEntries, null);

Java Model Exception: Java Model Status [Cannot nest 'ProjectName/src' inside 'ProjectName'. To enable the nesting exclude 'src/' from 'ProjectName']
    at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:784)
    at org.eclipse.jdt.internal.core.JavaProject.setRawClasspath(JavaProject.java:3102)
    at org.eclipse.jdt.internal.core.JavaProject.setRawClasspath(JavaProject.java:3064)
    at org.eclipse.jdt.internal.core.JavaProject.setRawClasspath(JavaProject.java:3117)

Can any one tell me how can I create source folder programmatically?

4

2 回答 2

1

当您调用 javaProject.getPackageFragmentRoot() 时,您为项目创建了一个构建路径,使用它自己作为源文件夹。跳过它,您可以从 IFolder 实例中获取项目相对路径,并从中创建您的 newSourceEntry。

于 2013-08-26T18:50:22.687 回答
0

我昨天遇到了这个问题。不幸的是,我无法再找到获得解决问题所需信息的链接,但这是解决方案

项目根“ProjectName”已经在类路径中,因此不能将其子文件夹添加到类路径中。为了在类路径中包含“ProjectName/src”,我只需将“ProjectName”类路径条目替换为新的“ProjectName/src”条目。

于 2015-05-29T06:47:47.690 回答