eclipse jdt初探

最近打算了解下jdt,以便以后做些代码辅助工具
下面记一些代码片段,等熟练时再将起串起来
IWorkspaceRoot root = workspace.getRoot();
		System.out.println(root.getLocationURI());
		IProject[] projects = root.getProjects();
    	for(IProject project : projects) {
    		System.out.println(project.getName());
    		IJavaProject myJavaProject = JavaCore.create(project);
    		if(myJavaProject != null) {
    			System.out.println("ElementName : " + myJavaProject.getElementName());
    		}
    	}
    	IClasspathEntry classpathEntry = JavaCore.newSourceEntry(new Path("/a/src"));


AddSelectedLibraryToBuildpathAction类中设置类路径的一段代码:
final IRunnableWithProgress runnable= new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					try {
				        IJavaProject project= JavaCore.create(files[0].getProject());
				        List result= addLibraryEntries(files, project, monitor);
						selectAndReveal(new StructuredSelection(result));
					} catch (CoreException e) {
						throw new InvocationTargetException(e);
					}
				}
			};


为java工程动态增加jar
IProject project = root.getProject("a");
    	IJavaProject javaProjectA = JavaCore.create(project);
		IClasspathEntry[] oldEntries = null;
		try {
			oldEntries = javaProjectA.getRawClasspath();
		} catch (JavaModelException e1) {
			e1.printStackTrace();
		}
		IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
		System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
		newEntries[oldEntries.length] = JavaCore.newLibraryEntry(new Path("/a/lib/mail.jar"), null,
				null);
		try {
			javaProjectA.setRawClasspath(newEntries, null);
		} catch (JavaModelException e1) {
			e1.printStackTrace();
		}
		IResource[] resources = null;
		IFolder folder = javaProjectA.getProject().getFolder("lib");
		try {
			resources = folder.members();
		} catch (CoreException e) {
			e.printStackTrace();
		}
		for(IResource resource : resources) {
			System.out.println("lib resources : " + resource);
		}

你可能感兴趣的:(java,eclipse)