Runtime.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* Runtime.java -- access to the VM process
  2. Copyright (C) 1998, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. import gnu.classpath.SystemProperties;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.OutputStream;
  37. import java.util.HashSet;
  38. import java.util.Iterator;
  39. import java.util.Set;
  40. import java.util.StringTokenizer;
  41. /**
  42. * Runtime represents the Virtual Machine.
  43. *
  44. * @author John Keiser
  45. * @author Eric Blake (ebb9@email.byu.edu)
  46. * @author Jeroen Frijters
  47. */
  48. // No idea why this class isn't final, since you can't build a subclass!
  49. public class Runtime
  50. {
  51. /**
  52. * The library path, to search when loading libraries. We can also safely use
  53. * this as a lock for synchronization.
  54. */
  55. private final String[] libpath;
  56. static
  57. {
  58. init();
  59. }
  60. /**
  61. * The thread that started the exit sequence. Access to this field must
  62. * be thread-safe; lock on libpath to avoid deadlock with user code.
  63. * <code>runFinalization()</code> may want to look at this to see if ALL
  64. * finalizers should be run, because the virtual machine is about to halt.
  65. */
  66. private Thread exitSequence;
  67. /**
  68. * All shutdown hooks. This is initialized lazily, and set to null once all
  69. * shutdown hooks have run. Access to this field must be thread-safe; lock
  70. * on libpath to avoid deadlock with user code.
  71. */
  72. private Set shutdownHooks;
  73. /** True if we should finalize on exit. */
  74. private boolean finalizeOnExit;
  75. /**
  76. * The one and only runtime instance.
  77. */
  78. private static final Runtime current = new Runtime();
  79. /**
  80. * Not instantiable by a user, this should only create one instance.
  81. */
  82. private Runtime()
  83. {
  84. if (current != null)
  85. throw new InternalError("Attempt to recreate Runtime");
  86. // We don't use libpath in the libgcj implementation. We still
  87. // set it to something to allow the various synchronizations to
  88. // work.
  89. libpath = new String[0];
  90. }
  91. /**
  92. * Get the current Runtime object for this JVM. This is necessary to access
  93. * the many instance methods of this class.
  94. *
  95. * @return the current Runtime object
  96. */
  97. public static Runtime getRuntime()
  98. {
  99. return current;
  100. }
  101. /**
  102. * Exit the Java runtime. This method will either throw a SecurityException
  103. * or it will never return. The status code is returned to the system; often
  104. * a non-zero status code indicates an abnormal exit. Of course, there is a
  105. * security check, <code>checkExit(status)</code>.
  106. *
  107. * <p>First, all shutdown hooks are run, in unspecified order, and
  108. * concurrently. Next, if finalization on exit has been enabled, all pending
  109. * finalizers are run. Finally, the system calls <code>halt</code>.</p>
  110. *
  111. * <p>If this is run a second time after shutdown has already started, there
  112. * are two actions. If shutdown hooks are still executing, it blocks
  113. * indefinitely. Otherwise, if the status is nonzero it halts immediately;
  114. * if it is zero, it blocks indefinitely. This is typically called by
  115. * <code>System.exit</code>.</p>
  116. *
  117. * @param status the status to exit with
  118. * @throws SecurityException if permission is denied
  119. * @see #addShutdownHook(Thread)
  120. * @see #runFinalizersOnExit(boolean)
  121. * @see #runFinalization()
  122. * @see #halt(int)
  123. */
  124. public void exit(int status)
  125. {
  126. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  127. if (sm != null)
  128. sm.checkExit(status);
  129. exitNoChecks(status);
  130. }
  131. // Accessor to avoid adding a vtable slot.
  132. static void exitNoChecksAccessor(int status)
  133. {
  134. current.exitNoChecks(status);
  135. }
  136. // Private since we can't add a vtable slot in 4.1.x.
  137. private void exitNoChecks(int status)
  138. {
  139. if (runShutdownHooks())
  140. exitInternal(status);
  141. // Someone else already called runShutdownHooks().
  142. // Make sure we are not/no longer in the shutdownHooks set.
  143. // And wait till the thread that is calling runShutdownHooks() finishes.
  144. synchronized (libpath)
  145. {
  146. if (shutdownHooks != null)
  147. {
  148. shutdownHooks.remove(Thread.currentThread());
  149. // Interrupt the exit sequence thread, in case it was waiting
  150. // inside a join on our thread.
  151. exitSequence.interrupt();
  152. // Shutdown hooks are still running, so we clear status to
  153. // make sure we don't halt.
  154. status = 0;
  155. }
  156. }
  157. // If exit() is called again after the shutdown hooks have run, but
  158. // while finalization for exit is going on and the status is non-zero
  159. // we halt immediately.
  160. if (status != 0)
  161. exitInternal(status);
  162. while (true)
  163. try
  164. {
  165. exitSequence.join();
  166. }
  167. catch (InterruptedException e)
  168. {
  169. // Ignore, we've suspended indefinitely to let all shutdown
  170. // hooks complete, and to let any non-zero exits through, because
  171. // this is a duplicate call to exit(0).
  172. }
  173. }
  174. /**
  175. * On first invocation, run all the shutdown hooks and return true.
  176. * Any subsequent invocations will simply return false.
  177. * Note that it is package accessible so that VMRuntime can call it
  178. * when VM exit is not triggered by a call to Runtime.exit().
  179. *
  180. * @return was the current thread the first one to call this method?
  181. */
  182. boolean runShutdownHooks()
  183. {
  184. boolean first = false;
  185. synchronized (libpath) // Synch on libpath, not this, to avoid deadlock.
  186. {
  187. if (exitSequence == null)
  188. {
  189. first = true;
  190. exitSequence = Thread.currentThread();
  191. if (shutdownHooks != null)
  192. {
  193. Iterator i = shutdownHooks.iterator();
  194. while (i.hasNext()) // Start all shutdown hooks.
  195. try
  196. {
  197. ((Thread) i.next()).start();
  198. }
  199. catch (IllegalThreadStateException e)
  200. {
  201. i.remove();
  202. }
  203. }
  204. }
  205. }
  206. if (first)
  207. {
  208. if (shutdownHooks != null)
  209. {
  210. // Check progress of all shutdown hooks. As a hook completes,
  211. // remove it from the set. If a hook calls exit, it removes
  212. // itself from the set, then waits indefinitely on the
  213. // exitSequence thread. Once the set is empty, set it to null to
  214. // signal all finalizer threads that halt may be called.
  215. while (true)
  216. {
  217. Thread[] hooks;
  218. synchronized (libpath)
  219. {
  220. hooks = new Thread[shutdownHooks.size()];
  221. shutdownHooks.toArray(hooks);
  222. }
  223. if (hooks.length == 0)
  224. break;
  225. for (int i = 0; i < hooks.length; i++)
  226. {
  227. try
  228. {
  229. synchronized (libpath)
  230. {
  231. if (!shutdownHooks.contains(hooks[i]))
  232. continue;
  233. }
  234. hooks[i].join();
  235. synchronized (libpath)
  236. {
  237. shutdownHooks.remove(hooks[i]);
  238. }
  239. }
  240. catch (InterruptedException x)
  241. {
  242. // continue waiting on the next thread
  243. }
  244. }
  245. }
  246. synchronized (libpath)
  247. {
  248. shutdownHooks = null;
  249. }
  250. }
  251. // Run finalization on all finalizable objects (even if they are
  252. // still reachable).
  253. runFinalizationForExit();
  254. }
  255. return first;
  256. }
  257. /**
  258. * Register a new shutdown hook. This is invoked when the program exits
  259. * normally (because all non-daemon threads ended, or because
  260. * <code>System.exit</code> was invoked), or when the user terminates
  261. * the virtual machine (such as by typing ^C, or logging off). There is
  262. * a security check to add hooks,
  263. * <code>RuntimePermission("shutdownHooks")</code>.
  264. *
  265. * <p>The hook must be an initialized, but unstarted Thread. The threads
  266. * are run concurrently, and started in an arbitrary order; and user
  267. * threads or daemons may still be running. Once shutdown hooks have
  268. * started, they must all complete, or else you must use <code>halt</code>,
  269. * to actually finish the shutdown sequence. Attempts to modify hooks
  270. * after shutdown has started result in IllegalStateExceptions.</p>
  271. *
  272. * <p>It is imperative that you code shutdown hooks defensively, as you
  273. * do not want to deadlock, and have no idea what other hooks will be
  274. * running concurrently. It is also a good idea to finish quickly, as the
  275. * virtual machine really wants to shut down!</p>
  276. *
  277. * <p>There are no guarantees that such hooks will run, as there are ways
  278. * to forcibly kill a process. But in such a drastic case, shutdown hooks
  279. * would do little for you in the first place.</p>
  280. *
  281. * @param hook an initialized, unstarted Thread
  282. * @throws IllegalArgumentException if the hook is already registered or run
  283. * @throws IllegalStateException if the virtual machine is already in
  284. * the shutdown sequence
  285. * @throws SecurityException if permission is denied
  286. * @since 1.3
  287. * @see #removeShutdownHook(Thread)
  288. * @see #exit(int)
  289. * @see #halt(int)
  290. */
  291. public void addShutdownHook(Thread hook)
  292. {
  293. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  294. if (sm != null)
  295. sm.checkPermission(new RuntimePermission("shutdownHooks"));
  296. if (hook.isAlive() || hook.getThreadGroup() == null)
  297. throw new IllegalArgumentException("The hook thread " + hook + " must not have been already run or started");
  298. synchronized (libpath)
  299. {
  300. if (exitSequence != null)
  301. throw new IllegalStateException("The Virtual Machine is exiting. It is not possible anymore to add any hooks");
  302. if (shutdownHooks == null)
  303. shutdownHooks = new HashSet(); // Lazy initialization.
  304. if (! shutdownHooks.add(hook))
  305. throw new IllegalArgumentException(hook.toString() + " had already been inserted");
  306. }
  307. }
  308. /**
  309. * De-register a shutdown hook. As when you registered it, there is a
  310. * security check to remove hooks,
  311. * <code>RuntimePermission("shutdownHooks")</code>.
  312. *
  313. * @param hook the hook to remove
  314. * @return true if the hook was successfully removed, false if it was not
  315. * registered in the first place
  316. * @throws IllegalStateException if the virtual machine is already in
  317. * the shutdown sequence
  318. * @throws SecurityException if permission is denied
  319. * @since 1.3
  320. * @see #addShutdownHook(Thread)
  321. * @see #exit(int)
  322. * @see #halt(int)
  323. */
  324. public boolean removeShutdownHook(Thread hook)
  325. {
  326. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  327. if (sm != null)
  328. sm.checkPermission(new RuntimePermission("shutdownHooks"));
  329. synchronized (libpath)
  330. {
  331. if (exitSequence != null)
  332. throw new IllegalStateException();
  333. if (shutdownHooks != null)
  334. return shutdownHooks.remove(hook);
  335. }
  336. return false;
  337. }
  338. /**
  339. * Forcibly terminate the virtual machine. This call never returns. It is
  340. * much more severe than <code>exit</code>, as it bypasses all shutdown
  341. * hooks and initializers. Use caution in calling this! Of course, there is
  342. * a security check, <code>checkExit(status)</code>.
  343. *
  344. * @param status the status to exit with
  345. * @throws SecurityException if permission is denied
  346. * @since 1.3
  347. * @see #exit(int)
  348. * @see #addShutdownHook(Thread)
  349. */
  350. public void halt(int status)
  351. {
  352. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  353. if (sm != null)
  354. sm.checkExit(status);
  355. exitInternal(status);
  356. }
  357. /**
  358. * Tell the VM to run the finalize() method on every single Object before
  359. * it exits. Note that the JVM may still exit abnormally and not perform
  360. * this, so you still don't have a guarantee. And besides that, this is
  361. * inherently unsafe in multi-threaded code, as it may result in deadlock
  362. * as multiple threads compete to manipulate objects. This value defaults to
  363. * <code>false</code>. There is a security check, <code>checkExit(0)</code>.
  364. *
  365. * @param finalizeOnExit whether to finalize all Objects on exit
  366. * @throws SecurityException if permission is denied
  367. * @see #exit(int)
  368. * @see #gc()
  369. * @since 1.1
  370. * @deprecated never rely on finalizers to do a clean, thread-safe,
  371. * mop-up from your code
  372. */
  373. public static void runFinalizersOnExit(boolean finalizeOnExit)
  374. {
  375. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  376. if (sm != null)
  377. sm.checkExit(0);
  378. current.finalizeOnExit = finalizeOnExit;
  379. }
  380. /**
  381. * Create a new subprocess with the specified command line. Calls
  382. * <code>exec(cmdline, null, null)</code>. A security check is performed,
  383. * <code>checkExec</code>.
  384. *
  385. * @param cmdline the command to call
  386. * @return the Process object
  387. * @throws SecurityException if permission is denied
  388. * @throws IOException if an I/O error occurs
  389. * @throws NullPointerException if cmdline is null
  390. * @throws IndexOutOfBoundsException if cmdline is ""
  391. */
  392. public Process exec(String cmdline) throws IOException
  393. {
  394. return exec(cmdline, null, null);
  395. }
  396. /**
  397. * Create a new subprocess with the specified command line and environment.
  398. * If the environment is null, the process inherits the environment of
  399. * this process. Calls <code>exec(cmdline, env, null)</code>. A security
  400. * check is performed, <code>checkExec</code>.
  401. *
  402. * @param cmdline the command to call
  403. * @param env the environment to use, in the format name=value
  404. * @return the Process object
  405. * @throws SecurityException if permission is denied
  406. * @throws IOException if an I/O error occurs
  407. * @throws NullPointerException if cmdline is null, or env has null entries
  408. * @throws IndexOutOfBoundsException if cmdline is ""
  409. */
  410. public Process exec(String cmdline, String[] env) throws IOException
  411. {
  412. return exec(cmdline, env, null);
  413. }
  414. /**
  415. * Create a new subprocess with the specified command line, environment, and
  416. * working directory. If the environment is null, the process inherits the
  417. * environment of this process. If the directory is null, the process uses
  418. * the current working directory. This splits cmdline into an array, using
  419. * the default StringTokenizer, then calls
  420. * <code>exec(cmdArray, env, dir)</code>. A security check is performed,
  421. * <code>checkExec</code>.
  422. *
  423. * @param cmdline the command to call
  424. * @param env the environment to use, in the format name=value
  425. * @param dir the working directory to use
  426. * @return the Process object
  427. * @throws SecurityException if permission is denied
  428. * @throws IOException if an I/O error occurs
  429. * @throws NullPointerException if cmdline is null, or env has null entries
  430. * @throws IndexOutOfBoundsException if cmdline is ""
  431. * @since 1.3
  432. */
  433. public Process exec(String cmdline, String[] env, File dir)
  434. throws IOException
  435. {
  436. StringTokenizer t = new StringTokenizer(cmdline);
  437. String[] cmd = new String[t.countTokens()];
  438. for (int i = 0; i < cmd.length; i++)
  439. cmd[i] = t.nextToken();
  440. return exec(cmd, env, dir);
  441. }
  442. /**
  443. * Create a new subprocess with the specified command line, already
  444. * tokenized. Calls <code>exec(cmd, null, null)</code>. A security check
  445. * is performed, <code>checkExec</code>.
  446. *
  447. * @param cmd the command to call
  448. * @return the Process object
  449. * @throws SecurityException if permission is denied
  450. * @throws IOException if an I/O error occurs
  451. * @throws NullPointerException if cmd is null, or has null entries
  452. * @throws IndexOutOfBoundsException if cmd is length 0
  453. */
  454. public Process exec(String[] cmd) throws IOException
  455. {
  456. return exec(cmd, null, null);
  457. }
  458. /**
  459. * Create a new subprocess with the specified command line, already
  460. * tokenized, and specified environment. If the environment is null, the
  461. * process inherits the environment of this process. Calls
  462. * <code>exec(cmd, env, null)</code>. A security check is performed,
  463. * <code>checkExec</code>.
  464. *
  465. * @param cmd the command to call
  466. * @param env the environment to use, in the format name=value
  467. * @return the Process object
  468. * @throws SecurityException if permission is denied
  469. * @throws IOException if an I/O error occurs
  470. * @throws NullPointerException if cmd is null, or cmd or env has null
  471. * entries
  472. * @throws IndexOutOfBoundsException if cmd is length 0
  473. */
  474. public Process exec(String[] cmd, String[] env) throws IOException
  475. {
  476. return exec(cmd, env, null);
  477. }
  478. /**
  479. * Create a new subprocess with the specified command line, already
  480. * tokenized, and the specified environment and working directory. If the
  481. * environment is null, the process inherits the environment of this
  482. * process. If the directory is null, the process uses the current working
  483. * directory. A security check is performed, <code>checkExec</code>.
  484. *
  485. * @param cmd the command to call
  486. * @param env the environment to use, in the format name=value
  487. * @param dir the working directory to use
  488. * @return the Process object
  489. * @throws SecurityException if permission is denied
  490. * @throws IOException if an I/O error occurs
  491. * @throws NullPointerException if cmd is null, or cmd or env has null
  492. * entries
  493. * @throws IndexOutOfBoundsException if cmd is length 0
  494. * @since 1.3
  495. */
  496. public Process exec(String[] cmd, String[] env, File dir)
  497. throws IOException
  498. {
  499. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  500. if (sm != null)
  501. sm.checkExec(cmd[0]);
  502. return execInternal(cmd, env, dir);
  503. }
  504. /**
  505. * Returns the number of available processors currently available to the
  506. * virtual machine. This number may change over time; so a multi-processor
  507. * program want to poll this to determine maximal resource usage.
  508. *
  509. * @return the number of processors available, at least 1
  510. */
  511. public native int availableProcessors();
  512. /**
  513. * Find out how much memory is still free for allocating Objects on the heap.
  514. *
  515. * @return the number of bytes of free memory for more Objects
  516. */
  517. public native long freeMemory();
  518. /**
  519. * Find out how much memory total is available on the heap for allocating
  520. * Objects.
  521. *
  522. * @return the total number of bytes of memory for Objects
  523. */
  524. public native long totalMemory();
  525. /**
  526. * Returns the maximum amount of memory the virtual machine can attempt to
  527. * use. This may be <code>Long.MAX_VALUE</code> if there is no inherent
  528. * limit (or if you really do have a 8 exabyte memory!).
  529. *
  530. * @return the maximum number of bytes the virtual machine will attempt
  531. * to allocate
  532. */
  533. public native long maxMemory();
  534. /**
  535. * Run the garbage collector. This method is more of a suggestion than
  536. * anything. All this method guarantees is that the garbage collector will
  537. * have "done its best" by the time it returns. Notice that garbage
  538. * collection takes place even without calling this method.
  539. */
  540. public native void gc();
  541. /**
  542. * Run finalization on all Objects that are waiting to be finalized. Again,
  543. * a suggestion, though a stronger one than {@link #gc()}. This calls the
  544. * <code>finalize</code> method of all objects waiting to be collected.
  545. *
  546. * @see #finalize()
  547. */
  548. public native void runFinalization();
  549. /**
  550. * Tell the VM to trace every bytecode instruction that executes (print out
  551. * a trace of it). No guarantees are made as to where it will be printed,
  552. * and the VM is allowed to ignore this request.
  553. *
  554. * @param on whether to turn instruction tracing on
  555. */
  556. public native void traceInstructions(boolean on);
  557. /**
  558. * Tell the VM to trace every method call that executes (print out a trace
  559. * of it). No guarantees are made as to where it will be printed, and the
  560. * VM is allowed to ignore this request.
  561. *
  562. * @param on whether to turn method tracing on
  563. */
  564. public native void traceMethodCalls(boolean on);
  565. /**
  566. * Load a native library using the system-dependent filename. This is similar
  567. * to loadLibrary, except the only name mangling done is inserting "_g"
  568. * before the final ".so" if the VM was invoked by the name "java_g". There
  569. * may be a security check, of <code>checkLink</code>.
  570. *
  571. * @param filename the file to load
  572. * @throws SecurityException if permission is denied
  573. * @throws UnsatisfiedLinkError if the library is not found
  574. */
  575. public void load(String filename)
  576. {
  577. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  578. if (sm != null)
  579. sm.checkLink(filename);
  580. _load(filename, false);
  581. }
  582. /**
  583. * Load a native library using a system-independent "short name" for the
  584. * library. It will be transformed to a correct filename in a
  585. * system-dependent manner (for example, in Windows, "mylib" will be turned
  586. * into "mylib.dll"). This is done as follows: if the context that called
  587. * load has a ClassLoader cl, then <code>cl.findLibrary(libpath)</code> is
  588. * used to convert the name. If that result was null, or there was no class
  589. * loader, this searches each directory of the system property
  590. * <code>java.library.path</code> for a file named
  591. * <code>System.mapLibraryName(libname)</code>. There may be a security
  592. * check, of <code>checkLink</code>.
  593. *
  594. * @param libname the library to load
  595. *
  596. * @throws SecurityException if permission is denied
  597. * @throws UnsatisfiedLinkError if the library is not found
  598. *
  599. * @see System#mapLibraryName(String)
  600. * @see ClassLoader#findLibrary(String)
  601. */
  602. public void loadLibrary(String libname)
  603. {
  604. // This is different from the Classpath implementation, but I
  605. // believe it is more correct.
  606. SecurityManager sm = SecurityManager.current; // Be thread-safe!
  607. if (sm != null)
  608. sm.checkLink(libname);
  609. _load(libname, true);
  610. }
  611. /**
  612. * Return a localized version of this InputStream, meaning all characters
  613. * are localized before they come out the other end.
  614. *
  615. * @param in the stream to localize
  616. * @return the localized stream
  617. * @deprecated <code>InputStreamReader</code> is the preferred way to read
  618. * local encodings
  619. */
  620. public InputStream getLocalizedInputStream(InputStream in)
  621. {
  622. return in;
  623. }
  624. /**
  625. * Return a localized version of this OutputStream, meaning all characters
  626. * are localized before they are sent to the other end.
  627. *
  628. * @param out the stream to localize
  629. * @return the localized stream
  630. * @deprecated <code>OutputStreamWriter</code> is the preferred way to write
  631. * local encodings
  632. */
  633. public OutputStream getLocalizedOutputStream(OutputStream out)
  634. {
  635. return out;
  636. }
  637. /**
  638. * Native method that actually shuts down the virtual machine.
  639. *
  640. * @param status the status to end the process with
  641. */
  642. native void exitInternal(int status);
  643. /**
  644. * Load a file. If it has already been loaded, do nothing. The name has
  645. * already been mapped to a true filename.
  646. *
  647. * @param filename the file to load
  648. * @param do_search True if we should search the load path for the file
  649. */
  650. native void _load(String filename, boolean do_search);
  651. /**
  652. *This is a helper function for the ClassLoader which can load
  653. * compiled libraries. Returns true if library (which is just the
  654. * base name -- path searching is done by this function) was loaded,
  655. * false otherwise.
  656. */
  657. native boolean loadLibraryInternal(String libname);
  658. /**
  659. * A helper for Runtime static initializer which does some internal native
  660. * initialization.
  661. */
  662. private static native void init ();
  663. /**
  664. * Run finalizers when exiting.
  665. */
  666. private native void runFinalizationForExit();
  667. /**
  668. * Map a system-independent "short name" to the full file name, and append
  669. * it to the path.
  670. * XXX This method is being replaced by System.mapLibraryName.
  671. *
  672. * @param pathname the path
  673. * @param libname the short version of the library name
  674. * @return the full filename
  675. */
  676. static native String nativeGetLibname(String pathname, String libname);
  677. /**
  678. * Execute a process. The command line has already been tokenized, and
  679. * the environment should contain name=value mappings. If directory is null,
  680. * use the current working directory; otherwise start the process in that
  681. * directory.
  682. *
  683. * @param cmd the non-null command tokens
  684. * @param env the non-null environment setup
  685. * @param dir the directory to use, may be null
  686. * @return the newly created process
  687. * @throws NullPointerException if cmd or env have null elements
  688. * @throws IOException if the exec fails
  689. */
  690. native Process execInternal(String[] cmd, String[] env, File dir)
  691. throws IOException;
  692. } // class Runtime