在Windows下使用GCJ+SWT的实例

Windows下使用GCJ+SWT的实例

GCJ可以把JAVA程序编译成本地代码,编译成功后的exe文件不再需要JRE就可直接运行,编译成本地后的程序运行速度有所提高。缺点是生成后的文件较大,我试了一个hello world都要两兆多,用WinRAR压缩后为600K

<o:p> </o:p>

安装及配置

1.       下载MinGW(http://sourceforge.net/projects/mingw )

网址:http://sourceforge.net/project/showfiles.php?group_id=2435

需要下载下列文件:

MinGW-2.0.0-3.exe (Windows下的GNU环境)

MSYS-1.0.8.exe (一个模拟的*nix 支持shell等)

gcj-3.2-20021210-1.tar.gz (主角GCJ编译java为本地代码)

2.       安装文件

依次安装MinGW-2.0.0-3.exeMSYS-1.0.8.exe

安装MSYS时会有提示要你输入MinGW的路径。

解压缩gcj-3.2-20021210-1.tar.gz将里面的文件复制到MinGW下,注意对应文件夹,比如gcj\bin下文件要放到对应的MinGW\bin下面。

<o:p> </o:p>

3.       设置WindowsPATHMinGW\bin加到系统的PATH里,方便以后运行gcj

<o:p> </o:p>

然后在命令行里输入gcj –v 看看吧:)

HelloGCJ.exe

为了方便描述,让我们先建立一个gcjwork文件夹,

gcjwork中建立一个普通的java源文件,HelloGCJ.java

public class HelloGCJ {<o:p></o:p>

    public static void main(String[] args) {<o:p></o:p>

        System.out.println("Hello GCJ!");<o:p></o:p>

    }<o:p></o:p>

}<o:p></o:p>

同目录下新建一个文本文件Makefile注意不要带扩展名。

TARGET=HelloGCJ<o:p></o:p>

GCJ=gcj<o:p></o:p>

<o:p> </o:p>

all: $(TARGET)<o:p></o:p>

<o:p> </o:p>

$(TARGET): $(TARGET).o<o:p></o:p>

       $(GCJ) --main=$(TARGET) -o $(TARGET) $(TARGET).o<o:p></o:p>

<o:p> </o:p>

$(TARGET).o: $(TARGET).java<o:p></o:p>

       $(GCJ) -c $(TARGET).java -o $(TARGET).o<o:p></o:p>

<o:p> </o:p>

.PHONY: clean<o:p></o:p>

<o:p> </o:p>

clean:<o:p></o:p>

       rm -rf *.o $(TARGET)<o:p></o:p>

<o:p> </o:p>

然后让我们打开MSYS使用cd进入到我们建立的gcjwork下,输入make

之后即可生成一个HelloGCJ.exe,运行他,这可是一个没有用虚拟机的用java写的程序哦。:)

<o:p> </o:p>

对比一下

为什么要把java便以成本地代码呢?

执行速度加快是一个很主要的特点,让我们做一个最最简单的对比。

还是上例的HelloGCJ,我们编译成javabytecode

注意gcj也可以编译bytecode,

使用gcj –C HelloGCJ.java 命令即可。

然后对比运行两个程序,在我的机器上可以明显感觉到HelloGCJ.class在执行时顿了一下。

HelloGCJ.exe则直接相应输出了文字。

<o:p> </o:p>

<o:p> </o:p>

再来搞搞SWT

      

用过Eclipse就知道SWTSWTIBM的图形界面开发包,因为它的实现也是与本地化有关,所以引来不少的争议,公说公有理婆说婆有理,我们就不说什么了,直接说怎么使用它,毕竟是好是坏,用了才知道!

<o:p> </o:p>

       准备<o:p></o:p>

       1swt.jat (SWTjava包,编译java要用)<o:p></o:p>

       2SWTwindows版本对应的dll(我的的文件名是swt-win32-2133.dll)

       上面这两个文件可以在eclipse下载最新的SWT工具包

       http://download2.eclipse.org/downloads/drops/R-2.1-200303272130/swt-2.1-win32.zip

<o:p> </o:p>

       3.用于编译exelibswt.a文件(我生成了一个,在这里下载)

       http://www.elvala.com/download/java/libswt.rar

<o:p> </o:p>

       开始<o:p></o:p>

       我们使用的是eclipse的例子程序,源代码如下:

<o:p> </o:p>

import org.eclipse.swt.*;<o:p></o:p>

import org.eclipse.swt.widgets.*;<o:p></o:p>

import org.eclipse.swt.layout.*;<o:p></o:p>

import org.eclipse.swt.events.*;<o:p></o:p>

import org.eclipse.swt.graphics.*;<o:p></o:p>

 <o:p></o:p>

public class ComplexGridLayoutExample {<o:p></o:p>

   static Display display;<o:p></o:p>

   static Shell shell;<o:p></o:p>

   static Text dogName;<o:p></o:p>

   static Combo dogBreed;<o:p></o:p>

   static Canvas dogPhoto;<o:p></o:p>

   static Image dogImage;<o:p></o:p>

   static List categories;<o:p></o:p>

   static Text ownerName;<o:p></o:p>

   static Text ownerPhone;<o:p></o:p>

 <o:p></o:p>

   public static void main(String[] args) {<o:p></o:p>

       display = new Display();<o:p></o:p>

       shell = new Shell(display);<o:p></o:p>

       shell.setText("Dog Show Entry");<o:p></o:p>

       GridLayout gridLayout = new GridLayout();<o:p></o:p>

       gridLayout.numColumns = 3;<o:p></o:p>

       shell.setLayout(gridLayout);<o:p></o:p>

     <o:p></o:p>

       new Label(shell, SWT.NONE).setText("Dog's Name:");<o:p></o:p>

       dogName = new Text(shell, SWT.SINGLE | SWT.BORDER);<o:p></o:p>

       GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);<o:p></o:p>

       gridData.horizontalSpan = 2;<o:p></o:p>

       dogName.setLayoutData(gridData);<o:p></o:p>

 <o:p></o:p>

       new Label(shell, SWT.NONE).setText("Breed:");<o:p></o:p>

       dogBreed = new Combo(shell, SWT.NONE);<o:p></o:p>

       dogBreed.setItems(new String [] {"Collie", "Pitbull", "Poodle", "Scottie"});<o:p></o:p>

       dogBreed.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));<o:p></o:p>

 <o:p></o:p>

       Label label = new Label(shell, SWT.NONE);<o:p></o:p>

       label.setText("Categories");<o:p></o:p>

       label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));<o:p></o:p>

      <o:p></o:p>

       new Label(shell, SWT.NONE).setText("Photo:");<o:p></o:p>

       dogPhoto = new Canvas(shell, SWT.BORDER);<o:p></o:p>

       gridData = new GridData(GridData.FILL_BOTH);<o:p></o:p>

       gridData.widthHint = 80;<o:p></o:p>

       gridData.heightHint = 80;<o:p></o:p>

       gridData.verticalSpan = 3;<o:p></o:p>

       dogPhoto.setLayoutData(gridData);<o:p></o:p>

       dogPhoto.addPaintListener(new PaintListener() {<o:p></o:p>

          public void paintControl(final PaintEvent event) {<o:p></o:p>

              if (dogImage != null) {<o:p></o:p>

                 event.gc.drawImage(dogImage, 0, 0);<o:p></o:p>

              }<o:p></o:p>

          }<o:p></o:p>

       });<o:p></o:p>

 <o:p></o:p>

       categories = new List(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);<o:p></o:p>

       categories.setItems(new String [] {<o:p></o:p>

          "Best of Breed", "Prettiest Female", "Handsomest Male",<o:p></o:p>

          "Best Dressed", "Fluffiest Ears", "Most Colors",<o:p></o:p>

          "Best Performer", "Loudest Bark", "Best Behaved",<o:p></o:p>

          "Prettiest Eyes", "Most Hair", "Longest Tail",<o:p></o:p>

          "Cutest Trick"});<o:p></o:p>

       gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);<o:p></o:p>

       gridData.verticalSpan = 4;<o:p></o:p>

       int listHeight = categories.getItemHeight() * 12;<o:p></o:p>

       Rectangle trim = categories.computeTrim(0, 0, 0, listHeight);<o:p></o:p>

       gridData.heightHint = trim.height;<o:p></o:p>

       categories.setLayoutData(gridData);<o:p></o:p>

      <o:p></o:p>

       Button browse = new Button(shell, SWT.PUSH);<o:p></o:p>

       browse.setText("Browse...");<o:p></o:p>

       gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);<o:p></o:p>

       gridData.horizontalIndent = 5;<o:p></o:p>

       browse.setLayoutData(gridData);<o:p></o:p>

       browse.addSelectionListener(new SelectionAdapter() {<o:p></o:p>

          public void widgetSelected(SelectionEvent event) {<o:p></o:p>

              String fileName = new FileDialog(shell).open();<o:p></o:p>

              if (fileName != null) {<o:p></o:p>

                 dogImage = new Image(display, fileName);<o:p></o:p>

              }<o:p></o:p>

          }<o:p></o:p>

       });<o:p></o:p>

      <o:p></o:p>

       Button delete = new Button(shell, SWT.PUSH);<o:p></o:p>

       delete.setText("Delete");<o:p></o:p>

       gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);<o:p></o:p>

       gridData.horizontalIndent = 5;<o:p></o:p>

       delete.setLayoutData(gridData);<o:p></o:p>

       delete.addSelectionListener(new SelectionAdapter() {<o:p></o:p>

          public void widgetSelected(SelectionEvent event) {<o:p></o:p>

              if (dogImage != null) {<o:p></o:p>

                 dogImage.dispose();<o:p></o:p>

                 dogImage = null;<o:p></o:p>

                 dogPhoto.redraw();<o:p></o:p>

              }<o:p></o:p>

          }<o:p></o:p>

       });<o:p></o:p>

      <o:p></o:p>

       Group ownerInfo = new Group(shell, SWT.NONE);<o:p></o:p>

       ownerInfo.setText("Owner Info");<o:p></o:p>

       gridLayout = new GridLayout();<o:p></o:p>

       gridLayout.numColumns = 2;<o:p></o:p>

       ownerInfo.setLayout(gridLayout);<o:p></o:p>

       gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);<o:p></o:p>

       gridData.horizontalSpan = 2;<o:p></o:p>

       ownerInfo.setLayoutData(gridData);<o:p></o:p>

      <o:p></o:p>

       new Label(ownerInfo, SWT.NONE).setText("Name:");<o:p></o:p>

       ownerName = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);<o:p></o:p>

       ownerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));<o:p></o:p>

      <o:p></o:p>

       new Label(ownerInfo, SWT.NONE).setText("Phone:");<o:p></o:p>

       ownerPhone = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);<o:p></o:p>

       ownerPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));<o:p></o:p>

      <o:p></o:p>

       Button enter = new Button(shell, SWT.PUSH);<o:p></o:p>

       enter.setText("Enter");<o:p></o:p>

       gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);<o:p></o:p>

       gridData.horizontalSpan = 3;<o:p></o:p>

       enter.setLayoutData(gridData);<o:p></o:p>

       enter.addSelectionListener(new SelectionAdapter() {<o:p></o:p>

          public void widgetSelected(SelectionEvent event) {<o:p></o:p>

              System.out.println("\nDog Name: " + dogName.getText());<o:p></o:p>

              System.out.println("Dog Breed: " + dogBreed.getText());<o:p></o:p>

              System.out.println("Owner Name: " + ownerName.getText());<o:p></o:p>

              System.out.println("Owner Phone: " + ownerPhone.getText());<o:p></o:p>

              System.out.println("Categories:");<o:p></o:p>

              String cats[] = categories.getSelection();<o:p></o:p>

              for (int i = 0; i < cats.length; i++) {<o:p></o:p>

                 System.out.println("\t" + cats[i]);<o:p></o:p>

              }<o:p></o:p>

          }<o:p></o:p>

       });<o:p></o:p>

      <o:p></o:p>

       shell.pack();<o:p></o:p>

       shell.open();<o:p></o:p>

       while (!shell.isDisposed()) {<o:p></o:p>

          if (!display.readAndDispatch()) display.sleep();<o:p></o:p>

       }<o:p></o:p>

       if (dogImage != null) {<o:p></o:p>

          dogImage.dispose();<o:p></o:p>

       }<o:p></o:p>

   }<o:p></o:p>

}<o:p></o:p>

       <o:p></o:p>

<o:p> </o:p>

       文件存为ComplexGridLayoutExample.java<o:p></o:p>

       对应的Makefile 文件

<o:p> </o:p>

TARGET=ComplexGridLayoutExample<o:p></o:p>

GCJ=gcj<o:p></o:p>

JFLAGS=--classpath=./swt.jar<o:p></o:p>

LFLAGS=-L. -lswt<o:p></o:p>

<o:p> </o:p>

all: $(TARGET)<o:p></o:p>

<o:p> </o:p>

$(TARGET): $(TARGET).o<o:p></o:p>

       $(GCJ) --main=$(TARGET) -o $(TARGET) $(TARGET).o $(LFLAGS)<o:p></o:p>

<o:p> </o:p>

$(TARGET).o: $(TARGET).java<o:p></o:p>

       $(GCJ) $(JFLAGS) -c $(TARGET).java -o $(TARGET).o<o:p></o:p>

.PHONY: clean<o:p></o:p>

<o:p> </o:p>

clean:<o:p></o:p>

       rm -rf *.o $(TARGET)<o:p></o:p>

<o:p> </o:p>

       上面两个文件和swt.jar,libswt.a,swt-win32-2133.dll 放在一起,

       关于路径问题注意

       JFLAGS=--classpath=./swt.jar<o:p></o:p>

LFLAGS=-L. –lswt<o:p></o:p>

其中JFLAGS要对应swt.jar

LFLAGS要对应libswt.a

<o:p> </o:p>

然后make!

运行生成的ComplexGridLayoutExample.exe

看到了吗?以后运行此程序只要和swt-win32-2133.dll在一起。

整个exe大概5兆多,似乎有些让人沮丧,太大啦,但是这东西的确很奇妙,

据说C# editor:SharpStudio将使用SWT来制作!SWT封装了很多功能,你会发现写应用程序会比使用Swing等要方便很多。

 

直接下载编译好的例子 ComplexGridLayoutExample.exe

http://www.elvala.com/download/java/gcj.rar 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值