java.lang.ProcessBuilder.environment()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(286)

本文整理了Java中java.lang.ProcessBuilder.environment()方法的一些代码示例,展示了ProcessBuilder.environment()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ProcessBuilder.environment()方法的具体详情如下:
包路径:java.lang.ProcessBuilder
类名称:ProcessBuilder
方法名:environment

ProcessBuilder.environment介绍

[英]Returns this process builder's current environment. When a process builder instance is created, the environment is populated with a copy of the environment, as returned by System#getenv(). Note that the map returned by this method is not a copy and any changes made to it are reflected in this instance's state.
[中]返回此process builder的当前环境。创建process builder实例时,环境将填充环境的副本,该副本由系统#getenv()返回。请注意,此方法返回的映射不是副本,对其所做的任何更改都会反映在该实例的状态中。

代码示例

代码示例来源:origin: SonarSource/sonarqube

/**
 * @see java.lang.ProcessBuilder#environment()
 */
@Override
public Map<String, String> environment() {
 return builder.environment();
}

代码示例来源:origin: stackoverflow.com

void setUpEnvironment(ProcessBuilder builder) {
  Map<String, String> env = builder.environment();
  // blah blah
}

代码示例来源:origin: jenkinsci/jenkins

private static ProcessBuilder environment(ProcessBuilder pb, String[] env) {
  if(env!=null) {
    Map<String, String> m = pb.environment();
    m.clear();
    for (String e : env) {
      int idx = e.indexOf('=');
      m.put(e.substring(0,idx),e.substring(idx+1,e.length()));
    }
  }
  return pb;
}

代码示例来源:origin: stackoverflow.com

ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();

代码示例来源:origin: alibaba/jstorm

protected static java.lang.Process launchProcess(final List<String> cmdlist,
                         final Map<String, String> environment) throws IOException {
  ProcessBuilder builder = new ProcessBuilder(cmdlist);
  builder.redirectErrorStream(true);
  Map<String, String> process_evn = builder.environment();
  for (Entry<String, String> entry : environment.entrySet()) {
    process_evn.put(entry.getKey(), entry.getValue());
  }
  return builder.start();
}

代码示例来源:origin: alibaba/jstorm

protected static Process launchProcess(final List<String> cmdlist,
                    final Map<String, String> environment) throws IOException {
  ProcessBuilder builder = new ProcessBuilder(cmdlist);
  builder.redirectErrorStream(true);
  Map<String, String> process_evn = builder.environment();
  for (Entry<String, String> entry : environment.entrySet()) {
    process_evn.put(entry.getKey(), entry.getValue());
  }
  return builder.start();
}

代码示例来源:origin: stackoverflow.com

public Process exec(String[] cmdarray, String[] envp, File dir)
  throws IOException {
  return new ProcessBuilder(cmdarray)
    .environment(envp)
    .directory(dir)
    .start();
}

代码示例来源:origin: stackoverflow.com

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();

代码示例来源:origin: GoogleContainerTools/jib

/**
 * Gets a function that takes a {@code docker} subcommand and gives back a {@link ProcessBuilder}
 * for that {@code docker} command.
 *
 * @param dockerExecutable path to {@code docker}
 * @return the default {@link ProcessBuilder} factory for running a {@code docker} subcommand
 */
@VisibleForTesting
static Function<List<String>, ProcessBuilder> defaultProcessBuilderFactory(
  String dockerExecutable, ImmutableMap<String, String> dockerEnvironment) {
 return dockerSubCommand -> {
  List<String> dockerCommand = new ArrayList<>(1 + dockerSubCommand.size());
  dockerCommand.add(dockerExecutable);
  dockerCommand.addAll(dockerSubCommand);
  ProcessBuilder processBuilder = new ProcessBuilder(dockerCommand);
  Map<String, String> environment = processBuilder.environment();
  environment.putAll(dockerEnvironment);
  return processBuilder;
 };
}

代码示例来源:origin: jenkinsci/jenkins

public Channel launchChannel(String[] cmd, OutputStream out, FilePath workDir, Map<String,String> envVars) throws IOException {
  printCommandLine(cmd, workDir);
  ProcessBuilder pb = new ProcessBuilder(cmd);
  pb.directory(toFile(workDir));
  if (envVars!=null) pb.environment().putAll(envVars);
  return launchChannel(out, pb);
}

代码示例来源:origin: gocd/gocd

public ProcessWrapper createProcess(String[] commandLine, String commandLineForDisplay, File workingDir, Map<String, String> envMap, EnvironmentVariableContext environmentVariableContext,
                  ConsoleOutputStreamConsumer consumer, String processTag, String encoding, String errorPrefix) {
  ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
  LOG.debug("Executing: {}", commandLineForDisplay);
  if (workingDir != null) {
    LOG.debug("[Command Line] Using working directory {} to start the process.", workingDir.getAbsolutePath());
    processBuilder.directory(workingDir);
  }
  processBuilder.environment().putAll(environmentVariableContext.getProperties());
  processBuilder.environment().putAll(envMap);
  Process process = startProcess(processBuilder, commandLineForDisplay);
  ProcessWrapper processWrapper = new ProcessWrapper(process, processTag, commandLineForDisplay, consumer, encoding, errorPrefix);
  processMap.putIfAbsent(process, processWrapper);
  return processWrapper;
}

代码示例来源:origin: prestodb/presto

private void launchPrestoCliWithServerArgument(String... arguments)
      throws IOException
  {
    requireNonNull(ldapTruststorePath, "databases.presto.cli_ldap_truststore_path is null");
    requireNonNull(ldapTruststorePassword, "databases.presto.cli_ldap_truststore_password is null");
    requireNonNull(ldapUserName, "databases.presto.cli_ldap_user_name is null");
    requireNonNull(ldapServerAddress, "databases.presto.cli_ldap_server_address is null");
    requireNonNull(ldapUserPassword, "databases.presto.cli_ldap_user_password is null");

    ImmutableList.Builder<String> prestoClientOptions = ImmutableList.builder();
    prestoClientOptions.add(
        "--server", ldapServerAddress,
        "--truststore-path", ldapTruststorePath,
        "--truststore-password", ldapTruststorePassword,
        "--user", ldapUserName,
        "--password");

    prestoClientOptions.add(arguments);
    ProcessBuilder processBuilder = getProcessBuilder(prestoClientOptions.build());
    processBuilder.environment().put("PRESTO_PASSWORD", ldapUserPassword);
    presto = new PrestoCliProcess(processBuilder.start());
  }
}

代码示例来源:origin: KronicDeth/intellij-elixir

@NotNull
protected Process startProcess(@NotNull List<String> commands) throws IOException {
  ProcessBuilder builder = new ProcessBuilder(commands);
  setupEnvironment(builder.environment());
  builder.directory(myWorkDirectory);
  builder.redirectErrorStream(false);
  return builder.start();
}

代码示例来源:origin: Alluxio/alluxio

private void startAlluxioFramework(Map<String, String> extraEnv) {
 String startScript = PathUtils.concatPath(sConf.get(PropertyKey.HOME),
   "integration", "mesos", "bin", "alluxio-mesos-start.sh");
 ProcessBuilder pb = new ProcessBuilder(startScript, mMesosAddress);
 Map<String, String> env = pb.environment();
 env.putAll(extraEnv);
 try {
  pb.start().waitFor();
 } catch (Exception e) {
  LOG.info("Failed to launch Alluxio on Mesos. Note that this test requires that "
    + "Mesos is currently running.");
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: ming1016/study

@Nullable
public Process startInterpreter(String pythonExe) {
  Process p;
  try {
    ProcessBuilder builder = new ProcessBuilder(pythonExe, "-i", jsonizer);
    builder.redirectErrorStream(true);
    builder.redirectError(new File(parserLog));
    builder.redirectOutput(new File(parserLog));
    builder.environment().remove("PYTHONPATH");
    p = builder.start();
  } catch (Exception e) {
    _.msg("Failed to start: " + pythonExe);
    return null;
  }
  return p;
}

代码示例来源:origin: jenkinsci/jenkins

pb.environment().putAll(cookie);

代码示例来源:origin: wildfly/wildfly

/**
 * Remove an environment variable from the process environment.
 *
 * @param key the environment variable name (must not be {@code null})
 * @return this builder
 */
public Builder removeEnvironment(String key) {
  checkNotNullParam("key", key);
  checkNotEmptyParam("key", key);
  builderProcessor = builderProcessor.andThen(pb -> {
    pb.environment().remove(key);
    return pb;
  });
  return this;
}

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
  @Override public ProcessBuilder get() {
    ProcessBuilder pythonProcBuilder = super.get();

    Ignite ignite = Ignition.ignite();
    ClusterNode locNode = ignite.cluster().localNode();

    Integer port = locNode.attribute(ClientListenerProcessor.CLIENT_LISTENER_PORT);

    Map<String, String> env = pythonProcBuilder.environment();
    env.put(ENV_PREFIX + "HOST", "localhost");

    if (port != null)
      env.put(ENV_PREFIX + "PORT", String.valueOf(port));

    if (loc != null)
      env.put(ENV_PREFIX + "LOCAL", String.valueOf(loc));

    return pythonProcBuilder;
  }
}

代码示例来源:origin: apache/storm

public Number launch(Map<String, Object> conf, TopologyContext context, boolean changeDirectory) {
  ProcessBuilder builder = new ProcessBuilder(command);
  if (!env.isEmpty()) {
    Map<String, String> buildEnv = builder.environment();
    modifyEnvironment(buildEnv);
  }
  if (changeDirectory) {
    builder.directory(new File(context.getCodeDir()));
  }
  ShellLogger = LoggerFactory.getLogger(context.getThisComponentId());
  this.componentName = context.getThisComponentId();
  this.serializer = getSerializer(conf);
  try {
    _subprocess = builder.start();
    processErrorStream = _subprocess.getErrorStream();
    serializer.initialize(_subprocess.getOutputStream(), _subprocess.getInputStream());
    this.pid = serializer.connect(conf, context);
  } catch (IOException e) {
    throw new RuntimeException(
      "Error when launching multilang subprocess\n"
      + getErrorsString(), e);
  } catch (NoOutputException e) {
    throw new RuntimeException(e + getErrorsString() + "\n");
  }
  return this.pid;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Add multiple environment values to the process environment.  The consumer is called once for every command
 * execution.
 *
 * @param consumer a consumer which can provide key-value pairs to add to the environment (must not be {@code null})
 * @return this builder
 */
public Builder addEnvironment(Consumer<BiConsumer<String, String>> consumer) {
  checkNotNullParam("consumer", consumer);
  builderProcessor = builderProcessor.andThen(pb -> {
    consumer.accept((key, value) -> pb.environment().put(checkNotEmptyParam("key", checkNotNullParam("key", key)), checkNotEmptyParam("value", checkNotNullParam("value", value))));
    return pb;
  });
  return this;
}

相关文章