views:

365

answers:

2

DISCLAIMER:

this is the classic case of .NET GUI trying to work his way around JAVA stuff.

PROBLEM DESCRIPTION:

I am trying to build a very simple GUI with JFace and SWT - the code is straightforward (there's plenty tutorials), what it's not so straightforward is that I can't seem to get JFace and SWT to work outside a plugin project.

I would expect to be able to use JFace and SWT in my project no hassle, since I put "C:/eclipse 3.5/plugins" in my CLASSPATH (from my computer --> properties --> advanced --> environment variables) and all the swt-*.dll I could find in my PATH (both local user and global PATHs, to be sure) as this article indicates in the "Installing SWT and JFace" box.

Problem is I can't import anything from eclipse.org unless I reference directly the jars from buildpath --> libraries --> add external jars (in order for it to build I have to add the following jars: org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar, org.eclipse.jface_3.5.1.M20090826-0800.jar). Once I do that it builds fine but then when I run it as "Java Application" I get the following error (should I RUN AS something else?):

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/runtime/IProgressMonitor
    at demo.ui.test.EntryPoint.main(EntryPoint.java:18)
Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.IProgressMonitor
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    ... 1 more

I tried to debug this and basically it is thrown as soon as it tries to instantiate the ApplicationWindow class (org.eclipse.jface.window.ApplicationWindow). Trying to reproduce the error, I commented out all my code and replaced it with the following in my main and it throws he same error as above on the first line:

ApplicationWindow w = new ApplicationWindow(null); //<-- error on this line
w.setBlockOnOpen(true);
w.open();
Display.getCurrent().dispose();

QUESTIONS:

Question 1: how can I fix the error above (I'd like to understand what's going on) and get the damn thing to run?

Question 2: why the jars are not visible to my project and how to make them?

I am probably missing something very trivial due to my lack of familiarity with Java and eclipse. Any help highly appreciated!

EDIT: seems like someone else had the same problem --> http://www.eclipsezone.com/eclipse/forums/t60528.html - not too clear how they solved it though, assistance appreciated

+3  A: 

IProgressMonitor interface is not available in those two jars you use. You also need to put org.eclipse.equinox.common plugin on your classpath. IProgressMonitor can be used without whole eclipse environment running.

(This is solution from the article you refer to. I originally thought that IProgressMonitor is in org.eclipse.core.runtime plugin, but it has been moved to org.eclipse.equinox.common as described in bug #122935)

Peter Štibraný
thanks - I managed to understand that, but it took a while as in that article their talking about MANIFEST file which I do not have since I a am on plain java. So I just went on and added a reference to org.eclipse.equinox.common and it fixed it (started throwing another error about EventManager actually but fixed it referencing org.eclipse.core.commands). I found this very confusing ... I should get some warnings if some of the stuff I need to run the app is missing.
JohnIdol
It is indeed confusing. If you were developing eclipse plugin, you would get all warnings, since plugin dependencies are written in MANIFEST.MF file. Ff you directly referenced some class/interface that is missing, you would also get warning. But using jar files in standalone application is tricky, and you need to hunt dependencies on your own (or use something like Ivy or Maven).
Peter Štibraný
+1  A: 

Echoing Peter's point above, I always have to add a bunch of JARs to get things running (especially for JFace). Typically, I'll need

./org.eclipse.core.commands_3.3.0.I20070605-0010.jar ./org.eclipse.core.runtime_3.3.100.v20070530.jar ./org.eclipse.equinox.common_3.3.0.v20070426.jar ./org.eclipse.jface_3.3.1.M20070910-0800b.jar ./org.eclipse.osgi_3.3.2.R33x_v20080105.jar ./org.eclipse.ui.forms_3.3.0.v20070511.jar

You can use something like jarfinder to help spot what files live in what JARs. In windows I seem to remember using the find feature to look for, say, IProgressMonitor.class in zip/jar files under the eclipse folder on the file system - as sometimes the actual JARs can be hard to track down.

I usually work by seeing what fails, tracking down / adding the JAR, rinse and repeat.

Doing RCP/plugins seems like Eclipse takes care of a lot of the issues for you, doing your own stand alone app comes with this extra baggage but its really nice to have a proper stand-alone native looking GUI if you stick with it. So I typically setup vanilla (non-plugin) eclipse projects and it forces you to confront these issues (add JARs to the classpath manually). It also comes in handy as and when you want to distribute your project.

Toby