2

I want to write an Java application that displays all name of projects in my workspace. But my program always notices as follows.

Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.
at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:411)
at myPackage.ProjectVisitor.<init>(ProjectVisitor.java:9)
at myPackage.ProjectVisitor.main(ProjectVisitor.java:16)

Here are my steps to create my own application:

  1. Install Java Developement Tool (JDT)

  2. Create a blank eclipse-plugin that satisfying all following options:

    • Eclipse version: 3.5 or greater
    • Execution environment: JavaSE-1.8
    • Is Rich Client Application? (choose NO)
  3. Add .jar named org.eclipse.core.resources_3.10.1.v20150725-1910.jar

  4. Create ProjectVisitor.java as follows.

    import org.eclipse.core.resources.IProject;
    import org.eclipse.core.resources.ResourcesPlugin;
    public class ProjectVisitor {
        public ProjectVisitor() {
            IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
            for (IProject project : projects) {
                // do something to display its name
            }
        }
        public static void main(String[] args) {
            ProjectVisitor m = new ProjectVisitor();
        }
    }
    
  5. Run as "Java Application".

Please help me. Thanks in advance.

ducanhnguyen
  • 161
  • 1
  • 10
  • This might help http://stackoverflow.com/questions/2113865/jdt-without-eclipse/22211176#22211176 – Templar Oct 17 '15 at 19:04

1 Answers1

5

You can't use any Eclipse workspace APIs from a plain Java Application.

Only Eclipse plugins running as part of Eclipse or an Eclipse RCP can access the workspace. This is because of the large amount of initialization done during Eclipse startup to make the workspace available.

greg-449
  • 109,219
  • 232
  • 102
  • 145