首页 > 代码库 > 后台创建Java工程

后台创建Java工程

 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(); iterator23                     .hasNext();) {24                 IClasspathEntry iClasspathEntry = iterator.next();25                 if (iClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE26                         && ("/" + project.getName()).equals(iClasspathEntry27                                 .getPath().toString())) {28                     iterator.remove();29                 }30             }31 32             //设置src路径,依赖库信息33             IClasspathEntry newSourceEntry = JavaCore.newSourceEntry(new Path(34                     "/" + project.getName() + "/src"));35             IClasspathEntry newContainerEntry = JavaCore36                     .newContainerEntry(new Path(37                             "org.eclipse.jdt.launching.JRE_CONTAINER"));38             uniqueEList.add(newSourceEntry);39             uniqueEList.add(newContainerEntry);40 41             //创建必要的文件夹42             IFolder src = http://www.mamicode.com/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(uniqueEList65                     .toArray(new IClasspathEntry[uniqueEList.size()]), null);66             javaProject.open(null);67         } catch (CoreException e) {68         }