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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2011, 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


 732 
 733     /**
 734      * Enables/Disables tracing of method calls.
 735      * If the <code>boolean</code> argument is <code>true</code>, this
 736      * method suggests that the Java virtual machine emit debugging
 737      * information for each method in the virtual machine as it is
 738      * called. The format of this information, and the file or other output
 739      * stream to which it is emitted, depends on the host environment. The
 740      * virtual machine may ignore this request if it does not support
 741      * this feature.
 742      * <p>
 743      * Calling this method with argument false suggests that the
 744      * virtual machine cease emitting per-call debugging information.
 745      *
 746      * @param   on   <code>true</code> to enable instruction tracing;
 747      *               <code>false</code> to disable this feature.
 748      */
 749     public native void traceMethodCalls(boolean on);
 750 
 751     /**
 752      * Loads the specified filename as a dynamic library. The filename
 753      * argument must be a complete path name,
 754      * (for example
 755      * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>).













 756      * <p>
 757      * First, if there is a security manager, its <code>checkLink</code>
 758      * method is called with the <code>filename</code> as its argument.
 759      * This may result in a security exception.
 760      * <p>
 761      * This is similar to the method {@link #loadLibrary(String)}, but it
 762      * accepts a general file name as an argument rather than just a library
 763      * name, allowing any file of native code to be loaded.
 764      * <p>
 765      * The method {@link System#load(String)} is the conventional and
 766      * convenient means of invoking this method.
 767      *
 768      * @param      filename   the file to load.
 769      * @exception  SecurityException  if a security manager exists and its
 770      *             <code>checkLink</code> method doesn't allow
 771      *             loading of the specified dynamic library
 772      * @exception  UnsatisfiedLinkError  if the file does not exist.



 773      * @exception  NullPointerException if <code>filename</code> is
 774      *             <code>null</code>
 775      * @see        java.lang.Runtime#getRuntime()
 776      * @see        java.lang.SecurityException
 777      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 778      */
 779     public void load(String filename) {
 780         load0(System.getCallerClass(), filename);
 781     }
 782 
 783     synchronized void load0(Class<?> fromClass, String filename) {
 784         SecurityManager security = System.getSecurityManager();
 785         if (security != null) {
 786             security.checkLink(filename);
 787         }
 788         if (!(new File(filename).isAbsolute())) {
 789             throw new UnsatisfiedLinkError(
 790                 "Expecting an absolute path of the library: " + filename);
 791         }
 792         ClassLoader.loadLibrary(fromClass, filename, true);
 793     }
 794 
 795     /**
 796      * Loads the dynamic library with the specified library name.
 797      * A file containing native code is loaded from the local file system
 798      * from a place where library files are conventionally obtained. The
 799      * details of this process are implementation-dependent. The
 800      * mapping from a library name to a specific filename is done in a
 801      * system-specific manner.




 802      * <p>
 803      * First, if there is a security manager, its <code>checkLink</code>
 804      * method is called with the <code>libname</code> as its argument.
 805      * This may result in a security exception.
 806      * <p>
 807      * The method {@link System#loadLibrary(String)} is the conventional
 808      * and convenient means of invoking this method. If native
 809      * methods are to be used in the implementation of a class, a standard
 810      * strategy is to put the native code in a library file (call it
 811      * <code>LibFile</code>) and then to put a static initializer:
 812      * <blockquote><pre>
 813      * static { System.loadLibrary("LibFile"); }
 814      * </pre></blockquote>
 815      * within the class declaration. When the class is loaded and
 816      * initialized, the necessary native code implementation for the native
 817      * methods will then be loaded as well.
 818      * <p>
 819      * If this method is called more than once with the same library
 820      * name, the second and subsequent calls are ignored.
 821      *
 822      * @param      libname   the name of the library.
 823      * @exception  SecurityException  if a security manager exists and its
 824      *             <code>checkLink</code> method doesn't allow
 825      *             loading of the specified dynamic library
 826      * @exception  UnsatisfiedLinkError  if the library does not exist.



 827      * @exception  NullPointerException if <code>libname</code> is
 828      *             <code>null</code>
 829      * @see        java.lang.SecurityException
 830      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 831      */
 832     public void loadLibrary(String libname) {
 833         loadLibrary0(System.getCallerClass(), libname);
 834     }
 835 
 836     synchronized void loadLibrary0(Class<?> fromClass, String libname) {
 837         SecurityManager security = System.getSecurityManager();
 838         if (security != null) {
 839             security.checkLink(libname);
 840         }
 841         if (libname.indexOf((int)File.separatorChar) != -1) {
 842             throw new UnsatisfiedLinkError(
 843     "Directory separator should not appear in library name: " + libname);
 844         }
 845         ClassLoader.loadLibrary(fromClass, libname, false);
 846     }


   1 /*
   2  * Copyright (c) 1995, 2013, 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


 732 
 733     /**
 734      * Enables/Disables tracing of method calls.
 735      * If the <code>boolean</code> argument is <code>true</code>, this
 736      * method suggests that the Java virtual machine emit debugging
 737      * information for each method in the virtual machine as it is
 738      * called. The format of this information, and the file or other output
 739      * stream to which it is emitted, depends on the host environment. The
 740      * virtual machine may ignore this request if it does not support
 741      * this feature.
 742      * <p>
 743      * Calling this method with argument false suggests that the
 744      * virtual machine cease emitting per-call debugging information.
 745      *
 746      * @param   on   <code>true</code> to enable instruction tracing;
 747      *               <code>false</code> to disable this feature.
 748      */
 749     public native void traceMethodCalls(boolean on);
 750 
 751     /**
 752      * Loads the native library specified by the filename argument.  The filename
 753      * argument must be an absolute path name.
 754      * (for example
 755      * <code>Runtime.getRuntime().load("/home/avh/lib/libX11.so");</code>).
 756      *
 757      * If the filename argument, when stripped of any platform-specific library 
 758      * prefix, path, and file extension, indicates a library whose name is,
 759      * for example, L, and a native library called L is statically linked
 760      * with the native application or the VM (which itself may be statically
 761      * linked into the native application), then the JNI_OnLoad_L
 762      * function exported by the library is invoked rather than attempting
 763      * to load a dynamic library.
 764      * A filename matching the argument does not have to exist in the file
 765      * system. See the JNI Specification for more details.
 766      *
 767      * Otherwise, the filename argument is mapped to a native library image in
 768      * an implementation-dependent manner.
 769      * <p>
 770      * First, if there is a security manager, its <code>checkLink</code>
 771      * method is called with the <code>filename</code> as its argument.
 772      * This may result in a security exception.
 773      * <p>
 774      * This is similar to the method {@link #loadLibrary(String)}, but it
 775      * accepts a general file name as an argument rather than just a library
 776      * name, allowing any file of native code to be loaded.
 777      * <p>
 778      * The method {@link System#load(String)} is the conventional and
 779      * convenient means of invoking this method.
 780      *
 781      * @param      filename   the file to load.
 782      * @exception  SecurityException  if a security manager exists and its
 783      *             <code>checkLink</code> method doesn't allow
 784      *             loading of the specified dynamic library
 785      * @exception  UnsatisfiedLinkError  if either the filename is not an 
 786      *             absolute path name, the native library is not statically 
 787      *             linked with the VM, or the library cannot be mapped to 
 788      *             a native library image by the host system.
 789      * @exception  NullPointerException if <code>filename</code> is
 790      *             <code>null</code>
 791      * @see        java.lang.Runtime#getRuntime()
 792      * @see        java.lang.SecurityException
 793      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 794      */
 795     public void load(String filename) {
 796         load0(System.getCallerClass(), filename);
 797     }
 798 
 799     synchronized void load0(Class<?> fromClass, String filename) {
 800         SecurityManager security = System.getSecurityManager();
 801         if (security != null) {
 802             security.checkLink(filename);
 803         }
 804         if (!(new File(filename).isAbsolute())) {
 805             throw new UnsatisfiedLinkError(
 806                 "Expecting an absolute path of the library: " + filename);
 807         }
 808         ClassLoader.loadLibrary(fromClass, filename, true);
 809     }
 810 
 811     /**
 812      * Loads the native library specified by the <code>libname</code>
 813      * argument.  The <code>libname</code> argument must not contain any platform
 814      * specific prefix, file extension or path. If a native library
 815      * called <code>libname</code> is statically linked with the VM, then the
 816      * JNI_OnLoad_<code>libname</code> function exported by the library is invoked.
 817      * See the JNI Specification for more details.
 818      *
 819      * Otherwise, the libname argument is loaded from a system library
 820      * location and mapped to a native library image in an implementation-
 821      * dependent manner.
 822      * <p>
 823      * First, if there is a security manager, its <code>checkLink</code>
 824      * method is called with the <code>libname</code> as its argument.
 825      * This may result in a security exception.
 826      * <p>
 827      * The method {@link System#loadLibrary(String)} is the conventional
 828      * and convenient means of invoking this method. If native
 829      * methods are to be used in the implementation of a class, a standard
 830      * strategy is to put the native code in a library file (call it
 831      * <code>LibFile</code>) and then to put a static initializer:
 832      * <blockquote><pre>
 833      * static { System.loadLibrary("LibFile"); }
 834      * </pre></blockquote>
 835      * within the class declaration. When the class is loaded and
 836      * initialized, the necessary native code implementation for the native
 837      * methods will then be loaded as well.
 838      * <p>
 839      * If this method is called more than once with the same library
 840      * name, the second and subsequent calls are ignored.
 841      *
 842      * @param      libname   the name of the library.
 843      * @exception  SecurityException  if a security manager exists and its
 844      *             <code>checkLink</code> method doesn't allow
 845      *             loading of the specified dynamic library
 846      * @exception  UnsatisfiedLinkError if either the libname argument 
 847      *             contains a file path, the native library is not statically 
 848      *             linked with the VM,  or the library cannot be mapped to a 
 849      *             native library image by the host system.
 850      * @exception  NullPointerException if <code>libname</code> is
 851      *             <code>null</code>
 852      * @see        java.lang.SecurityException
 853      * @see        java.lang.SecurityManager#checkLink(java.lang.String)
 854      */
 855     public void loadLibrary(String libname) {
 856         loadLibrary0(System.getCallerClass(), libname);
 857     }
 858 
 859     synchronized void loadLibrary0(Class<?> fromClass, String libname) {
 860         SecurityManager security = System.getSecurityManager();
 861         if (security != null) {
 862             security.checkLink(libname);
 863         }
 864         if (libname.indexOf((int)File.separatorChar) != -1) {
 865             throw new UnsatisfiedLinkError(
 866     "Directory separator should not appear in library name: " + libname);
 867         }
 868         ClassLoader.loadLibrary(fromClass, libname, false);
 869     }