Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run SWT app in Eclipse

Tags:

java

eclipse

swt

I was following the tutorial that came in the Eclipse editor (click Help -> Welcome -> Tutorials). I worked through all the steps which were:

This cheat sheet shows you how to create a "Hello World" application that uses the Standard Widget Toolkit (SWT). The application will simply display an empty window to the user. If you need help at any step, click the (?) to the right. Let's get started!

If you're not already in the Java perspective, in the main menu select Window > Open Perspective > Java or click on the "Click to Perform" link below.

Open the Import wizard from the main menu via File > Import..., and select Plug-in Development > Plug-ins and Fragments. Click Next. On the Import Plug-ins and Fragments page, select Import from: The active target platform. Plug-ins and Fragments to import: Select from all plug-ins and fragments found at specified location. Import As: Projects with source folders. Click Next. On the Selection page, Add org.eclipse.swt.{platform}.{os}.{arch} (for example: org.eclipse.swt.win32.win32.x86 for win32) to Plug-ins and Fragments to Import: list. Click Finish. This will create the org.eclipse.swt.{platform}.{os}.{arch} project which we will need to compile and run the application.

Now we need a project to store our own source code. In the main toolbar, click on the New Java Project button, or click on the link below. Enter HelloWorldSWT for the project name, then click Finish.

Since our project requires SWT, we need to specify this in the project properties. Right-click on the project and select Properties. In the Java Build Path page open the Projects tab, add the org.eclipse.swt.{platform}.{os}.{arch} project, then click OK.

The next step is to create a new class. In the main toolbar, click on the New Java Class button (or the link below). If not already specified, select HelloWorldSWT/src as the source folder. Enter HelloWorldSWT for the class name and select the checkbox to create the main() method, then click Finish. The Java editor will automatically open showing your new class.

In the Java editor, enter the following Java code in the main() method: Display display = new Display(); Shell shell = new Shell(display); shell.setText("Hello world!"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); You will get compile errors. Right click in the Java editor and select Source > Organize Imports, then save your changes.

To run your application, right-click on your class in the Package Explorer and select Run As > Java Application. A new empty window should appear with the title "Hello world!". Congratulations! You have successfully created a Hello World SWT application!

And here's a screenshot:

screenshot

But when I tried to run the program, instead of displaying a blank screen as expected, it dumped this in the terminal:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: 
no swt-cocoa-4233 in java.library.path
no swt-cocoa in java.library.path
Can't load library: /Users/devenkelling/.swt/lib/macosx/x86_64/libswt-cocoa-4233.jnilib
Can't load library: /Users/devenkelling/.swt/lib/macosx/x86_64/libswt-cocoa.jnilib

at org.eclipse.swt.internal.Library.loadLibrary(Library.java:331)
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:240)
at org.eclipse.swt.internal.C.<clinit>(C.java:21)
at org.eclipse.swt.widgets.Display.<clinit>(Display.java:101)
at HelloWorldSWT.main(HelloWorldSWT.java:11

I'm using an x64 system with the 64-bit Eclipse installed and both 32-bit and 64-bit versions of Java (I think). Please help. Thanks.

like image 739
Yatharth Agarwal Avatar asked Dec 26 '22 15:12

Yatharth Agarwal


1 Answers

Source: Eclipse Forums

This worked for me:

  • Right-click on your your project folder HelloWorldSWT;

  • Go to Properties -> Left Sidebar Java Build Path -> Tab Projects;

  • Expand the org.eclipse.swt.cocoa.macosx.x86_64 folder by clicking on the arrow left to it;

  • Select Native library location and click Edit;

  • Click the Workspace button and select org.eclipse.swt.cocoa.macosx.x86_64;

  • Click OK a couple of times;

And you're done! Hope this helped.

like image 162
Yatharth Agarwal Avatar answered Dec 31 '22 11:12

Yatharth Agarwal