|
17.4. SWT and gcjUp 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]
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.
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.
$ 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. |
|