步骤: 1. 创建一个org.eclipse.core.resources.IProject 2. 给该project设置description,也就是生成.project文件 注意:a、JavaCore.NATURE_ID = "org.eclipse.jdt.core.javanature"表示是个java project b、在将description置入project时,保证project是存在的并且打开中。 3. 将org.eclipse.core.resources.IProject转换为java project IJavaProject javaProject=JavaCore.create(project); 4. 取得eclipse的jre路径和当前project的路径,设置源代码的存放目录以及*.class文件的存放目录 这块生成的是.classPath文件中的内容 5. 将一个字符串编译为java文件和对应的.class文件 example: package org.wliu.sample.createnewjavaproject.actions; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.ui.PreferenceConstants; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; /** * Our sample action implements workbench action delegate. * The action proxy will be created by the workbench and * shown in the UI. When the user tries to use the action, * this delegate will be created and execution will be * delegated to it. * @see IWorkbenchWindowActionDelegate */ public class SampleAction implements IWorkbenchWindowActionDelegate { private IWorkbenchWindow window; /** * The constructor. */ public SampleAction() { } /** * The action has been activated. The argument of the * method represents the 'real' action sitting * in the workbench UI. * @see IWorkbenchWindowActionDelegate#run */ public void run(IAction action) { MessageDialog.openInformation( window.getShell(), "CreateNewJavaProject Plug-in", "Hello, Eclipse world"); // create the resource project IWorkspaceRoot root=ResourcesPlugin.getWorkspace().getRoot(); org.eclipse.core.resources.IProject project = root.getProject("wliu"); IJavaProject javaProject =null; if(!project.exists()){ IProjectDescription prjDesc=root.getWorkspace().newProjectDescription(project.getName()); String[] oldNatureIds=prjDesc.getNatureIds(); String[] newNatureIds = new String[oldNatureIds.length+1]; System.arraycopy(oldNatureIds, 0, newNatureIds, 0, oldNatureIds.length); newNatureIds[oldNatureIds.length]=JavaCore.NATURE_ID;// define this is a java project prjDesc.setNatureIds(newNatureIds); try { project.create(new NullProgressMonitor()); project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(new NullProgressMonitor(), 1000)); project.setDescription(prjDesc, new NullProgressMonitor()); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ try{ IProjectDescription prjDesc=project.getDescription(); String[] oldNatureIds=prjDesc.getNatureIds(); String[] newNatureIds = new String[oldNatureIds.length+1]; System.arraycopy(oldNatureIds, 0, newNatureIds, 0, oldNatureIds.length); newNatureIds[oldNatureIds.length]=JavaCore.NATURE_ID;// define this is a java project prjDesc.setNatureIds(newNatureIds); project.open(IResource.BACKGROUND_REFRESH, new SubProgressMonitor(new NullProgressMonitor(), 1000)); project.setDescription(prjDesc, new NullProgressMonitor()); }catch(CoreException e){ e.printStackTrace(); } } javaProject=JavaCore.create(project); // create the classpath entries // get the platform's jre classpath IClasspathEntry[] jreClasspaths=PreferenceConstants.getDefaultJRELibrary(); // get the project existing classpath IClasspathEntry[] oldClasspathEntries = null; try { oldClasspathEntries = javaProject.getRawClasspath(); } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } Set newClasspathEntries = new HashSet(); newClasspathEntries.addAll(Arrays.asList(jreClasspaths)); newClasspathEntries.addAll(Arrays.asList(oldClasspathEntries)); // try { // javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]), new NullProgressMonitor()); // } catch (JavaModelException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // create the output location IFolder binFolder=javaProject.getProject().getFolder("bin"); try { if(!binFolder.exists()){ binFolder.create(true, true, new NullProgressMonitor()); } } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { javaProject.setOutputLocation(binFolder.getFullPath(), new NullProgressMonitor()); } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } // create the source folder IFolder srcFolder = javaProject.getProject().getFolder("src"); if(!srcFolder.exists()){ try { srcFolder.create(true, true, null); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // add the src classpath entry to the .classpath file IClasspathEntry classpathEntry=JavaCore.newSourceEntry(srcFolder.getFullPath()); newClasspathEntries.add(classpathEntry); IClasspathEntry removeEntry = JavaCore.newSourceEntry(new Path("/"+project.getName())); if(newClasspathEntries.contains(removeEntry)){ newClasspathEntries.remove(removeEntry); } try { javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]), new NullProgressMonitor()); } catch (JavaModelException e) { // TODO Auto-generated catch block System.err.println("error"); e.printStackTrace(); } System.out.println("OK"); try { IPackageFragmentRoot fileRoot = javaProject.findPackageFragmentRoot(new Path("/"+project.getName()+"/src")); IPackageFragment packageFragment=fileRoot.createPackageFragment("org.talend.sample", true, new NullProgressMonitor()); String strJava = "package org.talend.sample;public class Test{public static void main(String[] args){"+ "System.out.println(/"OK/");}}"; packageFragment.createCompilationUnit("Test.java", strJava, true, new NullProgressMonitor()); }catch(JavaModelException e){ } } /** * Selection in the workbench has been changed. We * can change the state of the 'real' action here * if we want, but this can only happen after * the delegate has been created. * @see IWorkbenchWindowActionDelegate#selectionChanged */ public void selectionChanged(IAction action, ISelection selection) { } /** * We can use this method to dispose of any system * resources we previously allocated. * @see IWorkbenchWindowActionDelegate#dispose */ public void dispose() { } /** * We will cache window object in order to * be able to provide parent shell for the message dialog. * @see IWorkbenchWindowActionDelegate#init */ public void init(IWorkbenchWindow window) { this.window = window; } }
eclipse如何建立java项目_eclipse中编程创建一个java项目
最新推荐文章于 2024-10-20 21:13:54 发布