GCJ可以把JAVA程序编译成本地代码,编译成功后的exe文件不再需要JRE就可直接运行,编译成本地后的程序运行速度有所提高。缺点是生成后的文件较大,我试了一个hello world都要两兆多,用WinRAR压缩后为600多K。
安装及配置
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.exe、MSYS-1.0.8.exe
安装MSYS时会有提示要你输入MinGW的路径。
解压缩gcj-3.2-20021210-1.tar.gz将里面的文件复制到MinGW下,注意对应文件夹,比如gcj\bin下文件要放到对应的MinGW\bin下面。
3. 设置Windows的PATH把MinGW\bin加到系统的PATH里,方便以后运行gcj
然后在命令行里输入gcj –v 看看吧:)
HelloGCJ.exe为了方便描述,让我们先建立一个gcjwork文件夹,
在gcjwork中建立一个普通的java源文件,HelloGCJ.java
public class HelloGCJ {
public static void main(String[] args) {
System.out.println("Hello GCJ!");
}
}
同目录下新建一个文本文件Makefile注意不要带扩展名。
TARGET=HelloGCJ
GCJ=gcj
all: $(TARGET)
$(TARGET): $(TARGET).o
$(GCJ) --main=$(TARGET) -o $(TARGET) $(TARGET).o
$(TARGET).o: $(TARGET).java
$(GCJ) -c $(TARGET).java -o $(TARGET).o
.PHONY: clean
clean:
rm -rf *.o $(TARGET)
然后让我们打开MSYS使用cd进入到我们建立的gcjwork下,输入make
之后即可生成一个HelloGCJ.exe,运行他,这可是一个没有用虚拟机的用java写的程序哦。:)
对比一下
为什么要把java便以成本地代码呢?
执行速度加快是一个很主要的特点,让我们做一个最最简单的对比。
还是上例的HelloGCJ,我们编译成java的bytecode
注意gcj也可以编译bytecode,
使用gcj –C HelloGCJ.java 命令即可。
然后对比运行两个程序,在我的机器上可以明显感觉到HelloGCJ.class在执行时顿了一下。
而HelloGCJ.exe则直接相应输出了文字。
再来搞搞SWT
用过Eclipse就知道SWT,SWT是IBM的图形界面开发包,因为它的实现也是与本地化有关,所以引来不少的争议,公说公有理婆说婆有理,我们就不说什么了,直接说怎么使用它,毕竟是好是坏,用了才知道!
准备
1.swt.jat (SWT的java包,编译java要用)
2.SWT的windows版本对应的dll(我的的文件名是swt-win32-2133.dll)
上面这两个文件可以在eclipse下载最新的SWT工具包
http://download2.eclipse.org/downloads/drops/R-2.1-200303272130/swt-2.1-win32.zip
3.用于编译exe的libswt.a文件(我生成了一个,在这里下载)
http://www.elvala.com/download/java/libswt.rar
开始
我们使用的是eclipse的例子程序,源代码如下:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
public class ComplexGridLayoutExample {
static Display display;
static Shell shell;
static Text dogName;
static Combo dogBreed;
static Canvas dogPhoto;
static Image dogImage;
static List categories;
static Text ownerName;
static Text ownerPhone;
public static void main(String[] args) {
display = new Display();
shell = new Shell(display);
shell.setText("Dog Show Entry");
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);
new Label(shell, SWT.NONE).setText("Dog's Name:");
dogName = new Text(shell, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
dogName.setLayoutData(gridData);
new Label(shell, SWT.NONE).setText("Breed:");
dogBreed = new Combo(shell, SWT.NONE);
dogBreed.setItems(new String [] {"Collie", "Pitbull", "Poodle", "Scottie"});
dogBreed.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
Label label = new Label(shell, SWT.NONE);
label.setText("Categories");
label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
new Label(shell, SWT.NONE).setText("Photo:");
dogPhoto = new Canvas(shell, SWT.BORDER);
gridData = new GridData(GridData.FILL_BOTH);
gridData.widthHint = 80;
gridData.heightHint = 80;
gridData.verticalSpan = 3;
dogPhoto.setLayoutData(gridData);
dogPhoto.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent event) {
if (dogImage != null) {
event.gc.drawImage(dogImage, 0, 0);
}
}
});
categories = new List(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
categories.setItems(new String [] {
"Best of Breed", "Prettiest Female", "Handsomest Male",
"Best Dressed", "Fluffiest Ears", "Most Colors",
"Best Performer", "Loudest Bark", "Best Behaved",
"Prettiest Eyes", "Most Hair", "Longest Tail",
"Cutest Trick"});
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData.verticalSpan = 4;
int listHeight = categories.getItemHeight() * 12;
Rectangle trim = categories.computeTrim(0, 0, 0, listHeight);
gridData.heightHint = trim.height;
categories.setLayoutData(gridData);
Button browse = new Button(shell, SWT.PUSH);
browse.setText("Browse...");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalIndent = 5;
browse.setLayoutData(gridData);
browse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
String fileName = new FileDialog(shell).open();
if (fileName != null) {
dogImage = new Image(display, fileName);
}
}
});
Button delete = new Button(shell, SWT.PUSH);
delete.setText("Delete");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
gridData.horizontalIndent = 5;
delete.setLayoutData(gridData);
delete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (dogImage != null) {
dogImage.dispose();
dogImage = null;
dogPhoto.redraw();
}
}
});
Group ownerInfo = new Group(shell, SWT.NONE);
ownerInfo.setText("Owner Info");
gridLayout = new GridLayout();
gridLayout.numColumns = 2;
ownerInfo.setLayout(gridLayout);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
ownerInfo.setLayoutData(gridData);
new Label(ownerInfo, SWT.NONE).setText("Name:");
ownerName = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(ownerInfo, SWT.NONE).setText("Phone:");
ownerPhone = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button enter = new Button(shell, SWT.PUSH);
enter.setText("Enter");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
gridData.horizontalSpan = 3;
enter.setLayoutData(gridData);
enter.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
System.out.println("\nDog Name: " + dogName.getText());
System.out.println("Dog Breed: " + dogBreed.getText());
System.out.println("Owner Name: " + ownerName.getText());
System.out.println("Owner Phone: " + ownerPhone.getText());
System.out.println("Categories:");
String cats[] = categories.getSelection();
for (int i = 0; i < cats.length; i++) {
System.out.println("\t" + cats[i]);
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
if (dogImage != null) {
dogImage.dispose();
}
}
}
文件存为ComplexGridLayoutExample.java
对应的Makefile 文件
TARGET=ComplexGridLayoutExample
GCJ=gcj
JFLAGS=--classpath=./swt.jar
LFLAGS=-L. -lswt
all: $(TARGET)
$(TARGET): $(TARGET).o
$(GCJ) --main=$(TARGET) -o $(TARGET) $(TARGET).o $(LFLAGS)
$(TARGET).o: $(TARGET).java
$(GCJ) $(JFLAGS) -c $(TARGET).java -o $(TARGET).o
.PHONY: clean
clean:
rm -rf *.o $(TARGET)
上面两个文件和swt.jar,libswt.a,swt-win32-2133.dll 放在一起,
关于路径问题注意
JFLAGS=--classpath=./swt.jar
LFLAGS=-L. –lswt
其中JFLAGS要对应swt.jar
LFLAGS要对应libswt.a
然后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
本文地址:http://com.8s8s.com/it/it17469.htm