1 /*
   2  * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.*;
  29 import java.math.BigInteger;
  30 import java.util.ArrayList;
  31 import java.util.regex.Matcher;
  32 import java.util.regex.Pattern;
  33 import java.util.stream.Collectors;
  34 import java.util.Collections;
  35 import java.util.List;
  36 import java.util.Optional;
  37 import java.util.StringTokenizer;
  38 import jdk.internal.reflect.CallerSensitive;
  39 import jdk.internal.reflect.Reflection;
  40 
  41 /**
  42  * Every Java application has a single instance of class
  43  * {@code Runtime} that allows the application to interface with
  44  * the environment in which the application is running. The current
  45  * runtime can be obtained from the {@code getRuntime} method.
  46  * <p>
  47  * An application cannot create its own instance of this class.
  48  *
  49  * @author  unascribed
  50  * @see     java.lang.Runtime#getRuntime()
  51  * @since   1.0
  52  */
  53 
  54 public class Runtime {
  55     private static final Runtime currentRuntime = new Runtime();
  56 
  57     private static Version version;
  58 
  59     /**
  60      * Returns the runtime object associated with the current Java application.
  61      * Most of the methods of class {@code Runtime} are instance
  62      * methods and must be invoked with respect to the current runtime object.
  63      *
  64      * @return  the {@code Runtime} object associated with the current
  65      *          Java application.
  66      */
  67     public static Runtime getRuntime() {
  68         return currentRuntime;
  69     }
  70 
  71     /** Don't let anyone else instantiate this class */
  72     private Runtime() {}
  73 
  74     /**
  75      * Terminates the currently running Java virtual machine by initiating its
  76      * shutdown sequence.  This method never returns normally.  The argument
  77      * serves as a status code; by convention, a nonzero status code indicates
  78      * abnormal termination.
  79      *
  80      * <p> The virtual machine's shutdown sequence consists of two phases.  In
  81      * the first phase all registered {@link #addShutdownHook shutdown hooks},
  82      * if any, are started in some unspecified order and allowed to run
  83      * concurrently until they finish.  In the second phase all uninvoked
  84      * finalizers are run if {@link #runFinalizersOnExit finalization-on-exit}
  85      * has been enabled.  Once this is done the virtual machine {@link #halt halts}.
  86      *
  87      * <p> If this method is invoked after the virtual machine has begun its
  88      * shutdown sequence then if shutdown hooks are being run this method will
  89      * block indefinitely.  If shutdown hooks have already been run and on-exit
  90      * finalization has been enabled then this method halts the virtual machine
  91      * with the given status code if the status is nonzero; otherwise, it
  92      * blocks indefinitely.
  93      *
  94      * <p> The {@link System#exit(int) System.exit} method is the
  95      * conventional and convenient means of invoking this method.
  96      *
  97      * @param  status
  98      *         Termination status.  By convention, a nonzero status code
  99      *         indicates abnormal termination.
 100      *
 101      * @throws SecurityException
 102      *         If a security manager is present and its
 103      *         {@link SecurityManager#checkExit checkExit} method does not permit
 104      *         exiting with the specified status
 105      *
 106      * @see java.lang.SecurityException
 107      * @see java.lang.SecurityManager#checkExit(int)
 108      * @see #addShutdownHook
 109      * @see #removeShutdownHook
 110      * @see #runFinalizersOnExit
 111      * @see #halt(int)
 112      */
 113     public void exit(int status) {
 114         SecurityManager security = System.getSecurityManager();
 115         if (security != null) {
 116             security.checkExit(status);
 117         }
 118         Shutdown.exit(status);
 119     }
 120 
 121     /**
 122      * Registers a new virtual-machine shutdown hook.
 123      *
 124      * <p> The Java virtual machine <i>shuts down</i> in response to two kinds
 125      * of events:
 126      *
 127      *   <ul>
 128      *
 129      *   <li> The program <i>exits</i> normally, when the last non-daemon
 130      *   thread exits or when the {@link #exit exit} (equivalently,
 131      *   {@link System#exit(int) System.exit}) method is invoked, or
 132      *
 133      *   <li> The virtual machine is <i>terminated</i> in response to a
 134      *   user interrupt, such as typing {@code ^C}, or a system-wide event,
 135      *   such as user logoff or system shutdown.
 136      *
 137      *   </ul>
 138      *
 139      * <p> A <i>shutdown hook</i> is simply an initialized but unstarted
 140      * thread.  When the virtual machine begins its shutdown sequence it will
 141      * start all registered shutdown hooks in some unspecified order and let
 142      * them run concurrently.  When all the hooks have finished it will then
 143      * run all uninvoked finalizers if finalization-on-exit has been enabled.
 144      * Finally, the virtual machine will halt.  Note that daemon threads will
 145      * continue to run during the shutdown sequence, as will non-daemon threads
 146      * if shutdown was initiated by invoking the {@link #exit exit} method.
 147      *
 148      * <p> Once the shutdown sequence has begun it can be stopped only by
 149      * invoking the {@link #halt halt} method, which forcibly
 150      * terminates the virtual machine.
 151      *
 152      * <p> Once the shutdown sequence has begun it is impossible to register a
 153      * new shutdown hook or de-register a previously-registered hook.
 154      * Attempting either of these operations will cause an
 155      * {@link IllegalStateException} to be thrown.
 156      *
 157      * <p> Shutdown hooks run at a delicate time in the life cycle of a virtual
 158      * machine and should therefore be coded defensively.  They should, in
 159      * particular, be written to be thread-safe and to avoid deadlocks insofar
 160      * as possible.  They should also not rely blindly upon services that may
 161      * have registered their own shutdown hooks and therefore may themselves in
 162      * the process of shutting down.  Attempts to use other thread-based
 163      * services such as the AWT event-dispatch thread, for example, may lead to
 164      * deadlocks.
 165      *
 166      * <p> Shutdown hooks should also finish their work quickly.  When a
 167      * program invokes {@link #exit exit} the expectation is
 168      * that the virtual machine will promptly shut down and exit.  When the
 169      * virtual machine is terminated due to user logoff or system shutdown the
 170      * underlying operating system may only allow a fixed amount of time in
 171      * which to shut down and exit.  It is therefore inadvisable to attempt any
 172      * user interaction or to perform a long-running computation in a shutdown
 173      * hook.
 174      *
 175      * <p> Uncaught exceptions are handled in shutdown hooks just as in any
 176      * other thread, by invoking the
 177      * {@link ThreadGroup#uncaughtException uncaughtException} method of the
 178      * thread's {@link ThreadGroup} object. The default implementation of this
 179      * method prints the exception's stack trace to {@link System#err} and
 180      * terminates the thread; it does not cause the virtual machine to exit or
 181      * halt.
 182      *
 183      * <p> In rare circumstances the virtual machine may <i>abort</i>, that is,
 184      * stop running without shutting down cleanly.  This occurs when the
 185      * virtual machine is terminated externally, for example with the
 186      * {@code SIGKILL} signal on Unix or the {@code TerminateProcess} call on
 187      * Microsoft Windows.  The virtual machine may also abort if a native
 188      * method goes awry by, for example, corrupting internal data structures or
 189      * attempting to access nonexistent memory.  If the virtual machine aborts
 190      * then no guarantee can be made about whether or not any shutdown hooks
 191      * will be run.
 192      *
 193      * @param   hook
 194      *          An initialized but unstarted {@link Thread} object
 195      *
 196      * @throws  IllegalArgumentException
 197      *          If the specified hook has already been registered,
 198      *          or if it can be determined that the hook is already running or
 199      *          has already been run
 200      *
 201      * @throws  IllegalStateException
 202      *          If the virtual machine is already in the process
 203      *          of shutting down
 204      *
 205      * @throws  SecurityException
 206      *          If a security manager is present and it denies
 207      *          {@link RuntimePermission}("shutdownHooks")
 208      *
 209      * @see #removeShutdownHook
 210      * @see #halt(int)
 211      * @see #exit(int)
 212      * @since 1.3
 213      */
 214     public void addShutdownHook(Thread hook) {
 215         SecurityManager sm = System.getSecurityManager();
 216         if (sm != null) {
 217             sm.checkPermission(new RuntimePermission("shutdownHooks"));
 218         }
 219         ApplicationShutdownHooks.add(hook);
 220     }
 221 
 222     /**
 223      * De-registers a previously-registered virtual-machine shutdown hook.
 224      *
 225      * @param hook the hook to remove
 226      * @return {@code true} if the specified hook had previously been
 227      * registered and was successfully de-registered, {@code false}
 228      * otherwise.
 229      *
 230      * @throws  IllegalStateException
 231      *          If the virtual machine is already in the process of shutting
 232      *          down
 233      *
 234      * @throws  SecurityException
 235      *          If a security manager is present and it denies
 236      *          {@link RuntimePermission}("shutdownHooks")
 237      *
 238      * @see #addShutdownHook
 239      * @see #exit(int)
 240      * @since 1.3
 241      */
 242     public boolean removeShutdownHook(Thread hook) {
 243         SecurityManager sm = System.getSecurityManager();
 244         if (sm != null) {
 245             sm.checkPermission(new RuntimePermission("shutdownHooks"));
 246         }
 247         return ApplicationShutdownHooks.remove(hook);
 248     }
 249 
 250     /**
 251      * Forcibly terminates the currently running Java virtual machine.  This
 252      * method never returns normally.
 253      *
 254      * <p> This method should be used with extreme caution.  Unlike the
 255      * {@link #exit exit} method, this method does not cause shutdown
 256      * hooks to be started and does not run uninvoked finalizers if
 257      * finalization-on-exit has been enabled.  If the shutdown sequence has
 258      * already been initiated then this method does not wait for any running
 259      * shutdown hooks or finalizers to finish their work.
 260      *
 261      * @param  status
 262      *         Termination status. By convention, a nonzero status code
 263      *         indicates abnormal termination. If the {@link Runtime#exit exit}
 264      *         (equivalently, {@link System#exit(int) System.exit}) method
 265      *         has already been invoked then this status code
 266      *         will override the status code passed to that method.
 267      *
 268      * @throws SecurityException
 269      *         If a security manager is present and its
 270      *         {@link SecurityManager#checkExit checkExit} method
 271      *         does not permit an exit with the specified status
 272      *
 273      * @see #exit
 274      * @see #addShutdownHook
 275      * @see #removeShutdownHook
 276      * @since 1.3
 277      */
 278     public void halt(int status) {
 279         SecurityManager sm = System.getSecurityManager();
 280         if (sm != null) {
 281             sm.checkExit(status);
 282         }
 283         Shutdown.halt(status);
 284     }
 285 
 286     /**
 287      * Enable or disable finalization on exit; doing so specifies that the
 288      * finalizers of all objects that have finalizers that have not yet been
 289      * automatically invoked are to be run before the Java runtime exits.
 290      * By default, finalization on exit is disabled.
 291      *
 292      * <p>If there is a security manager,
 293      * its {@code checkExit} method is first called
 294      * with 0 as its argument to ensure the exit is allowed.
 295      * This could result in a SecurityException.
 296      *
 297      * @param value true to enable finalization on exit, false to disable
 298      * @deprecated  This method is inherently unsafe.  It may result in
 299      *      finalizers being called on live objects while other threads are
 300      *      concurrently manipulating those objects, resulting in erratic
 301      *      behavior or deadlock.
 302      *      This method is subject to removal in a future version of Java SE.
 303      *
 304      * @throws  SecurityException
 305      *        if a security manager exists and its {@code checkExit}
 306      *        method doesn't allow the exit.
 307      *
 308      * @see     java.lang.Runtime#exit(int)
 309      * @see     java.lang.Runtime#gc()
 310      * @see     java.lang.SecurityManager#checkExit(int)
 311      * @since   1.1
 312      */
 313     @Deprecated(since="1.2", forRemoval=true)
 314     public static void runFinalizersOnExit(boolean value) {
 315         SecurityManager security = System.getSecurityManager();
 316         if (security != null) {
 317             try {
 318                 security.checkExit(0);
 319             } catch (SecurityException e) {
 320                 throw new SecurityException("runFinalizersOnExit");
 321             }
 322         }
 323         Shutdown.setRunFinalizersOnExit(value);
 324     }
 325 
 326     /**
 327      * Executes the specified string command in a separate process.
 328      *
 329      * <p>This is a convenience method.  An invocation of the form
 330      * {@code exec(command)}
 331      * behaves in exactly the same way as the invocation
 332      * {@link #exec(String, String[], File) exec}{@code (command, null, null)}.
 333      *
 334      * @param   command   a specified system command.
 335      *
 336      * @return  A new {@link Process} object for managing the subprocess
 337      *
 338      * @throws  SecurityException
 339      *          If a security manager exists and its
 340      *          {@link SecurityManager#checkExec checkExec}
 341      *          method doesn't allow creation of the subprocess
 342      *
 343      * @throws  IOException
 344      *          If an I/O error occurs
 345      *
 346      * @throws  NullPointerException
 347      *          If {@code command} is {@code null}
 348      *
 349      * @throws  IllegalArgumentException
 350      *          If {@code command} is empty
 351      *
 352      * @see     #exec(String[], String[], File)
 353      * @see     ProcessBuilder
 354      */
 355     public Process exec(String command) throws IOException {
 356         return exec(command, null, null);
 357     }
 358 
 359     /**
 360      * Executes the specified string command in a separate process with the
 361      * specified environment.
 362      *
 363      * <p>This is a convenience method.  An invocation of the form
 364      * {@code exec(command, envp)}
 365      * behaves in exactly the same way as the invocation
 366      * {@link #exec(String, String[], File) exec}{@code (command, envp, null)}.
 367      *
 368      * @param   command   a specified system command.
 369      *
 370      * @param   envp      array of strings, each element of which
 371      *                    has environment variable settings in the format
 372      *                    <i>name</i>=<i>value</i>, or
 373      *                    {@code null} if the subprocess should inherit
 374      *                    the environment of the current process.
 375      *
 376      * @return  A new {@link Process} object for managing the subprocess
 377      *
 378      * @throws  SecurityException
 379      *          If a security manager exists and its
 380      *          {@link SecurityManager#checkExec checkExec}
 381      *          method doesn't allow creation of the subprocess
 382      *
 383      * @throws  IOException
 384      *          If an I/O error occurs
 385      *
 386      * @throws  NullPointerException
 387      *          If {@code command} is {@code null},
 388      *          or one of the elements of {@code envp} is {@code null}
 389      *
 390      * @throws  IllegalArgumentException
 391      *          If {@code command} is empty
 392      *
 393      * @see     #exec(String[], String[], File)
 394      * @see     ProcessBuilder
 395      */
 396     public Process exec(String command, String[] envp) throws IOException {
 397         return exec(command, envp, null);
 398     }
 399 
 400     /**
 401      * Executes the specified string command in a separate process with the
 402      * specified environment and working directory.
 403      *
 404      * <p>This is a convenience method.  An invocation of the form
 405      * {@code exec(command, envp, dir)}
 406      * behaves in exactly the same way as the invocation
 407      * {@link #exec(String[], String[], File) exec}{@code (cmdarray, envp, dir)},
 408      * where {@code cmdarray} is an array of all the tokens in
 409      * {@code command}.
 410      *
 411      * <p>More precisely, the {@code command} string is broken
 412      * into tokens using a {@link StringTokenizer} created by the call
 413      * {@code new {@link StringTokenizer}(command)} with no
 414      * further modification of the character categories.  The tokens
 415      * produced by the tokenizer are then placed in the new string
 416      * array {@code cmdarray}, in the same order.
 417      *
 418      * @param   command   a specified system command.
 419      *
 420      * @param   envp      array of strings, each element of which
 421      *                    has environment variable settings in the format
 422      *                    <i>name</i>=<i>value</i>, or
 423      *                    {@code null} if the subprocess should inherit
 424      *                    the environment of the current process.
 425      *
 426      * @param   dir       the working directory of the subprocess, or
 427      *                    {@code null} if the subprocess should inherit
 428      *                    the working directory of the current process.
 429      *
 430      * @return  A new {@link Process} object for managing the subprocess
 431      *
 432      * @throws  SecurityException
 433      *          If a security manager exists and its
 434      *          {@link SecurityManager#checkExec checkExec}
 435      *          method doesn't allow creation of the subprocess
 436      *
 437      * @throws  IOException
 438      *          If an I/O error occurs
 439      *
 440      * @throws  NullPointerException
 441      *          If {@code command} is {@code null},
 442      *          or one of the elements of {@code envp} is {@code null}
 443      *
 444      * @throws  IllegalArgumentException
 445      *          If {@code command} is empty
 446      *
 447      * @see     ProcessBuilder
 448      * @since 1.3
 449      */
 450     public Process exec(String command, String[] envp, File dir)
 451         throws IOException {
 452         if (command.length() == 0)
 453             throw new IllegalArgumentException("Empty command");
 454 
 455         StringTokenizer st = new StringTokenizer(command);
 456         String[] cmdarray = new String[st.countTokens()];
 457         for (int i = 0; st.hasMoreTokens(); i++)
 458             cmdarray[i] = st.nextToken();
 459         return exec(cmdarray, envp, dir);
 460     }
 461 
 462     /**
 463      * Executes the specified command and arguments in a separate process.
 464      *
 465      * <p>This is a convenience method.  An invocation of the form
 466      * {@code exec(cmdarray)}
 467      * behaves in exactly the same way as the invocation
 468      * {@link #exec(String[], String[], File) exec}{@code (cmdarray, null, null)}.
 469      *
 470      * @param   cmdarray  array containing the command to call and
 471      *                    its arguments.
 472      *
 473      * @return  A new {@link Process} object for managing the subprocess
 474      *
 475      * @throws  SecurityException
 476      *          If a security manager exists and its
 477      *          {@link SecurityManager#checkExec checkExec}
 478      *          method doesn't allow creation of the subprocess
 479      *
 480      * @throws  IOException
 481      *          If an I/O error occurs
 482      *
 483      * @throws  NullPointerException
 484      *          If {@code cmdarray} is {@code null},
 485      *          or one of the elements of {@code cmdarray} is {@code null}
 486      *
 487      * @throws  IndexOutOfBoundsException
 488      *          If {@code cmdarray} is an empty array
 489      *          (has length {@code 0})
 490      *
 491      * @see     ProcessBuilder
 492      */
 493     public Process exec(String cmdarray[]) throws IOException {
 494         return exec(cmdarray, null, null);
 495     }
 496 
 497     /**
 498      * Executes the specified command and arguments in a separate process
 499      * with the specified environment.
 500      *
 501      * <p>This is a convenience method.  An invocation of the form
 502      * {@code exec(cmdarray, envp)}
 503      * behaves in exactly the same way as the invocation
 504      * {@link #exec(String[], String[], File) exec}{@code (cmdarray, envp, null)}.
 505      *
 506      * @param   cmdarray  array containing the command to call and
 507      *                    its arguments.
 508      *
 509      * @param   envp      array of strings, each element of which
 510      *                    has environment variable settings in the format
 511      *                    <i>name</i>=<i>value</i>, or
 512      *                    {@code null} if the subprocess should inherit
 513      *                    the environment of the current process.
 514      *
 515      * @return  A new {@link Process} object for managing the subprocess
 516      *
 517      * @throws  SecurityException
 518      *          If a security manager exists and its
 519      *          {@link SecurityManager#checkExec checkExec}
 520      *          method doesn't allow creation of the subprocess
 521      *
 522      * @throws  IOException
 523      *          If an I/O error occurs
 524      *
 525      * @throws  NullPointerException
 526      *          If {@code cmdarray} is {@code null},
 527      *          or one of the elements of {@code cmdarray} is {@code null},
 528      *          or one of the elements of {@code envp} is {@code null}
 529      *
 530      * @throws  IndexOutOfBoundsException
 531      *          If {@code cmdarray} is an empty array
 532      *          (has length {@code 0})
 533      *
 534      * @see     ProcessBuilder
 535      */
 536     public Process exec(String[] cmdarray, String[] envp) throws IOException {
 537         return exec(cmdarray, envp, null);
 538     }
 539 
 540 
 541     /**
 542      * Executes the specified command and arguments in a separate process with
 543      * the specified environment and working directory.
 544      *
 545      * <p>Given an array of strings {@code cmdarray}, representing the
 546      * tokens of a command line, and an array of strings {@code envp},
 547      * representing "environment" variable settings, this method creates
 548      * a new process in which to execute the specified command.
 549      *
 550      * <p>This method checks that {@code cmdarray} is a valid operating
 551      * system command.  Which commands are valid is system-dependent,
 552      * but at the very least the command must be a non-empty list of
 553      * non-null strings.
 554      *
 555      * <p>If {@code envp} is {@code null}, the subprocess inherits the
 556      * environment settings of the current process.
 557      *
 558      * <p>A minimal set of system dependent environment variables may
 559      * be required to start a process on some operating systems.
 560      * As a result, the subprocess may inherit additional environment variable
 561      * settings beyond those in the specified environment.
 562      *
 563      * <p>{@link ProcessBuilder#start()} is now the preferred way to
 564      * start a process with a modified environment.
 565      *
 566      * <p>The working directory of the new subprocess is specified by {@code dir}.
 567      * If {@code dir} is {@code null}, the subprocess inherits the
 568      * current working directory of the current process.
 569      *
 570      * <p>If a security manager exists, its
 571      * {@link SecurityManager#checkExec checkExec}
 572      * method is invoked with the first component of the array
 573      * {@code cmdarray} as its argument. This may result in a
 574      * {@link SecurityException} being thrown.
 575      *
 576      * <p>Starting an operating system process is highly system-dependent.
 577      * Among the many things that can go wrong are:
 578      * <ul>
 579      * <li>The operating system program file was not found.
 580      * <li>Access to the program file was denied.
 581      * <li>The working directory does not exist.
 582      * </ul>
 583      *
 584      * <p>In such cases an exception will be thrown.  The exact nature
 585      * of the exception is system-dependent, but it will always be a
 586      * subclass of {@link IOException}.
 587      *
 588      * <p>If the operating system does not support the creation of
 589      * processes, an {@link UnsupportedOperationException} will be thrown.
 590      *
 591      *
 592      * @param   cmdarray  array containing the command to call and
 593      *                    its arguments.
 594      *
 595      * @param   envp      array of strings, each element of which
 596      *                    has environment variable settings in the format
 597      *                    <i>name</i>=<i>value</i>, or
 598      *                    {@code null} if the subprocess should inherit
 599      *                    the environment of the current process.
 600      *
 601      * @param   dir       the working directory of the subprocess, or
 602      *                    {@code null} if the subprocess should inherit
 603      *                    the working directory of the current process.
 604      *
 605      * @return  A new {@link Process} object for managing the subprocess
 606      *
 607      * @throws  SecurityException
 608      *          If a security manager exists and its
 609      *          {@link SecurityManager#checkExec checkExec}
 610      *          method doesn't allow creation of the subprocess
 611      *
 612      * @throws  UnsupportedOperationException
 613      *          If the operating system does not support the creation of processes.
 614      *
 615      * @throws  IOException
 616      *          If an I/O error occurs
 617      *
 618      * @throws  NullPointerException
 619      *          If {@code cmdarray} is {@code null},
 620      *          or one of the elements of {@code cmdarray} is {@code null},
 621      *          or one of the elements of {@code envp} is {@code null}
 622      *
 623      * @throws  IndexOutOfBoundsException
 624      *          If {@code cmdarray} is an empty array
 625      *          (has length {@code 0})
 626      *
 627      * @see     ProcessBuilder
 628      * @since 1.3
 629      */
 630     public Process exec(String[] cmdarray, String[] envp, File dir)
 631         throws IOException {
 632         return new ProcessBuilder(cmdarray)
 633             .environment(envp)
 634             .directory(dir)
 635             .start();
 636     }
 637 
 638     /**
 639      * Returns the number of processors available to the Java virtual machine.
 640      *
 641      * <p> This value may change during a particular invocation of the virtual
 642      * machine.  Applications that are sensitive to the number of available
 643      * processors should therefore occasionally poll this property and adjust
 644      * their resource usage appropriately. </p>
 645      *
 646      * @return  the maximum number of processors available to the virtual
 647      *          machine; never smaller than one
 648      * @since 1.4
 649      */
 650     public native int availableProcessors();
 651 
 652     /**
 653      * Returns the amount of free memory in the Java Virtual Machine.
 654      * Calling the
 655      * {@code gc} method may result in increasing the value returned
 656      * by {@code freeMemory.}
 657      *
 658      * @return  an approximation to the total amount of memory currently
 659      *          available for future allocated objects, measured in bytes.
 660      */
 661     public native long freeMemory();
 662 
 663     /**
 664      * Returns the total amount of memory in the Java virtual machine.
 665      * The value returned by this method may vary over time, depending on
 666      * the host environment.
 667      * <p>
 668      * Note that the amount of memory required to hold an object of any
 669      * given type may be implementation-dependent.
 670      *
 671      * @return  the total amount of memory currently available for current
 672      *          and future objects, measured in bytes.
 673      */
 674     public native long totalMemory();
 675 
 676     /**
 677      * Returns the maximum amount of memory that the Java virtual machine
 678      * will attempt to use.  If there is no inherent limit then the value
 679      * {@link java.lang.Long#MAX_VALUE} will be returned.
 680      *
 681      * @return  the maximum amount of memory that the virtual machine will
 682      *          attempt to use, measured in bytes
 683      * @since 1.4
 684      */
 685     public native long maxMemory();
 686 
 687     /**
 688      * Runs the garbage collector.
 689      * Calling this method suggests that the Java virtual machine expend
 690      * effort toward recycling unused objects in order to make the memory
 691      * they currently occupy available for quick reuse. When control
 692      * returns from the method call, the virtual machine has made
 693      * its best effort to recycle all discarded objects.
 694      * <p>
 695      * The name {@code gc} stands for "garbage
 696      * collector". The virtual machine performs this recycling
 697      * process automatically as needed, in a separate thread, even if the
 698      * {@code gc} method is not invoked explicitly.
 699      * <p>
 700      * The method {@link System#gc()} is the conventional and convenient
 701      * means of invoking this method.
 702      */
 703     public native void gc();
 704 
 705     /* Wormhole for calling java.lang.ref.Finalizer.runFinalization */
 706     private static native void runFinalization0();
 707 
 708     /**
 709      * Runs the finalization methods of any objects pending finalization.
 710      * Calling this method suggests that the Java virtual machine expend
 711      * effort toward running the {@code finalize} methods of objects
 712      * that have been found to be discarded but whose {@code finalize}
 713      * methods have not yet been run. When control returns from the
 714      * method call, the virtual machine has made a best effort to
 715      * complete all outstanding finalizations.
 716      * <p>
 717      * The virtual machine performs the finalization process
 718      * automatically as needed, in a separate thread, if the
 719      * {@code runFinalization} method is not invoked explicitly.
 720      * <p>
 721      * The method {@link System#runFinalization()} is the conventional
 722      * and convenient means of invoking this method.
 723      *
 724      * @see     java.lang.Object#finalize()
 725      */
 726     public void runFinalization() {
 727         runFinalization0();
 728     }
 729 
 730     /**
 731      * Not implemented, does nothing.
 732      *
 733      * @deprecated
 734      * This method was intended to control instruction tracing.
 735      * It has been superseded by JVM-specific tracing mechanisms.
 736      * This method is subject to removal in a future version of Java SE.
 737      *
 738      * @param on ignored
 739      */
 740     @Deprecated(since="9", forRemoval=true)
 741     public void traceInstructions(boolean on) { }
 742 
 743     /**
 744      * Not implemented, does nothing.
 745      *
 746      * @deprecated
 747      * This method was intended to control method call tracing.
 748      * It has been superseded by JVM-specific tracing mechanisms.
 749      * This method is subject to removal in a future version of Java SE.
 750      *
 751      * @param on ignored
 752      */
 753     @Deprecated(since="9", forRemoval=true)
 754     public void traceMethodCalls(boolean on) { }
 755 
 756     /**
 757      * Loads the native library specified by the filename argument.  The filename
 758      * argument must be an absolute path name.
 759      * (for example
 760      * {@code Runtime.getRuntime().load("/home/avh/lib/libX11.so");}).
 761      *
 762      * If the filename argument, when stripped of any platform-specific library
 763      * prefix, path, and file extension, indicates a library whose name is,
 764      * for example, L, and a native library called L is statically linked
 765      * with the VM, then the JNI_OnLoad_L function exported by the library
 766      * is invoked rather than attempting to load a dynamic library.
 767      * A filename matching the argument does not have to exist in the file
 768      * system. See the JNI Specification for more details.
 769      *
 770      * Otherwise, the filename argument is mapped to a native library image in
 771      * an implementation-dependent manner.
 772      * <p>
 773      * First, if there is a security manager, its {@code checkLink}
 774      * method is called with the {@code filename} as its argument.
 775      * This may result in a security exception.
 776      * <p>
 777      * This is similar to the method {@link #loadLibrary(String)}, but it
 778      * accepts a general file name as an argument rather than just a library
 779      * name, allowing any file of native code to be loaded.
 780      * <p>
 781      * The method {@link System#load(String)} is the conventional and
 782      * convenient means of invoking this method.
 783      *
 784      * @param      filename   the file to load.
 785      * @exception  SecurityException  if a security manager exists and its
 786      *             {@code checkLink} method doesn't allow
 787      *             loading of the specified dynamic library
 788      * @exception  UnsatisfiedLinkError  if either the filename is not an
 789      *             absolute path name, the native library is not statically
 790      *             linked with the VM, or the library cannot be mapped to
 791      *             a native library image by the host system.
 792      * @exception  NullPointerException if {@code filename} is
 793      *             {@code null}
 794      * @see        java.lang.Runtime#getRuntime()
 795      * @see        java.lang.SecurityException
 796      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 797      */
 798     @CallerSensitive
 799     public void load(String filename) {
 800         load0(Reflection.getCallerClass(), filename);
 801     }
 802 
 803     synchronized void load0(Class<?> fromClass, String filename) {
 804         SecurityManager security = System.getSecurityManager();
 805         if (security != null) {
 806             security.checkLink(filename);
 807         }
 808         if (!(new File(filename).isAbsolute())) {
 809             throw new UnsatisfiedLinkError(
 810                 "Expecting an absolute path of the library: " + filename);
 811         }
 812         ClassLoader.loadLibrary(fromClass, filename, true);
 813     }
 814 
 815     /**
 816      * Loads the native library specified by the {@code libname}
 817      * argument.  The {@code libname} argument must not contain any platform
 818      * specific prefix, file extension or path. If a native library
 819      * called {@code libname} is statically linked with the VM, then the
 820      * JNI_OnLoad_{@code libname} function exported by the library is invoked.
 821      * See the JNI Specification for more details.
 822      *
 823      * Otherwise, the libname argument is loaded from a system library
 824      * location and mapped to a native library image in an implementation-
 825      * dependent manner.
 826      * <p>
 827      * First, if there is a security manager, its {@code checkLink}
 828      * method is called with the {@code libname} as its argument.
 829      * This may result in a security exception.
 830      * <p>
 831      * The method {@link System#loadLibrary(String)} is the conventional
 832      * and convenient means of invoking this method. If native
 833      * methods are to be used in the implementation of a class, a standard
 834      * strategy is to put the native code in a library file (call it
 835      * {@code LibFile}) and then to put a static initializer:
 836      * <blockquote><pre>
 837      * static { System.loadLibrary("LibFile"); }
 838      * </pre></blockquote>
 839      * within the class declaration. When the class is loaded and
 840      * initialized, the necessary native code implementation for the native
 841      * methods will then be loaded as well.
 842      * <p>
 843      * If this method is called more than once with the same library
 844      * name, the second and subsequent calls are ignored.
 845      *
 846      * @param      libname   the name of the library.
 847      * @exception  SecurityException  if a security manager exists and its
 848      *             {@code checkLink} method doesn't allow
 849      *             loading of the specified dynamic library
 850      * @exception  UnsatisfiedLinkError if either the libname argument
 851      *             contains a file path, the native library is not statically
 852      *             linked with the VM,  or the library cannot be mapped to a
 853      *             native library image by the host system.
 854      * @exception  NullPointerException if {@code libname} is
 855      *             {@code null}
 856      * @see        java.lang.SecurityException
 857      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 858      */
 859     @CallerSensitive
 860     public void loadLibrary(String libname) {
 861         loadLibrary0(Reflection.getCallerClass(), libname);
 862     }
 863 
 864     synchronized void loadLibrary0(Class<?> fromClass, String libname) {
 865         SecurityManager security = System.getSecurityManager();
 866         if (security != null) {
 867             security.checkLink(libname);
 868         }
 869         if (libname.indexOf((int)File.separatorChar) != -1) {
 870             throw new UnsatisfiedLinkError(
 871     "Directory separator should not appear in library name: " + libname);
 872         }
 873         ClassLoader.loadLibrary(fromClass, libname, false);
 874     }
 875 
 876     /**
 877      * Creates a localized version of an input stream. This method takes
 878      * an {@code InputStream} and returns an {@code InputStream}
 879      * equivalent to the argument in all respects except that it is
 880      * localized: as characters in the local character set are read from
 881      * the stream, they are automatically converted from the local
 882      * character set to Unicode.
 883      * <p>
 884      * If the argument is already a localized stream, it may be returned
 885      * as the result.
 886      *
 887      * @param      in InputStream to localize
 888      * @return     a localized input stream
 889      * @see        java.io.InputStream
 890      * @see        java.io.BufferedReader#BufferedReader(java.io.Reader)
 891      * @see        java.io.InputStreamReader#InputStreamReader(java.io.InputStream)
 892      * @deprecated As of JDK&nbsp;1.1, the preferred way to translate a byte
 893      * stream in the local encoding into a character stream in Unicode is via
 894      * the {@code InputStreamReader} and {@code BufferedReader}
 895      * classes.
 896      * This method is subject to removal in a future version of Java SE.
 897      */
 898     @Deprecated(since="1.1", forRemoval=true)
 899     public InputStream getLocalizedInputStream(InputStream in) {
 900         return in;
 901     }
 902 
 903     /**
 904      * Creates a localized version of an output stream. This method
 905      * takes an {@code OutputStream} and returns an
 906      * {@code OutputStream} equivalent to the argument in all respects
 907      * except that it is localized: as Unicode characters are written to
 908      * the stream, they are automatically converted to the local
 909      * character set.
 910      * <p>
 911      * If the argument is already a localized stream, it may be returned
 912      * as the result.
 913      *
 914      * @deprecated As of JDK&nbsp;1.1, the preferred way to translate a
 915      * Unicode character stream into a byte stream in the local encoding is via
 916      * the {@code OutputStreamWriter}, {@code BufferedWriter}, and
 917      * {@code PrintWriter} classes.
 918      * This method is subject to removal in a future version of Java SE.
 919      *
 920      * @param      out OutputStream to localize
 921      * @return     a localized output stream
 922      * @see        java.io.OutputStream
 923      * @see        java.io.BufferedWriter#BufferedWriter(java.io.Writer)
 924      * @see        java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
 925      * @see        java.io.PrintWriter#PrintWriter(java.io.OutputStream)
 926      */
 927     @Deprecated(since="1.1", forRemoval=true)
 928     public OutputStream getLocalizedOutputStream(OutputStream out) {
 929         return out;
 930     }
 931 
 932     /**
 933      * Returns the version of the Java Runtime Environment as a {@link
 934      * Runtime.Version}.
 935      *
 936      * @return  the {@link Runtime.Version} of the Java Runtime Environment
 937      *
 938      * @since  9
 939      */
 940     public static Version version() {
 941         if (version == null) {
 942             version = new Version(VersionProps.versionNumbers(),
 943                     VersionProps.pre(), VersionProps.build(),
 944                     VersionProps.optional());
 945         }
 946         return version;
 947     }
 948 
 949     /**
 950      * A representation of a version string for an implementation of the
 951      * Java&nbsp;SE Platform.  A version string contains a version number
 952      * optionally followed by pre-release and build information.
 953      *
 954      * <h2><a name="verNum">Version numbers</a></h2>
 955      *
 956      * <p> A <em>version number</em>, {@code $VNUM}, is a non-empty sequence
 957      * of elements separated by period characters (U+002E).  An element is
 958      * either zero, or an unsigned integer numeral without leading zeros.  The
 959      * final element in a version number must not be zero.  The format is:
 960      * </p>
 961      *
 962      * <blockquote><pre>
 963      *     ^[1-9][0-9]*(((\.0)*\.[1-9][0-9]*)*)*$
 964      * </pre></blockquote>
 965      *
 966      * <p> The sequence may be of arbitrary length but the first three
 967      * elements are assigned specific meanings, as follows:</p>
 968      *
 969      * <blockquote><pre>
 970      *     $MAJOR.$MINOR.$SECURITY
 971      * </pre></blockquote>
 972      *
 973      * <ul>
 974      *
 975      * <li><p> <a name="major">{@code $MAJOR}</a> --- The major version
 976      * number, incremented for a major release that contains significant new
 977      * features as specified in a new edition of the Java SE Platform
 978      * Specification, <em>e.g.</em>, <a
 979      * href="https://jcp.org/en/jsr/detail?id=337">JSR 337</a> for
 980      * Java SE 8.  Features may be removed in a major release, given
 981      * advance notice at least one major release ahead of time, and
 982      * incompatible changes may be made when justified. The {@code $MAJOR}
 983      * version number of JDK 8 is {@code 8}; the {@code $MAJOR} version
 984      * number of JDK 9 is {@code 9}.  When {@code $MAJOR} is incremented,
 985      * all subsequent elements are removed. </p></li>
 986      *
 987      * <li><p> <a name="minor">{@code $MINOR}</a> --- The minor version
 988      * number, incremented for a minor update release that may contain
 989      * compatible bug fixes, revisions to standard APIs mandated by a
 990      * <a href="https://jcp.org/en/procedures/jcp2#5.3">Maintenance Release</a>
 991      * of the relevant Platform Specification, and implementation features
 992      * outside the scope of that Specification such as new JDK-specific APIs,
 993      * additional service providers, new garbage collectors, and ports to new
 994      * hardware architectures. </p></li>
 995      *
 996      * <li><p> <a name="security">{@code $SECURITY}</a> --- The security
 997      * level, incremented for a security update release that contains critical
 998      * fixes including those necessary to improve security.  {@code $SECURITY}
 999      * is <strong>not</strong> reset when {@code $MINOR} is incremented.  A
1000      * higher value of {@code $SECURITY} for a given {@code $MAJOR} value,
1001      * therefore, always indicates a more secure release, regardless of the
1002      * value of {@code $MINOR}. </p></li>
1003      *
1004      * </ul>
1005      *
1006      * <p> The fourth and later elements of a version number are free for use
1007      * by downstream consumers of this code base.  Such a consumer may,
1008      * <em>e.g.</em>, use the fourth element to identify patch releases which
1009      * contain a small number of critical non-security fixes in addition to
1010      * the security fixes in the corresponding security release. </p>
1011      *
1012      * <p> The version number does not include trailing zero elements;
1013      * <em>i.e.</em>, {@code $SECURITY} is omitted if it has the value zero,
1014      * and {@code $MINOR} is omitted if both {@code $MINOR} and {@code
1015      * $SECURITY} have the value zero. </p>
1016      *
1017      * <p> The sequence of numerals in a version number is compared to another
1018      * such sequence in numerical, pointwise fashion; <em>e.g.</em>, {@code
1019      * 9.9.1} is less than {@code 9.10.3}. If one sequence is shorter than
1020      * another then the missing elements of the shorter sequence are
1021      * considered to be less than the corresponding elements of the longer
1022      * sequence; <em>e.g.</em>, {@code 9.1.2} is less than {@code 9.1.2.1}.
1023      * </p>
1024      *
1025      * <h2><a name="verStr">Version strings</a></h2>
1026      *
1027      * <p> A <em>version string</em>, {@code $VSTR}, consists of a version
1028      * number {@code $VNUM}, as described above, optionally followed by
1029      * pre-release and build information, in the format </p>
1030      *
1031      * <blockquote><pre>
1032      *     $VNUM(-$PRE)?(\+($BUILD)?(-$OPT)?)?
1033      * </pre></blockquote>
1034      *
1035      * <p> where: </p>
1036      *
1037      * <ul>
1038      *
1039      * <li><p> <a name="pre">{@code $PRE}</a>, matching {@code ([a-zA-Z0-9]+)}
1040      * --- A pre-release identifier.  Typically {@code ea}, for a
1041      * potentially unstable early-access release under active development,
1042      * or {@code internal}, for an internal developer build.
1043      *
1044      * <li><p> <a name="build">{@code $BUILD}</a>, matching {@code
1045      * (0|[1-9][0-9]*)} --- The build number, incremented for each promoted
1046      * build.  {@code $BUILD} is reset to {@code 1} when any portion of {@code
1047      * $VNUM} is incremented. </p>
1048      *
1049      * <li><p> <a name="opt">{@code $OPT}</a>, matching {@code
1050      * ([-a-zA-Z0-9\.]+)} --- Additional build information, if desired.  In
1051      * the case of an {@code internal} build this will often contain the date
1052      * and time of the build. </p>
1053      *
1054      * </ul>
1055      *
1056      * <p> A version string {@code 10-ea} matches {@code $VNUM = "10"} and
1057      * {@code $PRE = "ea"}.  The version string {@code 10+-ea} matches
1058      * {@code $VNUM = "10"} and {@code $OPT = "ea"}. </p>
1059      *
1060      * <p> When comparing two version strings, the value of {@code $OPT}, if
1061      * present, may or may not be significant depending on the chosen
1062      * comparison method.  The comparison methods {@link #compareTo(Version)
1063      * compareTo()} and {@link #compareToIgnoreOptional(Version)
1064      * compareToIgnoreOptional()} should be used consistently with the
1065      * corresponding methods {@link #equals(Object) equals()} and {@link
1066      * #equalsIgnoreOptional(Object) equalsIgnoreOptional()}.  </p>
1067      *
1068      * <p> A <em>short version string</em>, {@code $SVSTR}, often useful in
1069      * less formal contexts, is a version number optionally followed by a
1070      * pre-release identifier:
1071      *
1072      * <blockquote><pre>
1073      *     $VNUM(-$PRE)?
1074      * </pre></blockquote>
1075      *
1076      * @since  9
1077      */
1078     public static class Version
1079         implements Comparable<Version>
1080     {
1081         private final List<Integer>     version;
1082         private final Optional<String>  pre;
1083         private final Optional<Integer> build;
1084         private final Optional<String>  optional;
1085 
1086         Version(List<Integer> version, Optional<String> pre,
1087                 Optional<Integer> build, Optional<String> optional) {
1088             this.version = Collections.unmodifiableList(version);
1089             this.pre = pre;
1090             this.build = build;
1091             this.optional = optional;
1092         }
1093 
1094         /**
1095          * Parses the given string as a valid
1096          * <a href="#verStr">version string</a> containing a
1097          * <a href="#verNum">version number</a> followed by pre-release and
1098          * build information.
1099          *
1100          * @param  s
1101          *         A string to interpret as a version
1102          *
1103          * @throws  IllegalArgumentException
1104          *          If the given string cannot be interpreted as a valid
1105          *          version
1106          *
1107          * @throws  NullPointerException
1108          *          If the given string is {@code null}
1109          *
1110          * @throws  NumberFormatException
1111          *          If an element of the version number or the build number
1112          *          cannot be represented as an {@link Integer}
1113          *
1114          * @return  The Version of the given string
1115          */
1116         public static Version parse(String s) {
1117             if (s == null)
1118                 throw new NullPointerException();
1119 
1120             // Shortcut to avoid initializing VersionPattern when creating
1121             // major version constants during startup
1122             if (isSimpleNumber(s)) {
1123                 return new Version(List.of(Integer.parseInt(s)),
1124                         Optional.empty(), Optional.empty(), Optional.empty());
1125             }
1126             Matcher m = VersionPattern.VSTR_PATTERN.matcher(s);
1127             if (!m.matches())
1128                 throw new IllegalArgumentException("Invalid version string: '"
1129                                                    + s + "'");
1130 
1131             // $VNUM is a dot-separated list of integers of arbitrary length
1132             List<Integer> version = new ArrayList<>();
1133             for (String i : m.group(VersionPattern.VNUM_GROUP).split("\\."))
1134                 version.add(Integer.parseInt(i));
1135 
1136             Optional<String> pre = Optional.ofNullable(
1137                     m.group(VersionPattern.PRE_GROUP));
1138 
1139             String b = m.group(VersionPattern.BUILD_GROUP);
1140             // $BUILD is an integer
1141             Optional<Integer> build = (b == null)
1142                 ? Optional.empty()
1143                 : Optional.of(Integer.parseInt(b));
1144 
1145             Optional<String> optional = Optional.ofNullable(
1146                     m.group(VersionPattern.OPT_GROUP));
1147 
1148             // empty '+'
1149             if ((m.group(VersionPattern.PLUS_GROUP) != null)
1150                     && !build.isPresent()) {
1151                 if (optional.isPresent()) {
1152                     if (pre.isPresent())
1153                         throw new IllegalArgumentException("'+' found with"
1154                             + " pre-release and optional components:'" + s
1155                             + "'");
1156                 } else {
1157                     throw new IllegalArgumentException("'+' found with neither"
1158                         + " build or optional components: '" + s + "'");
1159                 }
1160             }
1161             return new Version(version, pre, build, optional);
1162         }
1163 
1164         private static boolean isSimpleNumber(String s) {
1165             for (int i = 0; i < s.length(); i++) {
1166                 char c = s.charAt(i);
1167                 char lowerBound = (i > 0) ? '0' : '1';
1168                 if (c < lowerBound || c > '9') {
1169                     return false;
1170                 }
1171             }
1172             return true;
1173         }
1174 
1175         /**
1176          * Returns the <a href="#major">major</a> version number.
1177          *
1178          * @return  The major version number
1179          */
1180         public int major() {
1181             return version.get(0);
1182         }
1183 
1184         /**
1185          * Returns the <a href="#minor">minor</a> version number or zero if it
1186          * was not set.
1187          *
1188          * @return  The minor version number or zero if it was not set
1189          */
1190         public int minor() {
1191             return (version.size() > 1 ? version.get(1) : 0);
1192         }
1193 
1194         /**
1195          * Returns the <a href="#security">security</a> version number or zero
1196          * if it was not set.
1197          *
1198          * @return  The security version number or zero if it was not set
1199          */
1200         public int security() {
1201             return (version.size() > 2 ? version.get(2) : 0);
1202         }
1203 
1204         /**
1205          * Returns an unmodifiable {@link java.util.List List} of the
1206          * integer numerals contained in the <a href="#verNum">version
1207          * number</a>.  The {@code List} always contains at least one
1208          * element corresponding to the <a href="#major">major version
1209          * number</a>.
1210          *
1211          * @return  An unmodifiable list of the integer numerals
1212          *          contained in the version number
1213          */
1214         public List<Integer> version() {
1215             return version;
1216         }
1217 
1218         /**
1219          * Returns the optional <a href="#pre">pre-release</a> information.
1220          *
1221          * @return  The optional pre-release information as a String
1222          */
1223         public Optional<String> pre() {
1224             return pre;
1225         }
1226 
1227         /**
1228          * Returns the <a href="#build">build number</a>.
1229          *
1230          * @return  The optional build number.
1231          */
1232         public Optional<Integer> build() {
1233             return build;
1234         }
1235 
1236         /**
1237          * Returns <a href="#opt">optional</a> additional identifying build
1238          * information.
1239          *
1240          * @return  Additional build information as a String
1241          */
1242         public Optional<String> optional() {
1243             return optional;
1244         }
1245 
1246         /**
1247          * Compares this version to another.
1248          *
1249          * <p> Each of the components in the <a href="#verStr">version</a> is
1250          * compared in the following order of precedence: version numbers,
1251          * pre-release identifiers, build numbers, optional build information.
1252          * </p>
1253          *
1254          * <p> Comparison begins by examining the sequence of version numbers.
1255          * If one sequence is shorter than another, then the missing elements
1256          * of the shorter sequence are considered to be less than the
1257          * corresponding elements of the longer sequence. </p>
1258          *
1259          * <p> A version with a pre-release identifier is always considered to
1260          * be less than a version without one.  Pre-release identifiers are
1261          * compared numerically when they consist only of digits, and
1262          * lexicographically otherwise.  Numeric identifiers are considered to
1263          * be less than non-numeric identifiers.  </p>
1264          *
1265          * <p> A version without a build number is always less than one with a
1266          * build number; otherwise build numbers are compared numerically. </p>
1267          *
1268          * <p> The optional build information is compared lexicographically.
1269          * During this comparison, a version with optional build information is
1270          * considered to be greater than a version without one. </p>
1271          *
1272          * <p> A version is not comparable to any other type of object.
1273          *
1274          * @param  ob
1275          *         The object to be compared
1276          *
1277          * @return  A negative integer, zero, or a positive integer if this
1278          *          {@code Version} is less than, equal to, or greater than the
1279          *          given {@code Version}
1280          *
1281          * @throws  NullPointerException
1282          *          If the given object is {@code null}
1283          */
1284         @Override
1285         public int compareTo(Version ob) {
1286             return compare(ob, false);
1287         }
1288 
1289         /**
1290          * Compares this version to another disregarding optional build
1291          * information.
1292          *
1293          * <p> Two versions are compared by examining the version string as
1294          * described in {@link #compareTo(Version)} with the exception that the
1295          * optional build information is always ignored. </p>
1296          *
1297          * <p> A version is not comparable to any other type of object.
1298          *
1299          * @param  ob
1300          *         The object to be compared
1301          *
1302          * @return  A negative integer, zero, or a positive integer if this
1303          *          {@code Version} is less than, equal to, or greater than the
1304          *          given {@code Version}
1305          *
1306          * @throws  NullPointerException
1307          *          If the given object is {@code null}
1308          */
1309         public int compareToIgnoreOptional(Version ob) {
1310             return compare(ob, true);
1311         }
1312 
1313         private int compare(Version ob, boolean ignoreOpt) {
1314             if (ob == null)
1315                 throw new NullPointerException("Invalid argument");
1316 
1317             int ret = compareVersion(ob);
1318             if (ret != 0)
1319                 return ret;
1320 
1321             ret = comparePre(ob);
1322             if (ret != 0)
1323                 return ret;
1324 
1325             ret = compareBuild(ob);
1326             if (ret != 0)
1327                 return ret;
1328 
1329             if (!ignoreOpt)
1330                 return compareOptional(ob);
1331 
1332             return 0;
1333         }
1334 
1335         private int compareVersion(Version ob) {
1336             int size = version.size();
1337             int oSize = ob.version().size();
1338             int min = Math.min(size, oSize);
1339             for (int i = 0; i < min; i++) {
1340                 Integer val = version.get(i);
1341                 Integer oVal = ob.version().get(i);
1342                 if (val != oVal)
1343                     return val - oVal;
1344             }
1345             if (size != oSize)
1346                 return size - oSize;
1347             return 0;
1348         }
1349 
1350         private int comparePre(Version ob) {
1351             Optional<String> oPre = ob.pre();
1352             if (!pre.isPresent()) {
1353                 if (oPre.isPresent())
1354                     return 1;
1355             } else {
1356                 if (!oPre.isPresent())
1357                     return -1;
1358                 String val = pre.get();
1359                 String oVal = oPre.get();
1360                 if (val.matches("\\d+")) {
1361                     return (oVal.matches("\\d+")
1362                         ? (new BigInteger(val)).compareTo(new BigInteger(oVal))
1363                         : -1);
1364                 } else {
1365                     return (oVal.matches("\\d+")
1366                         ? 1
1367                         : val.compareTo(oVal));
1368                 }
1369             }
1370             return 0;
1371         }
1372 
1373         private int compareBuild(Version ob) {
1374             Optional<Integer> oBuild = ob.build();
1375             if (oBuild.isPresent()) {
1376                 return (build.isPresent()
1377                         ? build.get().compareTo(oBuild.get())
1378                         : -1);
1379             } else if (build.isPresent()) {
1380                 return 1;
1381             }
1382             return 0;
1383         }
1384 
1385         private int compareOptional(Version ob) {
1386             Optional<String> oOpt = ob.optional();
1387             if (!optional.isPresent()) {
1388                 if (oOpt.isPresent())
1389                     return -1;
1390             } else {
1391                 if (!oOpt.isPresent())
1392                     return 1;
1393                 return optional.get().compareTo(oOpt.get());
1394             }
1395             return 0;
1396         }
1397 
1398         /**
1399          * Returns a string representation of this version.
1400          *
1401          * @return  The version string
1402          */
1403         @Override
1404         public String toString() {
1405             StringBuilder sb
1406                 = new StringBuilder(version.stream()
1407                     .map(Object::toString)
1408                     .collect(Collectors.joining(".")));
1409 
1410             pre.ifPresent(v -> sb.append("-").append(v));
1411 
1412             if (build.isPresent()) {
1413                 sb.append("+").append(build.get());
1414                 if (optional.isPresent())
1415                     sb.append("-").append(optional.get());
1416             } else {
1417                 if (optional.isPresent()) {
1418                     sb.append(pre.isPresent() ? "-" : "+-");
1419                     sb.append(optional.get());
1420                 }
1421             }
1422 
1423             return sb.toString();
1424         }
1425 
1426         /**
1427          * Determines whether this {@code Version} is equal to another object.
1428          *
1429          * <p> Two {@code Version}s are equal if and only if they represent the
1430          * same version string.
1431          *
1432          * <p> This method satisfies the general contract of the {@link
1433          * Object#equals(Object) Object.equals} method. </p>
1434          *
1435          * @param  ob
1436          *         The object to which this {@code Version} is to be compared
1437          *
1438          * @return  {@code true} if, and only if, the given object is a {@code
1439          *          Version} that is identical to this {@code Version}
1440          *
1441          */
1442         @Override
1443         public boolean equals(Object ob) {
1444             boolean ret = equalsIgnoreOptional(ob);
1445             if (!ret)
1446                 return false;
1447 
1448             Version that = (Version)ob;
1449             return (this.optional().equals(that.optional()));
1450         }
1451 
1452         /**
1453          * Determines whether this {@code Version} is equal to another
1454          * disregarding optional build information.
1455          *
1456          * <p> Two {@code Version}s are equal if and only if they represent the
1457          * same version string disregarding the optional build information.
1458          *
1459          * @param  ob
1460          *         The object to which this {@code Version} is to be compared
1461          *
1462          * @return  {@code true} if, and only if, the given object is a {@code
1463          *          Version} that is identical to this {@code Version}
1464          *          ignoring the optional build information
1465          *
1466          */
1467         public boolean equalsIgnoreOptional(Object ob) {
1468             if (this == ob)
1469                 return true;
1470             if (!(ob instanceof Version))
1471                 return false;
1472 
1473             Version that = (Version)ob;
1474             return (this.version().equals(that.version())
1475                 && this.pre().equals(that.pre())
1476                 && this.build().equals(that.build()));
1477         }
1478 
1479         /**
1480          * Returns the hash code of this version.
1481          *
1482          * <p> This method satisfies the general contract of the {@link
1483          * Object#hashCode Object.hashCode} method.
1484          *
1485          * @return  The hashcode of this version
1486          */
1487         @Override
1488         public int hashCode() {
1489             int h = 1;
1490             int p = 17;
1491 
1492             h = p * h + version.hashCode();
1493             h = p * h + pre.hashCode();
1494             h = p * h + build.hashCode();
1495             h = p * h + optional.hashCode();
1496 
1497             return h;
1498         }
1499     }
1500 
1501     private static class VersionPattern {
1502         // $VNUM(-$PRE)?(\+($BUILD)?(\-$OPT)?)?
1503         // RE limits the format of version strings
1504         // ([1-9][0-9]*(?:(?:\.0)*\.[1-9][0-9]*)*)(?:-([a-zA-Z0-9]+))?(?:(\+)(0|[1-9][0-9]*)?)?(?:-([-a-zA-Z0-9.]+))?
1505 
1506         private static final String VNUM
1507             = "(?<VNUM>[1-9][0-9]*(?:(?:\\.0)*\\.[1-9][0-9]*)*)";
1508         private static final String PRE      = "(?:-(?<PRE>[a-zA-Z0-9]+))?";
1509         private static final String BUILD
1510             = "(?:(?<PLUS>\\+)(?<BUILD>0|[1-9][0-9]*)?)?";
1511         private static final String OPT      = "(?:-(?<OPT>[-a-zA-Z0-9.]+))?";
1512         private static final String VSTR_FORMAT
1513             = "^" + VNUM + PRE + BUILD + OPT + "$";
1514 
1515         static final Pattern VSTR_PATTERN = Pattern.compile(VSTR_FORMAT);
1516 
1517         static final String VNUM_GROUP  = "VNUM";
1518         static final String PRE_GROUP   = "PRE";
1519         static final String PLUS_GROUP  = "PLUS";
1520         static final String BUILD_GROUP = "BUILD";
1521         static final String OPT_GROUP   = "OPT";
1522     }
1523 }