通过代码创建Java项目

 在运行着Resources插件的RCP环境中,只需提供projectName,通过代码的方式创建一个具有Java语言特征的项目。
本例子通过一个简单的带有action的例子,创建一个固定名字的Java项目。

将创建项目的代码绑定到Create Sample Java Project 的action上。

String projectName =  "Sample"  ;
1、创建一个带有.project文件的项目
IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
                              NullProgressMonitor monitor= new NullProgressMonitor();
                               if (!prj.exists()){
                                    IProjectDescription desc = prj.getWorkspace().newProjectDescription(projectName);
                                    desc.setLocationURI( null );
                                     try {
                                          prj.create(desc, monitor);
                                           if (!prj.isOpen()) {
                                                prj.open(monitor);
                                                monitor= null ;
                                          }
                                    } catch (CoreException e) {
                                          e.printStackTrace();
                                    }
                              }



 2、添加Java项目的特性
                                    IProjectDescription desc = prj.getDescription();
                                    String oldnatureID[] = desc.getNatureIds();
                                     boolean hasJavaNature = prj.hasNature(JavaCore.NATURE_ID );
                                    
                                    String newnatureID[] = new String[hasJavaNature?oldnatureID.length :oldnatureID. length + 1];
                                    System. arraycopy(oldnatureID, 0, newnatureID, 0, oldnatureID.length );
                                     if (!hasJavaNature)
                                    newnatureID[oldnatureID. length ]=JavaCore. NATURE_ID;
                                    
                                    desc.setNatureIds(newnatureID);
                                    prj.setDescription(desc, new NullProgressMonitor());
                                    IJavaProject javaProject = JavaCore.create(prj);
                                    prj.setDefaultCharset( "UTF-8" ,new NullProgressMonitor());


3、添加src目录
IFolder src = javaProject.getProject().getFolder( "src" );
                                    File srcfile = src.getLocation().toFile();
                                     if (!src.exists()&& !srcfile.exists()){
                                          src.create( false , true , null);
                                    }


4、添加bin目录
IFolder bin = javaProject.getProject().getFolder( "bin" );
                                    File binfile = bin.getLocation().toFile();
                                     if (!bin.exists()&& !binfile.exists()){
                                          bin.create( false , true , null);
                                    }


5、添加.classpath关于jre、output、src的设值
//create .classpath
                                    List< IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry >();
                                     //jre
                                    String jre = JavaRuntime.getDefaultVMInstall().getName();
                                     IClasspathEntry[] entrties = getJREEntities(jre);
                                     //output
                                     final IPath projectPath = prj.getFullPath();
                                    IPath outputLocation = projectPath.append("/bin" );
                                    
                                     //src
                                     @SuppressWarnings ("restriction" )
                                    ClasspathEntry newSourceEntry = (ClasspathEntry) JavaCore
                                    . newSourceEntry(src.getFullPath(),
                                                 null );
                                    classpathEntries.add(newSourceEntry);
                                    classpathEntries.addAll(Arrays. asList(entrties));
                                    
                                     IClasspathEntry[] classpath  = (IClasspathEntry[]) classpathEntries
                                    .toArray( new IClasspathEntry[classpathEntries.size()]);
                                    
                                     //Sets the both the classpath of this project and its default output location at once
                                    javaProject.setRawClasspath(classpath, outputLocation,
                                                 new NullProgressMonitor());


工具方法
private IClasspathEntry[] getJREEntities(String jreName){
        IVMInstallType[] types = JavaRuntime. getVMInstallTypes();
        for (int i = 0; i < types. length; i++) {
            IVMInstallType type = types[i];
            IVMInstall[] installs = type.getVMInstalls();
            for (int j = 0; j < installs. length; j++) {
                IVMInstall install = installs[j];
                if (install.getName().equals(jreName)){
                    IPath jrePath=JavaRuntime.newJREContainerPath(install);
                    return new IClasspathEntry[] { JavaCore.newContainerEntry(jrePath) };
                }
            }
        }
        return PreferenceConstants.getDefaultJRELibrary();
      }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值