我尝试使用以下代码在java项目中创建一个源文件夹。
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);
但它从最后一行提供 Java Model Exception : 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)
任何人都可以告诉我如何以编程方式创建源文件夹?
答案 0 :(得分:1)
当您调用javaProject.getPackageFragmentRoot()时,您使用自身作为源文件夹为项目创建了构建路径。跳过它,您可以从IFolder实例获取项目相对路径,并从中创建newSourceEntry。
答案 1 :(得分:0)
昨天我遇到了这个问题。不幸的是,我无法找到我获得解决问题所需信息的链接,但这是解决方案
项目根“ProjectName”已经在类路径中,因此没有子文件夹可以添加到类路径中。要在类路径中包含“ProjectName / src”,我只需将“ProjectName”类路径条目替换为新的“ProjectName / src”条目。