我可以手动/编程生成eclipse工作区和项目吗?

时间:2012-12-07 23:08:09

标签: java eclipse eclipse-jdt

我需要测试JDT / LTK示例,为了做到这一点,我需要手动或以编程方式创建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");

2 个答案:

答案 0 :(得分:1)

我不确定您是否可以在正在运行的工作空间中创建新的IWorkspace,但是您应该能够创建一个目录和嵌套项目,这些项目可以在重新启动Eclipse时用作工作空间。

另一种方法是启动Eclipse测试环境。有一个运行对话框,可让您指定工作区根目录。我相信,如果你蠢蠢欲动,你可以推断出如何以编程方式关闭测试工作区(运行它)。

答案 1 :(得分: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