Top Description Methods
java.util.spi

public Interface ToolProvider

Imports
java.io.PrintStream, .PrintWriter, java.security.AccessController, .PrivilegedAction, java.util.Objects, .Optional, .ServiceLoader, java.util.stream.StreamSupport

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 TypeMethod and Description
public default Optional<String>

Returns:

a short description of the tool, or an empty Optional if no description is available
description
()

Returns a short description of the tool, or an empty Optional if no description is available.

public static Optional<ToolProvider>

Returns:

an Optional<ToolProvider> of the first instance found
findFirst
(String
the name of the desired tool provider
name
)

Returns the first instance of a ToolProvider with the given name, as loaded by ServiceLoader using the system class loader.

public String

Returns:

the name of this tool provider
name
()

Returns the name of this tool provider.

public int

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.
run
(PrintWriter
a stream to which "expected" output should be written
out
,
PrintWriter
a stream to which any error messages should be written
err
,
String...
the command-line arguments for the tool
args
)

Runs an instance of the tool, returning zero for a successful run.

public default int

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.
run
(PrintStream
a stream to which "expected" output should be written
out
,
PrintStream
a stream to which any error messages should be written
err
,
String...
the command-line arguments for the tool
args
)

Runs an instance of the tool, returning zero for a successful run.

Method Detail

descriptionback to summary
public 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 Specification

This implementation returns an empty Optional.

Returns:Optional<String>

a short description of the tool, or an empty Optional if no description is available

Since
19
findFirstback to summary
public 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:String

the name of the desired tool provider

Returns:Optional<ToolProvider>

an Optional<ToolProvider> of the first instance found

Annotations
@SuppressWarnings:removal
Exceptions
NullPointerException:
if name is null
nameback to summary
public 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:String

the name of this tool provider

runback to summary
public 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:PrintWriter

a stream to which "expected" output should be written

err:PrintWriter

a stream to which any error messages should be written

args:String[]

the command-line arguments for the tool

Returns:int

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.

Exceptions
NullPointerException:
if any of the arguments are null, or if there are any null values in the args array
runback to summary
public 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 PrintWriters, and then calls run(PrintWriter, PrintWriter, String[]).

Parameters
out:PrintStream

a stream to which "expected" output should be written

err:PrintStream

a stream to which any error messages should be written

args:String[]

the command-line arguments for the tool

Returns:int

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.

Exceptions
NullPointerException:
if any of the arguments are null, or if there are any null values in the args array