MCPcopy Index your code
hub / github.com/benfry/processing4 / restartJavaApplication

Method restartJavaApplication

app/src/processing/app/Platform.java:434–515  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

432 // Better to just re-launch the .exe instead.
433 // Hacked up from <a href="https://lewisleo.blogspot.com/2012/08/programmatically-restart-java.html">this code</a>.
434 static private void restartJavaApplication() {
435 // System.out.println("java path: " + javaPath);
436// String java = System.getProperty("java.home") + "/bin/java";
437 // Tested and working with JDK 17 [fry 230122]
438// System.out.println("sun java command: " + System.getProperty("sun.java.command"));
439// System.out.println("class path: " + System.getProperty("java.class.path"));
440 List<String> cmd = new ArrayList<>();
441
442 // Add the path to the current java binary
443 cmd.add(getJavaPath());
444
445 // Get all the VM arguments that are currently in use
446 List<String> vmArguments =
447 ManagementFactory.getRuntimeMXBean().getInputArguments();
448
449 // Add all the arguments we're using now, except for -agentlib
450 for (String arg : vmArguments) {
451 if (!arg.contains("-agentlib")) {
452 cmd.add(arg);
453 }
454 }
455
456 // Does not work for .jar files, should this be used in a more general way
457 cmd.add("-cp");
458 cmd.add(System.getProperty("java.class.path"));
459
460 // Finally, add the class that was used to launch the app
461 // (in our case, this is the Processing splash screen)
462 String javaCommand = System.getProperty("sun.java.command");
463 String[] splitCommand = PApplet.split(javaCommand, ' ');
464// if (splitCommand.length > 1) {
465// try {
466// Util.saveFile(javaCommand, PApplet.desktopFile("arrrrrghs.txt"));
467// } catch (IOException e) {
468// throw new RuntimeException(e);
469// }
470// }
471 cmd.add(splitCommand[0]); // should be the main class name
472
473 ProcessBuilder builder = new ProcessBuilder(cmd);
474
475 /*
476 StringBuffer vmArgsOneLine = new StringBuffer();
477 for (String arg : vmArguments) {
478 // if it's the agent argument : we ignore it otherwise the
479 // address of the old application and the new one will be in conflict
480 if (!arg.contains("-agentlib")) {
481 vmArgsOneLine.append(arg);
482 vmArgsOneLine.append(" ");
483 }
484 }
485 // init the command to execute, add the vm args
486 final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);
487 // program main and program arguments (be careful a sun property. might not be supported by all JVM)
488 String[] mainCommand = System.getProperty("sun.java.command").split(" ");
489 // program main is a jar
490 if (mainCommand[0].endsWith(".jar")) {
491 // if it's a jar, add -jar mainJar

Callers

nothing calls this directly

Calls 7

getJavaPathMethod · 0.95
splitMethod · 0.95
addMethod · 0.45
containsMethod · 0.45
startMethod · 0.45
printStackTraceMethod · 0.45
exitMethod · 0.45

Tested by

no test coverage detected