Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以手动/编程地生成eclipse工作区和项目吗?_Java_Eclipse_Eclipse Jdt - Fatal编程技术网

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

Java 我可以手动/编程地生成eclipse工作区和项目吗?,java,eclipse,eclipse-jdt,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"); p

我需要测试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");

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


另一种方法是启动Eclipse测试环境。有一个运行对话框,用于指定工作区根目录。我相信,如果您仔细研究一下,您可以推断出如何以编程方式启动(运行)测试工作区

以下代码使用包和类动态创建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);
参考: