Class JseOsLib

All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable

public class JseOsLib extends OsLib
Subclass of LibFunction which implements the standard lua os library.

This contains more complete implementations of the following functions using features that are specific to JSE:

  • execute()
  • remove()
  • rename()
  • tmpname()

Because the nature of the os library is to encapsulate os-specific features, the behavior of these functions varies considerably from their counterparts in the C platform.

Typically, this library is included as part of a call to org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()

 
 Globals globals = JsePlatform.standardGlobals();
 System.out.println( globals.get("os").get("time").call() );
  

For special cases where the smallest possible footprint is desired, a minimal set of libraries could be loaded directly via LuaValue.load(LuaValue) using code such as:

 
 Globals globals = new Globals();
 globals.load(new JseBaseLib());
 globals.load(new PackageLib());
 globals.load(new JseOsLib());
 System.out.println( globals.get("os").get("time").call() );
  

However, other libraries such as MathLib are not loaded in this case.

See Also:
  • Field Details

    • EXEC_IOEXCEPTION

      public static final int EXEC_IOEXCEPTION
      return code indicating the execute() threw an I/O exception
      See Also:
    • EXEC_INTERRUPTED

      public static final int EXEC_INTERRUPTED
      return code indicating the execute() was interrupted
      See Also:
    • EXEC_ERROR

      public static final int EXEC_ERROR
      return code indicating the execute() threw an unknown exception
      See Also:
  • Constructor Details

    • JseOsLib

      public JseOsLib()
      public constructor
  • Method Details

    • getenv

      protected String getenv(String varname)
      Description copied from class: OsLib
      Returns the value of the process environment variable varname, or the System property value for varname, or null if the variable is not defined in either environment. The default implementation, which is used by the JmePlatform, only queryies System.getProperty(). The JsePlatform overrides this behavior and returns the environment variable value using System.getenv() if it exists, or the System property value if it does not. A SecurityException may be thrown if access is not allowed for 'varname'.
      Overrides:
      getenv in class OsLib
      Returns:
      String value, or null if not defined
    • remove

      protected void remove(String filename) throws IOException
      Description copied from class: OsLib
      Deletes the file or directory with the given name. Directories must be empty to be removed. If this function fails, it throws and IOException
      Overrides:
      remove in class OsLib
      Throws:
      IOException - if it fails
    • rename

      protected void rename(String oldname, String newname) throws IOException
      Description copied from class: OsLib
      Renames file or directory named oldname to newname. If this function fails,it throws and IOException
      Overrides:
      rename in class OsLib
      Parameters:
      oldname - old file name
      newname - new file name
      Throws:
      IOException - if it fails
    • tmpname

      protected String tmpname()
      Description copied from class: OsLib
      Returns a string with a file name that can be used for a temporary file. The file must be explicitly opened before its use and explicitly removed when no longer needed. On some systems (POSIX), this function also creates a file with that name, to avoid security risks. (Someone else might create the file with wrong permissions in the time between getting the name and creating the file.) You still have to open the file to use it and to remove it (even if you do not use it).
      Overrides:
      tmpname in class OsLib
      Returns:
      String filename to use