我正在尝试以编程方式创建一个带有源文件夹的 java 项目。我能够创建 java 项目,但在其中我无法创建源文件夹,因为我收到如下错误。
java.lang.ArrayStoreException
at java.util.AbstractCollection.toArray(AbstractCollection.java:171)
at org.eclipse.jdt.launching.JavaRuntime.getVMInstallTypes(JavaRuntime.java:533)
at org.eclipse.jdt.launching.JavaRuntime.getVMInstallType(JavaRuntime.java:414)
at org.eclipse.jdt.internal.launching.VMDefinitionsContainer.populateVMTypes(VMDefinitionsContainer.java:483)
at org.eclipse.jdt.internal.launching.VMDefinitionsContainer.parseXMLIntoContainer(VMDefinitionsContainer.java:467)
at org.eclipse.jdt.launching.JavaRuntime.addPersistedVMs(JavaRuntime.java:1488)
at org.eclipse.jdt.launching.JavaRuntime.initializeVMs(JavaRuntime.java:2654)
at org.eclipse.jdt.launching.JavaRuntime.getDefaultVMId(JavaRuntime.java:541)
at org.eclipse.jdt.launching.JavaRuntime.getDefaultVMInstall(JavaRuntime.java:486)
我尝试了以下代码来以编程方式创建 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 binFolder = project.getFolder("bin");
binFolder.create(false, true, null);
javaProject.setOutputLocation(binFolder.getFullPath(), null);
List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
for (LibraryLocation element : locations) {
entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
}
//add libs to project class path
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
IFolder sourceFolder = project.getFolder("src");
sourceFolder.create(false, true, null);
PackageFragmentRoot 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);
有什么问题吗?How can I create source folder programmatically
?