1 //获取工程对象 2 IProject project = ResourcesPlugin.getWorkspace().getRoot() 3 .getProject(TEST_PROJECT); 4 try { 5 //创建工程资源 6 if (!project.exists()) { 7 project.create(null); 8 } 9 10 //设置工程语言描述信息 11 IProjectDescription description = ResourcesPlugin.getWorkspace() 12 .newProjectDescription(TEST_PROJECT); 13 description.setNatureIds(new String[] { JavaCore.NATURE_ID }); 14 project.open(null); 15 project.setDescription(description, null); 16 17 //获取Java工程对象 18 IJavaProject javaProject = JavaCore.create(project); 19 UniqueEList<IClasspathEntry> uniqueEList = new UniqueEList<IClasspathEntry>( 20 Arrays.asList(javaProject.getRawClasspath())); 21 //删除无用的src文件路径信息 22 for (Iterator<IClasspathEntry> iterator = uniqueEList.iterator(); iterator 23 .hasNext();) { 24 IClasspathEntry iClasspathEntry = iterator.next(); 25 if (iClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE 26 && ("/" + project.getName()).equals(iClasspathEntry 27 .getPath().toString())) { 28 iterator.remove(); 29 } 30 } 31 32 //设置src路径,依赖库信息 33 IClasspathEntry newSourceEntry = JavaCore.newSourceEntry(new Path( 34 "/" + project.getName() + "/src")); 35 IClasspathEntry newContainerEntry = JavaCore 36 .newContainerEntry(new Path( 37 "org.eclipse.jdt.launching.JRE_CONTAINER")); 38 uniqueEList.add(newSourceEntry); 39 uniqueEList.add(newContainerEntry); 40 41 //创建必要的文件夹 42 IFolder src = project.getFolder(new Path("src")); 43 if (!src.exists()) { 44 try { 45 src.create(false, true, null); 46 } catch (CoreException e) { 47 e.printStackTrace(); 48 } 49 } 50 IFolder bin = project.getFolder(new Path("bin")); 51 if (!bin.exists()) { 52 try { 53 bin.create(false, true, null); 54 } catch (CoreException e) { 55 e.printStackTrace(); 56 } 57 } 58 59 //设置输出路径 60 javaProject.setOutputLocation(new Path("/" + project.getName() 61 + "/bin"), null); 62 63 //设置描述信息到Java工程 64 javaProject.setRawClasspath(uniqueEList 65 .toArray(new IClasspathEntry[uniqueEList.size()]), null); 66 javaProject.open(null); 67 } catch (CoreException e) { 68 }