try {
// 先找指定的源文件夹所在的IPackageFragmentRoot
final IProject project = root.getProject(projectName);
IJavaProject javaProject = JavaCore.create(project);
IFolder srcFolder = javaProject.getProject().getFolder("src");
? ? ? ? srcFolder.getName();
? ? ? ? if(!srcFolder.exists()){
srcFolder.create(true, true, null);
? ? ? ? }
IPackageFragmentRoot packageFragmentRoot = javaProject
.findPackageFragmentRoot(new Path("/"+projectName+"/src"));
// 根据IPackageFragmentRoot创建IPackageFragment,IPackageFragment就是包了
if(packageFragmentRoot != null){
IPackageFragment packageFragment = packageFragmentRoot
.createPackageFragment(packageSrcName, true, null);
System.out.println(packageFragment.toString());
} else
throwCoreException( projectName + "下不存在src文件夹,请手动创建." );
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}