Module java.base
Package java.util.spi

# Interface ToolProvider

public interface ToolProvider
An interface for command-line tools to provide a way to be invoked without necessarily starting a new VM.

Tool providers are normally located using the service-provider loading facility defined by `ServiceLoader`. Each provider must provide a name, and a method to run an instance of the corresponding tool. When a tool is run, it will be provided with an array of string arguments, and a pair of streams: one for normal (or expected) output and the other for reporting any errors that may occur. The interpretation of the string arguments will normally be defined by each individual tool provider, but will generally correspond to the arguments that could be provided to the tool when invoking the tool from the command line.

Since:
  9

- ## Method Summary

  Modifier and Type
  Method
  Description
  `default Optional<String>`
  `description()`
  Returns a short description of the tool, or an empty `Optional` if no description is available.
  `static Optional<ToolProvider>`
  `findFirst(String name)`
  Returns the first instance of a `ToolProvider` with the given name, as loaded by `ServiceLoader` using the system class loader.
  `String`
  `name()`
  Returns the name of this tool provider.
  `default int`
  `run(PrintStream out, PrintStream err, String... args)`
  Runs an instance of the tool, returning zero for a successful run.
  `int`
  `run(PrintWriter out, PrintWriter err, String... args)`
  Runs an instance of the tool, returning zero for a successful run.

- ## Method Details

  - ### name

    String name()
    Returns the name of this tool provider.
    API Note:
      It is recommended that the name be the same as would be used on the command line: for example, "javac", "jar", "jlink".
    Returns:
      the name of this tool provider

  - ### description

    default Optional<String> description()
    Returns a short description of the tool, or an empty `Optional` if no description is available.
    API Note:
      It is recommended that the description fits into a single line in order to allow creating concise overviews like the following:

           jar
             Create, manipulate, and extract an archive of classes and resources.
           javac
             Read Java declarations and compile them into class files.
           jlink
             Assemble a set of modules (...) into a custom runtime image.

    Implementation Requirements:
      This implementation returns an empty `Optional`.
    Returns:
      a short description of the tool, or an empty `Optional` if no description is available
    Since:
      19

  - ### run

    int run(PrintWriter out, PrintWriter err, String... args)
    Runs an instance of the tool, returning zero for a successful run. Any non-zero return value indicates a tool-specific error during the execution. Two streams should be provided, for "expected" output, and for any error messages. If it is not necessary to distinguish the output, the same stream may be used for both.
    API Note:
      The interpretation of the arguments will be specific to each tool.
    Parameters:
      `out` - a stream to which "expected" output should be written
      `err` - a stream to which any error messages should be written
      `args` - the command-line arguments for the tool
    Returns:
      the result of executing the tool. A return value of 0 means the tool did not encounter any errors; any other value indicates that at least one error occurred during execution.
    Throws:
      `NullPointerException` - if any of the arguments are `null`, or if there are any `null` values in the `args` array

  - ### run

    default int run(PrintStream out, PrintStream err, String... args)
    Runs an instance of the tool, returning zero for a successful run. Any non-zero return value indicates a tool-specific error during the execution. Two streams should be provided, for "expected" output, and for any error messages. If it is not necessary to distinguish the output, the same stream may be used for both.
    API Note:
      The interpretation of the arguments will be specific to each tool.
    Implementation Note:
      This implementation wraps the `out` and `err` streams within `PrintWriter`s, and then calls `run(PrintWriter, PrintWriter, String[])`.
    Parameters:
      `out` - a stream to which "expected" output should be written
      `err` - a stream to which any error messages should be written
      `args` - the command-line arguments for the tool
    Returns:
      the result of executing the tool. A return value of 0 means the tool did not encounter any errors; any other value indicates that at least one error occurred during execution.
    Throws:
      `NullPointerException` - if any of the arguments are `null`, or if there are any `null` values in the `args` array

  - ### findFirst

    static Optional<ToolProvider> findFirst(String name)
    Returns the first instance of a `ToolProvider` with the given name, as loaded by `ServiceLoader` using the system class loader.
    Parameters:
      `name` - the name of the desired tool provider
    Returns:
      an `Optional<ToolProvider>` of the first instance found
    Throws:
      `NullPointerException` - if `name` is `null`