Runtime.java 3.79 KB
Newer Older
Tom Tromey committed
1 2
// Runtime.java - Runtime class.

3
/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
Tom Tromey committed
4 5 6 7 8 9 10 11 12 13 14 15

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.lang;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
16
import java.util.StringTokenizer;
Tom Tromey committed
17 18 19 20 21 22 23 24 25

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date August 27, 1998 
 */

/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 * "The Java Language Specification", ISBN 0-201-63451-1
 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
26
 * Status:  All 1.1 methods exist.  exec() is not fully implemented.
Tom Tromey committed
27 28 29 30 31 32
 */

public class Runtime
{
  public Process exec (String prog) throws IOException
  {
33
    return exec (prog, null);
Tom Tromey committed
34 35 36 37
  }

  public Process exec (String prog, String[] envp) throws IOException
  {
38 39 40 41
    StringTokenizer st = new StringTokenizer(prog);
    String[] a = new String[st.countTokens ()];
    for (int i = 0; i < a.length; i++)
      a[i] = st.nextToken ();
Tom Tromey committed
42 43 44 45 46 47 48 49 50 51 52 53 54
    return exec (a, envp);
  }

  public Process exec (String[] progarray) throws IOException
  {
    return exec (progarray, null);
  }

  public Process exec (String[] progarray, String[] envp) throws IOException
  {
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkExec(progarray[0]);
55
    return new ConcreteProcess (progarray, envp);
Tom Tromey committed
56 57 58 59 60 61 62 63 64 65
  }

  private final static void checkExit (int status)
  {
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkExit(status);
  }

  public native void exit (int status);
66 67 68 69
  
  // Shutdown the runtime without a SecurityManager check. libgcj uses this 
  // exit function internally.
  final native void _exit (int status);
Tom Tromey committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

  public native long freeMemory ();
  public native void gc ();

  // Deprecated in 1.1.  We implement what the JCL book says.
  public InputStream getLocalizedInputStream (InputStream in)
  {
    return in;
  }

  // Deprecated in 1.1.  We implement what the JCL book says.
  public OutputStream getLocalizedOutputStream (OutputStream out)
  {
    return out;
  }

  public static Runtime getRuntime ()
  {
    return self;
  }

  private final void checkLink (String lib)
  {
    if (lib == null)
      throw new NullPointerException ();
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkLink(lib);
  }

100 101 102 103 104 105 106 107 108 109 110
  private native void _load (String pathname, boolean do_search);

  public void load (String pathname)
  {
    _load (pathname, false);
  }

  public void loadLibrary (String libname)
  {
    _load (libname, true);
  }
Tom Tromey committed
111

112 113 114 115 116 117
  // This is a helper function for the ClassLoader which can load
  // compiled libraries.  Returns true if library (which is just the
  // base name -- path searching is done by this function) was loaded,
  // false otherwise.
  native boolean loadLibraryInternal (String libname);

Tom Tromey committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  public native void runFinalization ();

  // This method is static in JDK 1.1, but isn't listed as static in
  // the books.  It is marked as static in the 1.2 docs.
  public static void runFinalizersOnExit (boolean run)
  {
    // The status we pass to the security check is unspecified.
    checkExit (0);
    self.finalize_on_exit = run;
  }

  public native long totalMemory ();
  public native void traceInstructions (boolean on);
  public native void traceMethodCalls (boolean on);

Tom Tromey committed
133 134 135
  // A helper for the constructor.
  private final native void init ();

Tom Tromey committed
136 137 138
  // The sole constructor.
  private Runtime ()
  {
Tom Tromey committed
139
    init ();
Tom Tromey committed
140 141 142 143 144 145 146 147
  }

  // Private data.
  private static Runtime self = new Runtime ();
  // FIXME: for now this can't be static.  If it is, our compiler will
  // mark it as local, and it will be inaccessible to natRuntime.cc.
  private boolean finalize_on_exit;
}