Java程序启动同时复制resources下文件到jar包同级目录
生活随笔
收集整理的這篇文章主要介紹了
Java程序启动同时复制resources下文件到jar包同级目录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java代碼調用.exe,包括獲取exec()中的日志并打印,可以看我之前的博客
一、Java調用exe打包成jar的調用方式有倆種:
Java程序中resources下的exe等文件,是可以同時打包到Jar中
1. resources下放置exe 直接獲取exe的絕對路徑調用
2. resources下放置exe copyExe到Jar包同級目錄后調用
二、源代碼
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;import java.io.*;@Slf4j
@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {public static void main(String[] args) {SpringApplication.run(SpringBootApp.class, args);}@Overridepublic void run(String... args) throws Exception {Options options = new Options();options.addOption("param", true, "exe params");CommandLineParser parser = new BasicParser();try {CommandLine command = parser.parse(options, args);if (!command.hasOption("param")) {log.error("dir is not provided");return;}String param = command.getOptionValue("param");// 法一 resources下放置exe 直接獲取exe的絕對路徑調用callExe2(param);// 法二 resources下放置exe copyExe到Jar包同級目錄后調用copyAndCallExe(param);} catch (ParseException e) {log.error("command line error,", e);}}/*** 添加拷貝exe及依賴dll文件*/private void copyExeToJarDir(String folderName) {// 初始化轉換工具類exe的文件夾copyFile(folderName, "readMe.txt");copyFile(folderName, "test.exe");log.info("-----------copy copyExeToJarDir endl-----------");}/*** 拷貝資源文件** @param folderName 文件目錄文件夾* @param fileName 要拷貝的exe或者dll* @return*/private boolean copyFile(String folderName, String fileName) {File libFolder = new File(folderName);if (!libFolder.exists()) {libFolder.mkdir();}File libFile = new File(libFolder, fileName);
// log.info("copy resource:" + folderName + fileName + "|" + libFile.exists());if (!libFile.exists()) {try {ClassPathResource resource = new ClassPathResource(fileName);InputStream in = resource.getInputStream();FileUtils.copyInputStreamToFile(in, libFile);in.close();} catch (Exception e) {e.printStackTrace();return false;}}return true;}public void copyAndCallExe(String param) throws IOException {String folderName = System.getProperty("user.dir") + File.separator + "lib" + File.separator;// 添加拷貝exe及依賴dll文件到jar包同級目錄copyExeToJarDir(folderName);// user.dir指定了當前的路徑String path = folderName + "test.exe";// 參數1 exe的絕對路徑 參數2 工程文件目錄log.info("command: {}", path + " " + File.separator + param);ProcessBuilder processBuilder = new ProcessBuilder(path, param);processBuilder.redirectErrorStream(true);Process process = processBuilder.start();StringBuilder processOutput = new StringBuilder();try (BufferedReader processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));) {String readLine;while ((readLine = processOutputReader.readLine()) != null) {processOutput.append(readLine + System.lineSeparator());}process.waitFor();} catch (IOException e) {e.printStackTrace();log.error(e.getMessage());} catch (InterruptedException e) {e.printStackTrace();log.error(e.getMessage());} finally {if (process != null) {process.destroy();}}log.info("callExe res: {}", processOutput.toString().trim());}public void callExe2(String param) throws IOException {// user.dir指定了當前的路徑String path = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "test.exe";// 參數1 exe的絕對路徑 參數2 工程文件目錄log.info("command: {}", path + " " + File.separator + param);ProcessBuilder processBuilder = new ProcessBuilder(path, param);processBuilder.redirectErrorStream(true);Process process = processBuilder.start();StringBuilder processOutput = new StringBuilder();try (BufferedReader processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));) {String readLine;while ((readLine = processOutputReader.readLine()) != null) {processOutput.append(readLine + System.lineSeparator());}process.waitFor();} catch (IOException e) {e.printStackTrace();log.error(e.getMessage());} catch (InterruptedException e) {e.printStackTrace();log.error(e.getMessage());} finally {if (process != null) {process.destroy();}}log.info("callExe res: {}", processOutput.toString().trim());}
}
總結
以上是生活随笔為你收集整理的Java程序启动同时复制resources下文件到jar包同级目录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Mysql】日期、行变列(IF、CAS
- 下一篇: Java CSV文件读取、写入及追加