|
3 | 3 | /* |
4 | 4 | Part of the Processing project - http://processing.org |
5 | 5 |
|
6 | | - Copyright (c) 2012-20 The Processing Foundation |
| 6 | + Copyright (c) 2012-23 The Processing Foundation |
7 | 7 | Copyright (c) 2008-12 Ben Fry and Casey Reas |
8 | 8 |
|
9 | 9 | This program is free software; you can redistribute it and/or modify |
|
25 | 25 |
|
26 | 26 | import java.io.File; |
27 | 27 | import java.io.IOException; |
| 28 | +import java.lang.management.ManagementFactory; |
28 | 29 | import java.net.URISyntaxException; |
29 | 30 | import java.net.URL; |
| 31 | +import java.util.ArrayList; |
30 | 32 | import java.util.HashMap; |
| 33 | +import java.util.List; |
31 | 34 | import java.util.Map; |
32 | 35 |
|
33 | 36 | import com.sun.jna.platform.FileUtils; |
@@ -404,6 +407,114 @@ static public String getJavaPath() { |
404 | 407 | } |
405 | 408 |
|
406 | 409 |
|
| 410 | + static protected File getProcessingApp() { |
| 411 | + File appFile; |
| 412 | + if (Platform.isMacOS()) { |
| 413 | + // walks up from Processing.app/Contents/Java to Processing.app |
| 414 | + // (or whatever the user has renamed it to) |
| 415 | + appFile = getContentFile("../.."); |
| 416 | + } else if (Platform.isWindows()) { |
| 417 | + appFile = getContentFile("processing.exe"); |
| 418 | + } else { |
| 419 | + appFile = getContentFile("processing"); |
| 420 | + } |
| 421 | + try { |
| 422 | + return appFile.getCanonicalFile(); |
| 423 | + |
| 424 | + } catch (Exception e) { |
| 425 | + e.printStackTrace(); |
| 426 | + } |
| 427 | + return null; |
| 428 | + } |
| 429 | + |
| 430 | + |
| 431 | + // Not great, shows the crusty Duke icon in the dock. |
| 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 |
| 492 | + cmd.append("-jar " + new File(mainCommand[0]).getPath()); |
| 493 | + } else { |
| 494 | + // else it's a .class, add the classpath and mainClass |
| 495 | + cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]); |
| 496 | + } |
| 497 | + // finally add program arguments |
| 498 | + for (int i = 1; i < mainCommand.length; i++) { |
| 499 | + cmd.append(" "); |
| 500 | + cmd.append(mainCommand[i]); |
| 501 | + } |
| 502 | + */ |
| 503 | + // execute the command in a shutdown hook, to be sure that all the |
| 504 | + // resources have been disposed before restarting the application |
| 505 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 506 | + try { |
| 507 | +// System.out.println(new StringList(cmd).join(" ")); |
| 508 | +// Runtime.getRuntime().exec(cmd.toArray(new String[0])); |
| 509 | + builder.start(); |
| 510 | + } catch (IOException e) { |
| 511 | + e.printStackTrace(); |
| 512 | + } |
| 513 | + })); |
| 514 | + System.exit(0); |
| 515 | + } |
| 516 | + |
| 517 | + |
407 | 518 | // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
408 | 519 |
|
409 | 520 |
|
|
0 commit comments