1-) Java - Uygulamayı restart etmek. Java My Application Restart
1-) Java - İlk açılan uygulamanın PID(Process ID'sini bir yere yazarız). RM_OCR.jar uygulamanın ADI
public void MyIsRunning() {
try {
String line;
Process p = Runtime.getRuntime().exec("jcmd -l");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if ((line.contains("RM_OCR.jar")) && (line.contains(" "))) {
System.out.println("RM_OCR çalışıyor. O halde restart edelim");
String PID = line.substring(0, line.indexOf(" "));
String cmd = "taskkill /F /PID " + PID;
System.out.println("PID = " + PID);
System.out.println("CMD = " + cmd);
System.out.println("pidInfo = " + line);
String restart = getJarPath() + "\\ayarlar\\restart.txt";
PrintWriter writer = new PrintWriter(restart, "UTF-8");
writer.println(PID);
// writer.println("The second line");
writer.close();
// Runtime.getRuntime().exec(cmd);
}
}
input.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "HATA! MyIsRunning()" + ex.getMessage());
}
}
2-) txt'ye yazılan PID değerini TaskKill Yaparız.
public void MyTaskKill() {
try {
String line;
Process p = Runtime.getRuntime().exec("jcmd -l");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String kayitli_PID = MyGetTxt(getJarPath() + "\\ayarlar\\restart.txt").replace("\n", "");
while ((line = input.readLine()) != null) {
if (line.contains(kayitli_PID)) {
System.out.println("Sonlandırılıyor...");
String cmd = "taskkill /F /PID " + kayitli_PID;
Runtime.getRuntime().exec(cmd);
}
}
input.close();
} catch (Exception ex) {
System.out.println("HATA! MyTaskKill()" + ex.getMessage());
}
}
3-) Yardımcı txt Reader
public String getJarPath() { // JAR'IN BULUNDUĞU PATH'İ VERİR
try {
String path = RM_OCR.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File file = new File(path);
String grandParent = file.getParentFile().getParent(); // JAR'IN BULUNDUĞU PATH'İN BİR ÜST PATH'İNİ VERİR
//String grandParent = file.getParentFile().getPath(); // ----- bunu açacağım yukarısındakini yorum satırı yapacagım -> JAR'IN BULUNDUĞU PATH'İ VERİR
grandParent = URLDecoder.decode(grandParent, "UTF-8");
return grandParent;
} catch (Exception ex) {
JOptionPane.showMessageDialog(rootPane, "HATA! -> " + ex.getMessage());
return "C:\\RM_OCR_TXT";
}
}
public String MyGetTxt(String path) { // PATH'İ VERİLEN TXT'Yİ OKUR VE STRİNG OLARAK DÖNDÜRÜR
try {
BufferedReader in = new BufferedReader(new FileReader(path));
String line;
while ((line = in.readLine()) != null) {
// System.out.println(line);
path = line + "\n";
}
in.close();
return path;
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
return "C:\\RM_OCR_TXT";
}
}