java 程式碼執行jar
阿新 • • 發佈:2022-04-02
1.程式碼:
public static void cmdRunJar(String jarPath, int port) { String cmd = "nohup java -jar " + jarPath + " --server.port=" + port; BufferedReader br = null; try { Process process = Runtime.getRuntime().exec(cmd); InputStream is = process.getInputStream(); br = new BufferedReader(new InputStreamReader(is,"GBK")); StringBuffer sb = new StringBuffer(); sb.append(br.readLine()); String content; while ((content = br.readLine()) != null) { content = br.readLine(); sb.append(content); } br.close(); br = null; log.info(content); } catch (IOException e) { e.printStackTrace(); } finally { if(null!=br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void cmdTerminateJar(int port) { String os = System.getProperties().getProperty("os.name"); String path = ClassUtils.getDefaultClassLoader().getResource("").getPath(); String cmd = null; if(os.contains("win")) { cmd = path+"stopJar.bat "+ port; } else { cmd = path + "" + port; } BufferedReader br = null; try { Process process = Runtime.getRuntime().exec(cmd); InputStream is = process.getInputStream(); br = new BufferedReader(new InputStreamReader(is,"GBK")); StringBuffer sb = new StringBuffer(); sb.append(br.readLine()); String content; while ((content = br.readLine()) != null) { content = br.readLine(); sb.append(content); } br.close(); br = null; log.info(content); } catch (IOException e) { e.printStackTrace(); } finally { if(null!=br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2.指令碼: stopJar.bat
@echo off for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%1"') do taskkill /f /pid %%m