Skip to content

Instantly share code, notes, and snippets.

@riversun
Last active August 24, 2016 09:45
Show Gist options
  • Save riversun/864dd6ecf7ad6369c283 to your computer and use it in GitHub Desktop.
Save riversun/864dd6ecf7ad6369c283 to your computer and use it in GitHub Desktop.
[Java]Execute command with shell,Enable to write the standard output and error output to the console
/**
* Copyright 2006-2016 Tom Misawa(riversun.org@gmail.com)
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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
*
* Simple Shell Utility<br>
* Write the standard output and error output to the console
*
* @author Tom Misawa (riversun.org@gmail.com)
*/
public class ShellUtil {
public static String ENCODING = "UTF-8";
public static void main(String[] args) {
// Windows DOS Prompt Example
ShellUtil.ENCODING = "MS932";
// exec dir
ShellUtil.executeCommand("cmd /c dir");
}
public static int executeCommand(String cmd) {
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(cmd);
final InputStream stdIs = process.getInputStream();
final InputStream errorIs = process.getErrorStream();
final Runnable stdOutRunnable = new Runnable() {
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(stdIs, ENCODING));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stdIs != null) {
try {
stdIs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
final Runnable errOutRunnable = new Runnable() {
BufferedReader ebr = null;
public void run() {
try {
String errLine;
ebr = new BufferedReader(new InputStreamReader(errorIs, ENCODING));
while ((errLine = ebr.readLine()) != null) {
System.err.println(errLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ebr != null) {
try {
ebr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (errorIs != null) {
try {
errorIs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
final Thread stdRun = new Thread(stdOutRunnable);
final Thread errRun = new Thread(errOutRunnable);
stdRun.start();
errRun.start();
int ret = process.waitFor();
stdRun.join();
errRun.join();
return ret;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment