要将exe文件包含到项目中,请通过文件系统将此exe文件复制到
src
Netbeans项目的文件夹。
构建项目后,这个exe文件将打包到项目jar文件中。
在运行时,您需要
to extract this exe file from your jar file
.
当提取这个exe文件时,您可以执行它。
要从java代码启动外部应用程序,我建议使用Apache Commons Exec:
http://commons.apache.org/proper/commons-exec/
更新
下面的示例类演示如何从当前运行的jar文件中提取所有exe文件。我使用这些SO帖子来制作这个类:
the first
和
the second
一个。
import java.io.File;
import java.io.IOException;
/**
*
*/
public class TestClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
extractExeFiles("C://Temp");
}
/**
* Gets running jar file path.
* @return running jar file path.
*/
private static File getCurrentJarFilePath() {
return new File(TestClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
}
/**
* Extracts all exe files to the destination directory.
* @param destDir destination directory.
* @throws IOException if there's an i/o problem.
*/
private static void extractExeFiles(String destDir) throws IOException {
java.util.jar.JarFile jar = new java.util.jar.JarFile(getCurrentJarFilePath());
java.util.Enumeration enumEntries = jar.entries();
String entryName;
while (enumEntries.hasMoreElements()) {
java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
entryName = file.getName();
if ( (entryName != null) && (entryName.endsWith(".exe"))) {
java.io.File f = new java.io.File(destDir + java.io.File.separator + entryName);
if (file.isDirectory()) { // if its a directory, create it
f.mkdir();
continue;
}
java.io.InputStream is = jar.getInputStream(file); // get the input stream
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
while (is.available() > 0) { // write contents of 'is' to 'fos'
fos.write(is.read());
}
fos.close();
is.close();
}
}
}
}