一般来说使用JSmooth可以把纯Java程序封装成windows的可执行文件。
如果你做的是SWT应用程序,那得需要写个Lancher来加载一些本地文件然后启动程序,下面是我在具体项目中写的样例代码,你可以参考一下:
CODE:
public class Lanucher {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
String homePath = null;
if (args.length > 0) {
homePath = args[0];
}
if (homePath == null) {
homePath = System.getProperty("user.dir");
}
if (homePath == null) {
//如果主目录信息不存在, 则抛出错误, 然后关闭程序.
System.err.println("错误:系统没有指定主目录路径, 请联系系统管理员.");
execute(runtime, "pause");
} else {
System.out.println("1.使用主目录:" + homePath);
//启动应用程序.
String libPath = homePath + "/lib";
File libFile = new File(libPath);
File[] jarFiles = libFile.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar") || name.endsWith(".JAR");
}
});
String classpath = libPath;
if (jarFiles != null) {
for (int i = 0; i < jarFiles.length; i++) {
String jarFilePath = jarFiles.getPath();
System.out.println("2.添加库文件:" + jarFilePath);
classpath += ";" + jarFilePath;
}
}
String execute = ("\"" +
homePath + "/jre/bin/java\" -classpath \"" +
classpath + "\" -Djava.library.path=\"" +
libPath + "\" com.yipsilon.lifestyle.Application " +
homePath).replace('\\', '/');
System.out.println("3.执行程序:" + execute);
execute(runtime, execute);
}
}
private static void execute(Runtime runtime, String command) {
try {
runtime.exec(command);
} catch (IOException ioe) {
//如果主目录信息不存在, 则抛出错误, 然后关闭程序.
System.err.println("错误:执行主程序时出现错误.\n\n" + ioe.getMessage());
try {
runtime.exec("pause");
} catch (IOException ignore) {
}
}
}
}