java 代码执行jar
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
无聊我就学英语

浙公网安备 33010602011771号