< prev index next >

src/java.base/share/classes/java/lang/Runtime.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Azul Systems, Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.  Oracle designates this
   9  * particular file as subject to the "Classpath" exception as provided
  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 
  27 package java.lang;
  28 
  29 import java.io.*;
  30 import java.math.BigInteger;
  31 import java.util.regex.Matcher;
  32 import java.util.regex.Pattern;
  33 import java.util.stream.Collectors;
  34 import java.util.List;
  35 import java.util.Optional;
  36 import java.util.StringTokenizer;
  37 
  38 import jdk.internal.access.SharedSecrets;

  39 import jdk.internal.reflect.CallerSensitive;
  40 import jdk.internal.reflect.Reflection;
  41 
  42 /**
  43  * Every Java application has a single instance of class
  44  * {@code Runtime} that allows the application to interface with
  45  * the environment in which the application is running. The current
  46  * runtime can be obtained from the {@code getRuntime} method.
  47  * <p>
  48  * An application cannot create its own instance of this class.
  49  *
  50  * @author  unascribed
  51  * @see     java.lang.Runtime#getRuntime()
  52  * @since   1.0
  53  */
  54 
  55 public class Runtime {
  56     private static final Runtime currentRuntime = new Runtime();
  57 
  58     private static Version version;


 721      * @throws     UnsatisfiedLinkError  if either the filename is not an
 722      *             absolute path name, the native library is not statically
 723      *             linked with the VM, or the library cannot be mapped to
 724      *             a native library image by the host system.
 725      * @throws     NullPointerException if {@code filename} is
 726      *             {@code null}
 727      * @see        java.lang.Runtime#getRuntime()
 728      * @see        java.lang.SecurityException
 729      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 730      */
 731     @CallerSensitive
 732     public void load(String filename) {
 733         load0(Reflection.getCallerClass(), filename);
 734     }
 735 
 736     void load0(Class<?> fromClass, String filename) {
 737         SecurityManager security = System.getSecurityManager();
 738         if (security != null) {
 739             security.checkLink(filename);
 740         }
 741         if (!(new File(filename).isAbsolute())) {

 742             throw new UnsatisfiedLinkError(
 743                 "Expecting an absolute path of the library: " + filename);
 744         }
 745         ClassLoader.loadLibrary(fromClass, filename, true);
 746     }
 747 
 748     /**
 749      * Loads the native library specified by the {@code libname}
 750      * argument.  The {@code libname} argument must not contain any platform
 751      * specific prefix, file extension or path. If a native library
 752      * called {@code libname} is statically linked with the VM, then the
 753      * JNI_OnLoad_{@code libname} function exported by the library is invoked.
 754      * See the <a href="{@docRoot}/../specs/jni/index.html"> JNI Specification</a>
 755      * for more details.
 756      *
 757      * Otherwise, the libname argument is loaded from a system library
 758      * location and mapped to a native library image in an implementation-
 759      * dependent manner.
 760      * <p>
 761      * First, if there is a security manager, its {@code checkLink}
 762      * method is called with the {@code libname} as its argument.
 763      * This may result in a security exception.
 764      * <p>
 765      * The method {@link System#loadLibrary(String)} is the conventional


 787      *             native library image by the host system.
 788      * @throws     NullPointerException if {@code libname} is
 789      *             {@code null}
 790      * @see        java.lang.SecurityException
 791      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 792      */
 793     @CallerSensitive
 794     public void loadLibrary(String libname) {
 795         loadLibrary0(Reflection.getCallerClass(), libname);
 796     }
 797 
 798     void loadLibrary0(Class<?> fromClass, String libname) {
 799         SecurityManager security = System.getSecurityManager();
 800         if (security != null) {
 801             security.checkLink(libname);
 802         }
 803         if (libname.indexOf((int)File.separatorChar) != -1) {
 804             throw new UnsatisfiedLinkError(
 805                 "Directory separator should not appear in library name: " + libname);
 806         }
 807         ClassLoader.loadLibrary(fromClass, libname, false);
 808     }
 809 
 810     /**
 811      * Returns the version of the Java Runtime Environment as a {@link Version}.
 812      *
 813      * @return  the {@link Version} of the Java Runtime Environment
 814      *
 815      * @since  9
 816      */
 817     public static Version version() {
 818         if (version == null) {
 819             version = new Version(VersionProps.versionNumbers(),
 820                     VersionProps.pre(), VersionProps.build(),
 821                     VersionProps.optional());
 822         }
 823         return version;
 824     }
 825 
 826     /**
 827      * A representation of a version string for an implementation of the


   1 /*
   2  * Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Azul Systems, Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.  Oracle designates this
   9  * particular file as subject to the "Classpath" exception as provided
  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 
  27 package java.lang;
  28 
  29 import java.io.*;
  30 import java.math.BigInteger;
  31 import java.util.regex.Matcher;
  32 import java.util.regex.Pattern;
  33 import java.util.stream.Collectors;
  34 import java.util.List;
  35 import java.util.Optional;
  36 import java.util.StringTokenizer;
  37 
  38 import jdk.internal.access.SharedSecrets;
  39 import jdk.internal.loader.NativeLibrary;
  40 import jdk.internal.reflect.CallerSensitive;
  41 import jdk.internal.reflect.Reflection;
  42 
  43 /**
  44  * Every Java application has a single instance of class
  45  * {@code Runtime} that allows the application to interface with
  46  * the environment in which the application is running. The current
  47  * runtime can be obtained from the {@code getRuntime} method.
  48  * <p>
  49  * An application cannot create its own instance of this class.
  50  *
  51  * @author  unascribed
  52  * @see     java.lang.Runtime#getRuntime()
  53  * @since   1.0
  54  */
  55 
  56 public class Runtime {
  57     private static final Runtime currentRuntime = new Runtime();
  58 
  59     private static Version version;


 722      * @throws     UnsatisfiedLinkError  if either the filename is not an
 723      *             absolute path name, the native library is not statically
 724      *             linked with the VM, or the library cannot be mapped to
 725      *             a native library image by the host system.
 726      * @throws     NullPointerException if {@code filename} is
 727      *             {@code null}
 728      * @see        java.lang.Runtime#getRuntime()
 729      * @see        java.lang.SecurityException
 730      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 731      */
 732     @CallerSensitive
 733     public void load(String filename) {
 734         load0(Reflection.getCallerClass(), filename);
 735     }
 736 
 737     void load0(Class<?> fromClass, String filename) {
 738         SecurityManager security = System.getSecurityManager();
 739         if (security != null) {
 740             security.checkLink(filename);
 741         }
 742         File file = new File(filename);
 743         if (!file.isAbsolute()) {
 744             throw new UnsatisfiedLinkError(
 745                 "Expecting an absolute path of the library: " + filename);
 746         }
 747         ClassLoader.loadLibrary(fromClass, file);
 748     }
 749 
 750     /**
 751      * Loads the native library specified by the {@code libname}
 752      * argument.  The {@code libname} argument must not contain any platform
 753      * specific prefix, file extension or path. If a native library
 754      * called {@code libname} is statically linked with the VM, then the
 755      * JNI_OnLoad_{@code libname} function exported by the library is invoked.
 756      * See the <a href="{@docRoot}/../specs/jni/index.html"> JNI Specification</a>
 757      * for more details.
 758      *
 759      * Otherwise, the libname argument is loaded from a system library
 760      * location and mapped to a native library image in an implementation-
 761      * dependent manner.
 762      * <p>
 763      * First, if there is a security manager, its {@code checkLink}
 764      * method is called with the {@code libname} as its argument.
 765      * This may result in a security exception.
 766      * <p>
 767      * The method {@link System#loadLibrary(String)} is the conventional


 789      *             native library image by the host system.
 790      * @throws     NullPointerException if {@code libname} is
 791      *             {@code null}
 792      * @see        java.lang.SecurityException
 793      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 794      */
 795     @CallerSensitive
 796     public void loadLibrary(String libname) {
 797         loadLibrary0(Reflection.getCallerClass(), libname);
 798     }
 799 
 800     void loadLibrary0(Class<?> fromClass, String libname) {
 801         SecurityManager security = System.getSecurityManager();
 802         if (security != null) {
 803             security.checkLink(libname);
 804         }
 805         if (libname.indexOf((int)File.separatorChar) != -1) {
 806             throw new UnsatisfiedLinkError(
 807                 "Directory separator should not appear in library name: " + libname);
 808         }
 809         ClassLoader.loadLibrary(fromClass, libname);
 810     }
 811 
 812     /**
 813      * Returns the version of the Java Runtime Environment as a {@link Version}.
 814      *
 815      * @return  the {@link Version} of the Java Runtime Environment
 816      *
 817      * @since  9
 818      */
 819     public static Version version() {
 820         if (version == null) {
 821             version = new Version(VersionProps.versionNumbers(),
 822                     VersionProps.pre(), VersionProps.build(),
 823                     VersionProps.optional());
 824         }
 825         return version;
 826     }
 827 
 828     /**
 829      * A representation of a version string for an implementation of the


< prev index next >