2012-12-07 36 views
1

我需要測試JDT/LTK示例,爲了做到這一點,我需要手動或以編程方式創建eclipse工作區和項目。我可以手動/編程生成eclipse工作區和項目嗎?

如何組織一個目錄以便我可以獲得有效的工作區和項目對象?我的意思是,eclipse工作區和項目有什麼特別之處?

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 
// 1. The name of the project in the workspace 
IProject project = root.getProject("Hello"); 
project.open(null /* IProgressMonitor */); 

IJavaProject javaProject = JavaCore.create(project); 
// 2. The name of the Type (including the namespace) 
IType itype = javaProject.findType("smcho.A"); 

回答

0

我不知道,你可以從一個正在運行的工作空間中創建一個新的IWorkspace,但你應該能夠創建一個目錄,並可以使用,當您重新啓動Eclipse工作空間嵌套的項目。

另一種方法是調出Eclipse測試環境。有一個運行對話框讓你指定工作區根目錄。我相信,如果你足夠了解,你可以推斷如何以編程方式關閉(運行)測試工作區。

0

以下代碼動態創建一個包含類和類的Java項目。也許它對你有用。

// create a project with name "TESTJDT" 
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 
IProject project = root.getProject("TESTJDT"); 
project.create(null); 
project.open(null); 

//set the Java nature 
IProjectDescription description = project.getDescription(); 
description.setNatureIds(new String[] { JavaCore.NATURE_ID }); 

//create the project 
project.setDescription(description, null); 
IJavaProject javaProject = JavaCore.create(project); 

//set the build path 
IClasspathEntry[] buildPath = { 
     JavaCore.newSourceEntry(project.getFullPath().append("src")), 
       JavaRuntime.getDefaultJREContainerEntry() }; 

javaProject.setRawClasspath(buildPath, project.getFullPath().append(
       "bin"), null); 

//create folder by using resources package 
IFolder folder = project.getFolder("src"); 
folder.create(true, true, null); 

//Add folder to Java element 
IPackageFragmentRoot srcFolder = javaProject 
       .getPackageFragmentRoot(folder); 

//create package fragment 
IPackageFragment fragment = srcFolder.createPackageFragment(
     "com.programcreek", true, null); 

//init code string and create compilation unit 
String str = "package com.programcreek;" + "\n" 
    + "public class Test {" + "\n" + " private String name;" 
    + "\n" + "}"; 

     ICompilationUnit cu = fragment.createCompilationUnit("Test.java", str, 
       false, null); 

//create a field 
IType type = cu.getType("Test"); 

type.createField("private String age;", null, true, null); 

參考:Creat, Access, Load Projects by using Eclipse JDT

相關問題