Interface ProcessHandler
- Since:
- 19.1.0
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic final class
Subprocess attributes passed tostart
method.static final class
Represents a source of subprocess input or a destination of subprocess output. -
Method Summary
Modifier and TypeMethodDescriptionstart
(ProcessHandler.ProcessCommand command) A request to start a new subprocess with given attributes.
-
Method Details
-
start
A request to start a new subprocess with given attributes.The default implementation uses
ProcessBuilder
to create the new subprocess. The subprocess current working directory is set toProcessHandler.ProcessCommand.getDirectory()
. TheProcessHandler.ProcessCommand.getDirectory()
value was either explicitely set by the guest language or theFileSystem
's current working directory is used. The subprocess environment is set toProcessHandler.ProcessCommand.getEnvironment()
, the initial value ofProcessBuilder.environment()
is cleaned. TheProcessHandler.ProcessCommand.getEnvironment()
contains the environment variables set by guest language and possibly also the JVM process environment depending on value ofContext.Builder.allowEnvironmentAccess(org.graalvm.polyglot.EnvironmentAccess)
. Implementation example:public Process start(ProcessCommand command) throws IOException { ProcessBuilder builder = new ProcessBuilder(command.getCommand()).redirectErrorStream(command.isRedirectErrorStream()).redirectInput( asProcessBuilderRedirect(command.getInputRedirect())).redirectOutput(asProcessBuilderRedirect(command.getOutputRedirect())).redirectError( asProcessBuilderRedirect(command.getErrorRedirect())); Map<String, String> env = builder.environment(); env.clear(); env.putAll(command.getEnvironment()); String cwd = command.getDirectory(); if (cwd != null) { builder.directory(Paths.get(cwd).toFile()); } return builder.start(); } private static java.lang.ProcessBuilder.Redirect asProcessBuilderRedirect(ProcessHandler.Redirect redirect) { if (redirect == ProcessHandler.Redirect.PIPE) { return java.lang.ProcessBuilder.Redirect.PIPE; } else if (redirect == ProcessHandler.Redirect.INHERIT) { return java.lang.ProcessBuilder.Redirect.INHERIT; } else { throw new IllegalStateException("Unsupported redirect: " + redirect); } }
- Parameters:
command
- the subprocess attributes- Throws:
SecurityException
- if the process creation was forbidden by this handlerIOException
- if the process fails to execute- Since:
- 19.1.0
-