Class FileExecuteHelper

java.lang.Object
net.simplace.core.service.FileExecuteHelper

public class FileExecuteHelper extends Object
Opens files, URLs and e-mails in external applications and runs executables and scripts.
Author:
Andreas Enders
  • Constructor Details

    • FileExecuteHelper

      public FileExecuteHelper()
  • Method Details

    • showInExternalBrowser

      public static void showInExternalBrowser(String aURL, boolean aNewWindows) throws ExecutionException
      Opens a URL in the default browser of the operating system.
      Parameters:
      aURL - URL to open; if it contains no slash, http:// is prepended
      aNewWindows - ignored: java.awt.Desktop has no "new window" policy, the OS-registered browser decides
      Throws:
      ExecutionException - if no Desktop/BROWSE support is available or the browser could not be launched
    • executeFile

      public static Process executeFile(File aFile) throws ExecutionException
      Opens a file in the application registered for its type.
      Parameters:
      aFile - file to open
      Returns:
      the started process, or null (see executeFileExtern(File, boolean))
      Throws:
      ExecutionException - if the file cannot be opened
    • executeFile

      public static Process executeFile(String aFileName)
      Opens a file in the application registered for its type. Errors are logged, not thrown.
      Parameters:
      aFileName - path of the file to open
      Returns:
      the started process, or null if an error occurred (or see executeFileExtern(File, boolean))
    • executeFileExtern

      public static Process executeFileExtern(File aFile) throws ExecutionException
      Opens a file in the application registered for its type.
      Parameters:
      aFile - file to open
      Returns:
      the started process, or null (see executeFileExtern(File, boolean))
      Throws:
      ExecutionException - if the file cannot be opened
    • executeFileExtern

      public static Process executeFileExtern(String aFile) throws ExecutionException
      Opens a file in the application registered for its type.
      Parameters:
      aFile - path of the file to open
      Returns:
      the started process, or null (see executeFileExtern(File, boolean))
      Throws:
      ExecutionException - if the file cannot be opened
    • executeFileExtern

      public static Process executeFileExtern(File aFile, boolean aCreateTemporaryCopy) throws ExecutionException
      Opens a file in the application registered for its type. On Linux kfmclient (KDE) or gnome-open is used, on Windows NT 4.0 rundll32, on all other systems (Windows, macOS) Desktop.open(File).

      CAVEAT: Changes to this method must be tested against all supported operating systems.

      Parameters:
      aFile - file to open
      aCreateTemporaryCopy - if true, the file is copied to a random temp directory and the copy is opened. This can be used to prevent the original document from being edited.
      Returns:
      the started process; null on systems other than Linux and Windows NT 4.0 (no process handle available)
      Throws:
      ExecutionException - if the file cannot be copied or opened
    • executeExecutable

      public static Process executeExecutable(String aWholePathAndProgName)
      Starts an executable. Forward slashes in the path are replaced by backslashes (Windows only). Errors are logged, not thrown.
      Parameters:
      aWholePathAndProgName - full path of the executable
      Returns:
      the started process, or null if an error occurred
    • executeApplication

      public static String executeApplication(String aAppName, File aWorkingDir) throws IOException
      Runs a command with cmd.exe /C (Windows only), waits until it has finished and returns its standard output.
      Parameters:
      aAppName - command line to run; forward slashes are replaced by backslashes
      aWorkingDir - working directory of the process
      Returns:
      the standard output of the process
      Throws:
      IOException - if the process cannot be started or its output cannot be read
    • executeScript

      public static void executeScript(String aPath, String aExecutableName, String aParameterString) throws ExecutionException
      Runs a shell script (Linux: <path>/<name>.sh) or batch file (other systems: <path>/<name>.bat) with parameters, if it exists.
      Parameters:
      aPath - directory of the script
      aExecutableName - name of the script without extension
      aParameterString - parameters appended to the command
      Throws:
      ExecutionException - if the script cannot be run
    • newMailInStandardClient

      public static void newMailInStandardClient(List<String> aToAddrsList, String aSubject, String aBody, String aAttachementPath) throws IOException
      Opens a new e-mail in the mail client. Currently only implemented for Linux (except GNOME), where Thunderbird is started; on other systems only a log message is written.
      Parameters:
      aToAddrsList - list of recipient e-mail addresses
      aSubject - subject of the e-mail
      aBody - body text of the e-mail
      aAttachementPath - path of a file to attach, or null
      Throws:
      IOException - if the mail client cannot be started