问题描述
我试图将一个exe文件包含在jar-Application中并运行它。我们的想法是首先将其临时提取,然后运行temp-exe文件。怎么做?这就是我尝试过的。有我的代码。由于源文件ffmpeg.exe,它发生异常java.io.FileNotFoundException。我验证了,但文件已包含且目录正确。
I tried to to include an exe-file into a jar-Application and to run it. The idea is to extract it temporary at first and then to run the temp-exe-file. How to do that? That is what I have tried. There is my code. It occurs the exception java.io.FileNotFoundException because of the source file "ffmpeg.exe". I verified, but the file is included and the directory is correct.
package extractffmpeg;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.apache.commons.io.FileUtils;
public class ExtractFFmpeg extends Application {
public void start(Stage primaryStage) throws IOException, URISyntaxException {
extractExe();
System.out.println("extract successfull");
Platform.exit();
}
public static void main(String[] args) {
launch(args);
}
public void extractExe() throws URISyntaxException, IOException{
final String resourcesPath = "ffmpeg/ffmpeg.exe";
URL url = ExtractFFmpeg.class.getResource(resourcesPath);
File source = new File(url.toString());
System.out.println("shows url of ffmpeg: " + url.getPath());
System.out.println("shows file of ffmpeg: " + source);
File destination = new File("C:/Users/FNI2Abt/Desktop/ffmpeg.exe");
FileUtils.copyFile(source, destination);
}
}
推荐答案
想法是创建一个自解压存档。存档应包含JAR和EXE。 JAR文件应包含一个类,该类将在相邻的EXE上调用Process.exec(...)。
从那里开始,你可以按照这个教程:
The idea is to create a self-extracting archive. The archive shall contain both JAR and EXE. The JAR file shall contain a class which would call Process.exec(...) on the adjacent EXE.
Starting there, you can follow this tutorial:How do I make a self extract and running installer
这篇关于在jar中包含一个exe文件并运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!