JVM执行WINDOWS命令的方法
2008-11-05 10:30
232 查看
public class ExecCommand {
public ExecCommand() {
}
/**
* 执行一条命令
*
* @param execStr
* String 命令字符串
* @return String 执行命令错误时的信息。
*/
public void exec(String execStr) {
System.out.println("exec this function");
Runtime runtime = Runtime.getRuntime(); // 取得当前运行期对象
String outInfo = ""; // 执行错误的输出信息
// String[] args = new String[] { "sh", "-c", execStr };// 执行linux下的命令
// 执行windows下的命令
String[] args = new String[] {"cmd", "-c", execStr};
Process proc = null;
try {
proc = runtime.exec(args);
System.out.println("exec this function end");
InputStream in = proc.getErrorStream();// 得到错误信息输出。
if(in==null){
System.out.println("The executive result is null");
}
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = br.readLine()) != null) {
outInfo = outInfo + line + "/n";
System.out.println("The executive result is null");
System.out.println(outInfo);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // 启动另一个进程来执行命令
// 检查命令是否失败。
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
System.out.println("exec this function end");
}
} catch (InterruptedException e) {
System.err.print(e);
e.printStackTrace();
}
}
/*
* ping the server
* @param server String
* @param timeout int
* @return boolean
* @throws IOException
*/
public boolean pingServer(String server,int timeout)
{
BufferedReader in = null;
Runtime r = Runtime.getRuntime();
String pingCommand = "ping " + server + " -w " + timeout;
try
{
Process p = r.exec(pingCommand);
if (p == null)
{
return false;
}
in = new BufferedReader(new InputStreamReader(p.
getInputStream()));
String line = null;
while ( (line = in.readLine()) != null)
{
System.out.println(line);
if (line.startsWith("Reply from"))
{
return true;
}
}
in.close();
}
catch (Exception ex)
{
return false;
}
return false;
}
}
public ExecCommand() {
}
/**
* 执行一条命令
*
* @param execStr
* String 命令字符串
* @return String 执行命令错误时的信息。
*/
public void exec(String execStr) {
System.out.println("exec this function");
Runtime runtime = Runtime.getRuntime(); // 取得当前运行期对象
String outInfo = ""; // 执行错误的输出信息
// String[] args = new String[] { "sh", "-c", execStr };// 执行linux下的命令
// 执行windows下的命令
String[] args = new String[] {"cmd", "-c", execStr};
Process proc = null;
try {
proc = runtime.exec(args);
System.out.println("exec this function end");
InputStream in = proc.getErrorStream();// 得到错误信息输出。
if(in==null){
System.out.println("The executive result is null");
}
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = br.readLine()) != null) {
outInfo = outInfo + line + "/n";
System.out.println("The executive result is null");
System.out.println(outInfo);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // 启动另一个进程来执行命令
// 检查命令是否失败。
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
System.out.println("exec this function end");
}
} catch (InterruptedException e) {
System.err.print(e);
e.printStackTrace();
}
}
/*
* ping the server
* @param server String
* @param timeout int
* @return boolean
* @throws IOException
*/
public boolean pingServer(String server,int timeout)
{
BufferedReader in = null;
Runtime r = Runtime.getRuntime();
String pingCommand = "ping " + server + " -w " + timeout;
try
{
Process p = r.exec(pingCommand);
if (p == null)
{
return false;
}
in = new BufferedReader(new InputStreamReader(p.
getInputStream()));
String line = null;
while ( (line = in.readLine()) != null)
{
System.out.println(line);
if (line.startsWith("Reply from"))
{
return true;
}
}
in.close();
}
catch (Exception ex)
{
return false;
}
return false;
}
}
相关文章推荐
- PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
- java执行windows下cmd命令的方法
- PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
- java调用windows/Linux/Unix 命令行执行命令的方法--调用ImageMagick的合并图形的命令
- linux下增加自动执行的命令(即windows下的批处理)的方法
- maven执行打包命令(mvn package)是出现Java heap space错误的解决方法(windows环境)
- java执行windows下cmd命令的方法
- java执行windows下cmd命令的方法
- resin WED服务器初用遇到的问题和解决方法 java.lang.RuntimeException: java.net.SocketException: Unrecognized Windows Socke ts error: 0: JVM_Bind
- sqlplus 执行linux系统命令方法
- Windows通过DOS命令进入MYSQL的方法
- Linux下smb 挂载执行Windows下命令
- C/C++程序中执行shell命令的2种方法
- U-Boot添加menu命令的方法及U-Boot命令执行过程
- Linux--- MySQL中GRANT命令执行时发生root权限报错及解决方法
- java执行windows 的cmd 命令说明
- C# 数据库sql中用参数的方法来执行UPDATE命令,实现更新dataTime类型
- hbase执行create命令时延迟无反应的解决方法
- U-Boot添加menu命令的方法及U-Boot命令执行过程