Section 17.4. SWT and gcj


17.4. SWT and gcj

Up to now, we have told you again and again that SWT will work with gcj. But no Linux distribution with which we are familiar provides SWT with gcj out of the box. So how do you get SWT to play nice with gcj? Unfortunately, you have a bit of work to do. Fortunately, the work is not particularly difficult.

Before we proceed, we must acknowledge those who have been there before. We, too, had heard about SWT's usability with gcj but we had never bothered to try it because there was no documentation on how to do it. We first made the attempt thanks to a great IBM developerWorks article by Kirk Vogen entitled "Create Native, Cross-Platform GUI Applications." Follow the URL[17] to the information that enabled us to write this chapter.[18]

[17] http://www-106.ibm.com/developerworks/library/j-nativegui/

[18] Please note that Kirk's article provides links to additional documentation and to an ant buildfile that automates the steps we are going to teach you manually here. We certainly didn't want to steal anything from Mr. Vogen (or from IBMscary!), so we will instead direct you to the (copyrighted) IBM Web resources. The article is worth checking out. It can save you some time over our version of the process. It is up to you.

SWT source code is included in the Eclipse SDK download. See Section 10.4 for details on where and how to download and install Eclipse. Once you have Eclipse, you need to get your mits on the SWT source code. What we will do is compile the SWT source into a shared object file that we can link to any gcj application.

We're assuming that you've got gcj installed. We're assuming that you've unzipped the Eclipse SDK. We're assuming you're still reading the book. We have to make that assumption. The first thing you need to do is to unzip the SWT source code. It is found in ECLIPSE_INSTALL/plugins/org.eclipse.platform.linux.gtk.source_2.1.2/src/org.eclipse.swt.gtk_2.1.2/ws/gtk. If you are using (as we recommend) the GTK version of Eclipse,[19] there are two files in there: swtsrc.zip and swt-pisrc.zip.

[19] Be aware: As helpful as Kirk Vogen's article and files are, they are written to an old version of gcj and they assume you are using the Motif version of Eclipse. His scripts work only with the Motif version.

Once you have these unzipped, you have to compile the code with gcj. There are two different patterns these files follow. Files that do not contain native methods are compiled with a command line that looks like this:

 $ gcj -c SomeClass.java -o SomeClass.o 

Files that do contain native methods are compiled with a command line that looks like this:

 $ gcj -fjni -c SomeClass.java -o SomeClass.o 

That said, it does no harm to compile a source file that has no native methods with the -fjni flag. This gives us a quick and dirty way to make our library file.

 $ find . -name "*.java" -exec gcj -fjni -c {} \; -print 

Remember, you are in UNIX-land. Leverage your tools! In this case, the advantage of using find is that, should the SWT source change (classes added or removed), our "compile process" will handle it. Obviously, you can take this in more sophisticated directions with make or ant. But this will get the job done for us for now.

That will compile all of the SWT source.[20] Next, we want to assemble all of the object files produced into a shared object.

[20] When we did this with Eclipse 2.1 GTK and gcj version 3.2.2, we had one compile error where the return type of the org.eclipse.swt.custom.TableCursor.traverse() method was void, whereas the Control.traverse() method (from which TableCursor inherits) was boolean. So we hacked it. We changed the return type of TableCursor.traverse() to boolean and had it return true. We didn't test to see if this was right! Use at your own peril!

 $ gcj -shared -o swt.so $(find . -name "*.o" -print) 

Once again, we leverage our tools. This time, we use bash execution quotes around our find command to get all of the .o filenames added to our gcj command that builds the shared library. For our final trick, we will compile our HelloWorld class from the start of this chapter with gcj and our new SWT shared library:

 $ gcj -classpath=~/eclipse/plugins/org.eclipse.swt/swt.jar:\ ~/eclipse/plugins/org.eclipse.swt/swt-pi.jar -c HelloWorld.java $ gcj -main=HelloWorld -o HelloWorld Hello.o swt.so $ export LD_LIBRARY_PATH=.:~/eclipse:\ ~/eclipse/plugins/org.eclipse.swt/ws/gtk $ ./HelloWorld 

Et voilà! You have the HelloWorld application! Again. But now it is an executable binary. Enjoy.



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net