1 7 8 package java.lang; 9 10 import java.io.*; 11 import java.util.StringTokenizer ; 12 13 26 27 public class Runtime { 28 private static Runtime currentRuntime = new Runtime (); 29 30 38 public static Runtime getRuntime() { 39 return currentRuntime; 40 } 41 42 43 private Runtime() {} 44 45 85 public void exit(int status) { 86 SecurityManager security = System.getSecurityManager(); 87 if (security != null) { 88 security.checkExit(status); 89 } 90 Shutdown.exit(status); 91 } 92 93 185 public void addShutdownHook(Thread hook) { 186 SecurityManager sm = System.getSecurityManager(); 187 if (sm != null) { 188 sm.checkPermission(new RuntimePermission ("shutdownHooks")); 189 } 190 Shutdown.add(hook); 191 } 192 193 213 public boolean removeShutdownHook(Thread hook) { 214 SecurityManager sm = System.getSecurityManager(); 215 if (sm != null) { 216 sm.checkPermission(new RuntimePermission ("shutdownHooks")); 217 } 218 return Shutdown.remove(hook); 219 } 220 221 249 public void halt(int status) { 250 SecurityManager sm = System.getSecurityManager(); 251 if (sm != null) { 252 sm.checkExit(status); 253 } 254 Shutdown.halt(status); 255 } 256 257 283 @Deprecated 284 public static void runFinalizersOnExit(boolean value) { 285 SecurityManager security = System.getSecurityManager(); 286 if (security != null) { 287 try { 288 security.checkExit(0); 289 } catch (SecurityException e) { 290 throw new SecurityException ("runFinalizersOnExit"); 291 } 292 } 293 Shutdown.setRunFinalizersOnExit(value); 294 } 295 296 325 public Process exec(String command) throws IOException { 326 return exec(command, null, null); 327 } 328 329 366 public Process exec(String command, String [] envp) throws IOException { 367 return exec(command, envp, null); 368 } 369 370 420 public Process exec(String command, String [] envp, File dir) 421 throws IOException { 422 if (command.length() == 0) 423 throw new IllegalArgumentException ("Empty command"); 424 425 StringTokenizer st = new StringTokenizer (command); 426 String [] cmdarray = new String [st.countTokens()]; 427 for (int i = 0; st.hasMoreTokens(); i++) 428 cmdarray[i] = st.nextToken(); 429 return exec(cmdarray, envp, dir); 430 } 431 432 463 public Process exec(String cmdarray[]) throws IOException { 464 return exec(cmdarray, null, null); 465 } 466 467 506 public Process exec(String [] cmdarray, String [] envp) throws IOException { 507 return exec(cmdarray, envp, null); 508 } 509 510 511 589 public Process exec(String [] cmdarray, String [] envp, File dir) 590 throws IOException { 591 return new ProcessBuilder (cmdarray) 592 .environment(envp) 593 .directory(dir) 594 .start(); 595 } 596 597 609 public native int availableProcessors(); 610 611 620 public native long freeMemory(); 621 622 633 public native long totalMemory(); 634 635 644 public native long maxMemory(); 645 646 662 public native void gc(); 663 664 665 private static native void runFinalization0(); 666 667 685 public void runFinalization() { 686 runFinalization0(); 687 } 688 689 707 public native void traceInstructions(boolean on); 708 709 725 public native void traceMethodCalls(boolean on); 726 727 756 public void load(String filename) { 757 load0(System.getCallerClass(), filename); 758 } 759 760 synchronized void load0(Class fromClass, String filename) { 761 SecurityManager security = System.getSecurityManager(); 762 if (security != null) { 763 security.checkLink(filename); 764 } 765 if (!(new File(filename).isAbsolute())) { 766 throw new UnsatisfiedLinkError ( 767 "Expecting an absolute path of the library: " + filename); 768 } 769 ClassLoader.loadLibrary(fromClass, filename, true); 770 } 771 772 809 public void loadLibrary(String libname) { 810 loadLibrary0(System.getCallerClass(), libname); 811 } 812 813 synchronized void loadLibrary0(Class fromClass, String libname) { 814 SecurityManager security = System.getSecurityManager(); 815 if (security != null) { 816 security.checkLink(libname); 817 } 818 if (libname.indexOf((int)File.separatorChar) != -1) { 819 throw new UnsatisfiedLinkError ( 820 "Directory separator should not appear in library name: " + libname); 821 } 822 ClassLoader.loadLibrary(fromClass, libname, false); 823 } 824 825 846 @Deprecated 847 public InputStream getLocalizedInputStream(InputStream in) { 848 return in; 849 } 850 851 874 @Deprecated 875 public OutputStream getLocalizedOutputStream(OutputStream out) { 876 return out; 877 } 878 879 } 880 | Popular Tags |