Java ProcessBuilder add JVM, module, class, main class path

Description

Java ProcessBuilder add JVM, module, class, main class path


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    long sleepInterval = 5;
    long sleepDuration = 60;

    long pid = ProcessHandle.current().pid();
    System.out.printf("Job (pid=%d) info: Sleep Interval" + "=%d seconds, Sleep Duration=%d " + "seconds.%n", pid,
        sleepInterval, sleepDuration);/*ww  w.java2  s . com*/

    for (long sleptFor = 0; sleptFor < sleepDuration; sleptFor += sleepInterval) {
      try {
        System.out.printf("Job (pid=%d) is going to" + " sleep for %d seconds.%n", pid, sleepInterval);
        // Sleep for the sleep interval
        TimeUnit.SECONDS.sleep(sleepInterval);

      } catch (InterruptedException ex) {
        System.out.printf("Job (pid=%d) was " + "interrupted.%n", pid);
      }
    }
  }

  /**
   * Starts a new JVM to run the Job class.
   */
  public static Process startProcess(long sleepInterval, long sleepDuration) {
    List<String> cmd = new ArrayList<>();

    addJvmPath(cmd);
    addModulePath(cmd);
    addClassPath(cmd);
    addMainClass(cmd);

    cmd.add(String.valueOf(sleepInterval));
    cmd.add(String.valueOf(sleepDuration));

    ProcessBuilder pb = new ProcessBuilder().command(cmd).inheritIO();
    String commandLine = pb.command().stream().collect(Collectors.joining(" "));
    System.out.println("Command used:\n" + commandLine);

    Process p = null;
    try {
      p = pb.start();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return p;
  }

  private static void addJvmPath(List<String> cmd) {
    String jvmPath = ProcessHandle.current().info().command().orElse("");
    if (jvmPath.length() > 0) {
      cmd.add(jvmPath);
    } else {
      // Try composing the JVM path using the java.home system property
      final String FILE_SEPARATOR = System.getProperty("file.separator");
      jvmPath = System.getProperty("java.home") + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "java";
      cmd.add(jvmPath);
    }
  }

  private static void addModulePath(List<String> cmd) {
    String modulePath = System.getProperty("jdk.module.path");
    if (modulePath != null && modulePath.trim().length() > 0) {
      cmd.add("--module-path");
      cmd.add(modulePath);
    }
  }

  private static void addClassPath(List<String> cmd) {
    String classPath = System.getProperty("java.class.path");
    if (classPath != null && classPath.trim().length() > 0) {
      cmd.add("--class-path");
      cmd.add(classPath);
    }
  }

  private static void addMainClass(List<String> cmd) {
    Class<Main> cls = Main.class;
    String className = cls.getName();
    Module module = cls.getModule();
    if (module.isNamed()) {
      String moduleName = module.getName();
      cmd.add("--module");
      cmd.add(moduleName + "/" + className);
    } else {
      cmd.add(className);
    }
  }
}



PreviousNext

Related