Specifying the Location of the Native Library

The second step in configuring Eclipse for SWT development is to specify the location of the SWT native library that will be passed to the JRE when you run your code within Eclipse for testing purposes. This allows the JRE to load up the native code that handles the tasks of creating and drawing the on-screen widgets. If the JRE is not able to locate this file, a runtime exception will occur.

How do I do that?

Since this is a runtime setting, it must be specified as a Run Configuration for your Eclipse project. To specify a Run Configuration, select Run Run from the Eclipse menu. This will invoke the properties dialog shown in Figure 1-9.

The Run Configurations dialog

Figure 1-9. The Run Configurations dialog

In the Run Configurations dialog, select the type of application (Java Application), then click the New button. This loads the Run Properties dialog, shown in Figure 1-10, where you specify settings that govern how your project is loaded into the JRE. The setting you need to set is located on the Arguments tab.

Since this argument is to be passed to the JVM, it must be entered into the VM arguments text box. The same rules for constructing the argument to pass apply here as when running from the command line.

The argument to pass to java is -Djava.library.path= pathtolibrary, substituting the actual path to the location where you extracted the SWT library files.

Setting the runtime arguments

Figure 1-10. Setting the runtime arguments

Now Eclipse is configured to allow SWT development and to run your SWT programs from within the Eclipse IDE for testing.

What just happened?

You learned how to specify a runtime property to be passed into the JVM when executing your program. You can learn more about the other arguments that can be passed into the JVM by consulting the Java SDK documentation, or by opening a command prompt and entering java.

This causes help text to appear in the Console window:

Note

There’s no substitute for the learning experience of just firing up a console window and experimenting. Something to do on a rainy Saturday.

C:\Documents and Settings\Administrator>java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
    -client       to select the "client" VM
    -server       to select the "server" VM
    -hotspot      is a synonym for the "client" VM  [deprecated]
                  The default VM is client.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose[:class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>

 C:\Documents and Settings\Administrator>java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
    -client       to select the "client" VM
    -server       to select the "server" VM
    -hotspot      is a synonym for the "client" VM  [deprecated]
                  The default VM is client.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose[:class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions

The setting detailed in this lab is the same as passing the -D option to the java program from the command line, as shown in Figure 1-5. You must remember this when distributing your application for running in standalone mode outside Eclipse. The proper command to invoke your SWT application from the command line would be:

java -classpath C:\SWT\swt.jar;C:\MyApp\my.jar 
     -Djava.library.path=C:\SWT  MyApp

Tip

In this and other code samples, I’ve added some formatting to allow this to appear on the printed page. This should all be on one line in reality.

Of course, you’ll need to substitute the proper values for JAR file locations, the native library location, and the application class name.

What about...

this odd error message you keep getting:

java.lang.UnsatisfiedLinkError: no swt-win32-3038 in java.library.path
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at org.eclipse.swt.internal.Library.loadLibrary(Library.java:100)
        at org.eclipse.swt.internal.win32.OS.<clinit>(OS.java:46)
        at org.eclipse.swt.widgets.Display.internal_new_GC(Display.java:1548)
        at org.eclipse.swt.graphics.Device.init(Device.java:541)
        at org.eclipse.swt.widgets.Display.init(Display.java:1573)
        at org.eclipse.swt.graphics.Device.<init>(Device.java:96)
        at org.eclipse.swt.widgets.Display.<init>(Display.java:331)
        at org.eclipse.swt.widgets.Display.<init>(Display.java:327)
        at ToolbarShellExample.<init>(SWTExample.java:24)
        at Runner.main(Runner.java:20)
Exception in thread "main"

Running within Eclipse without specifying the location of the SWT library file, or specifying an incorrect location, causes this exception to appear in the Console (class names and line numbers will vary). An identical error would appear in the Java Console if running from the command prompt using java. If you get this error, check your configuration settings and run again.

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.