24 Ağustos 2021 Salı

Process Sınıfı

Giriş
Şu satırı dahil deriz
import java.lang.Process;
getInputStream metodu
Örnek
Şöyle yaparız
boolean executeOpenSslCmdProcess(String host) {

  StringBuffer command = new StringBuffer();
  command.append("openssl x509 -req -in /tmp/ppserver.csr -out /tmp/ppserver.crt -sha256 "
  +
" -CA /tmp/powerpath-CA.crt -CAkey /tmp/powerpath-CA.key -CAcreateserial -days 821"
); String[] cmd = { "/bin/sh", "-c", command.toString() }; try { Process process = Runtime.getRuntime().exec(cmd); InputStream inputStream = process.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, CHAR_SET)); String result = br.lines().collect(Collectors.joining(System.lineSeparator())); exitCode = process.waitFor(); logger.info("ExitCode : {} ", exitCode); if (exitCode != 0) { return false; } } catch (IOException | InterruptedException ex) { logger.info("IOException - " + ex); return false; } }
pid metodu
Eskiden şöyle yapardık
static String processPid(Process process) {
  try {
    return Process.class.getMethod("pid").invoke(process).toString();
  } catch (Exception e) {
    return process.toString().replaceFirst("^.*pid=(\\d+).*$", "$1");
  }
}
Şimdi şöyle yapıyoruz
String processPid(Process process) {
  try {
    return String.valueOf(process.pid());
  } catch (Exception e) {
    return process.toString().replaceFirst("^.*pid=(\\d+).*$", "$1");
  }
}


Hiç yorum yok:

Yorum Gönder