Java tutorial
//package com.java2s; /* * Copyright (C) 2012 Dominik Schrmann <dominik@dominikschuermann.de> * Copyright (c) 2012 Michael Elsdrfer (Android Autostarts) * Copyright (c) 2012 Stephen Erickson, Chris Ravenscroft, Adam Shanks (RootTools) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Map; public class Main { public static Process runWithEnv(String command, ArrayList<String> customAddedEnv, String baseDirectory) throws IOException { Map<String, String> environment = System.getenv(); String[] envArray = new String[environment.size() + (customAddedEnv != null ? customAddedEnv.size() : 0)]; int i = 0; for (Map.Entry<String, String> entry : environment.entrySet()) { envArray[i++] = entry.getKey() + "=" + entry.getValue(); } if (customAddedEnv != null) { for (String entry : customAddedEnv) { envArray[i++] = entry; } } Process process; if (baseDirectory == null) { process = Runtime.getRuntime().exec(command, envArray, null); } else { process = Runtime.getRuntime().exec(command, envArray, new File(baseDirectory)); } return process; } }