Obter nome dos projetos abertos no Eclipse

5 respostas
harrysongui
Preciso obter o nome dos projetos abertos no Eclipse.Tentei fazer da seguinte forma:
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;



public class PegaProjeto {
	public void getProjects(){
		IWorkspace workSpace=ResourcesPlugin.getWorkspace();
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		int i;
		for(i=0;i<projects.length;i++){
			System.out.println("Projeto: "+projects[i].getName());
		}
	}


	public static void main(String[]args){
		new PegaProjeto().getProjects();
	}
}
Mas está dando algum erro que eu não estou entendendo o que pode ser.>

5 Respostas

Ataxexe

Qual erro deu?

harrysongui

Tentei rodar numa “classe simples” e deu isso:

Exception in thread main java.lang.NoClassDefFoundError: org/eclipse/core/runtime/Plugin

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)

at java.lang.ClassLoader.defineClass(ClassLoader.java:615)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)

at java.net.URLClassLoader.access$000(URLClassLoader.java:58)

at java.net.URLClassLoader$1.run(URLClassLoader.java:197)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

at harryson.PegaProjeto.getProjects(PegaProjeto.java:11)

at harryson.PegaProjeto.main(PegaProjeto.java:21)

Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.Plugin

at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

 14 more

E tentei rodar num plugin pro eclipse e deu isso:

!SESSION 2012-12-11 17:36:46.414 -----------------------------------------------
eclipse.buildId=I20110613-1736
java.version=1.6.0_37
java.vendor=Apple Inc.
BootLoader constants: OS=macosx, ARCH=x86, WS=cocoa, NL=pt_BR
Framework arguments: -product org.eclipse.sdk.ide
Command-line arguments: -product org.eclipse.sdk.ide -data /Users/harrysonguimaraes/Desktop/…/runtime-EclipseApplication -dev file:/Users/harrysonguimaraes/Desktop/.metadata/.plugins/org.eclipse.pde.core/Eclipse Application/dev.properties -os macosx -ws cocoa -arch x86 -consoleLog

!ENTRY org.eclipse.core.net 1 0 2012-12-11 17:36:53.236
!MESSAGE System property http.nonProxyHosts has been set to local|.local|169.254/16|.169.254/16 by an external source. This value will be overwritten using the values from the preferences

!ENTRY org.eclipse.ui 4 0 2012-12-11 17:36:54.957

!MESSAGE Unhandled event loop exception

!STACK 0

java.lang.NoClassDefFoundError: org/eclipse/core/resources/ResourcesPlugin

at teste_plugin.actions.PegaProjeto.getProjects(PegaProjeto.java:10)

at teste_plugin.actions.SampleAction.run(SampleAction.java:32)

at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:251)

at org.eclipse.ui.internal.WWinPluginAction.runWithEvent(WWinPluginAction.java:229)

at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)

at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)

at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:452)

at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)

at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4125)

at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1457)

at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1480)

at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1465)

at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1270)

at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3971)

at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3610)

at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)

at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)

at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)

at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)

at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)

at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)

at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)

at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)

at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)

at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)

at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)

at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)

at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)

at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)

at org.eclipse.equinox.launcher.Main.run(Main.java:1410)

at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

Caused by: java.lang.ClassNotFoundException: org.eclipse.core.resources.ResourcesPlugin

at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)

at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)

at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)

at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)

at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

 36 more
Ataxexe

Poste a definição do plugin que você usou pra rodar e o código. Rodando localmente não vai dar certo mesmo, precisa ser em um plugin do Eclipse (e o plugin deve declarar dependência do plugin “resources”).

harrysongui
No plugin está assim:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="Teste_Plugin.actionSet">
         <menu
               label="Sample &Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="&Sample Action"
               icon="icons/sample.gif"
               class="teste_plugin.actions.SampleAction"
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="teste_plugin.actions.SampleAction">
         </action>
      </actionSet>
   </extension>

</plugin>
E a classe que inicia o plugin:
package teste_plugin.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.MessageDialog;

/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class SampleAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	/**
	 * The constructor.
	 */
	public SampleAction() {
	}

	/**
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {
		new PegaProjeto().getProjects();
	}

	/**
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}
Mas o problema é mesmo na minha classe,pq se eu altero o plugin pra dar um simples Hello world ele funciona, portant ocreio que o problema esteja na minha classe.
harrysongui

Alguma ideia, Ataxexe?
No GUJ não tem muito conteúdo sobre plugins e em todo material em que eu encontrei,o código pra pegar projetos do eclipse é igual esse meu:public void getProjects(){ IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); int i; for(i=0;i<projects.length;i++){ System.out.println("Projeto: "+projects[i]); } }
Pelo que me parece,está faltando é algum pacote.Eu usei somente esses imports: import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin;>

Criado 11 de dezembro de 2012
Ultima resposta 13 de dez. de 2012
Respostas 5
Participantes 2