我装的是linux8.0,mysql4.0,tomcat5.028,我想在jsp页面中调用shell命令来执行mysql数据库备份的命令。在linux的shell下,测试可以成功。命令如下:
[root@mynet mysql]# /usr/local/mysql/bin/mysqldump -u root -p7788919 axtic_cg >
/usr/xyz.sql
但是在jsp中却不行了。<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head><body>
<%
String commandstr = "/usr/local/mysql/bin/mysqldump -u root -p7788919 axtic_cg > /usr/xyz.sql";
Process p;
try
{
    out.print("start backup\n");
    p = Runtime.getRuntime().exec(commandstr);
    if (p.waitFor() == 0)
    {
out.print("backup over\n");     }
    else
    {
out.print("backup fail\n");     }
}
catch (Exception e)
{
    out.print("backup fail\n");
    out.println(e.toString());
}
%>
</body>
</html>
可是我把命令改成了String commandstr = "who am i"
却又显示的是backup over.

解决方案 »

  1.   

    今天又测试了一下,在jsp中使用tomcat的命令可以执行。
    String commandstr = "/usr/tomcat/bin/shutdown.sh";
    这个命令可以执行,不知道为什么备份mysql的就不行了。大家帮忙看看。
      

  2.   

    原因是java runtime.process run的一个bug网上有很多解决方案只有把outputstream /error stream即使的清空,命令才能完成,否则会一直等待而且linux/solaris下面默认的shell buffer很小,很容易出现这种情况
      

  3.   

    谢谢gameboy999指点,我找找资料。
      

  4.   

    这是在jdk里面找到的,gameboy999你说的是这个吧创建进程的方法可能无法针对某些本机平台上的特定进程很好地工作,比如,本机窗口进程,守护进程,Microsoft Windows 上的 Win16/DOS 进程,或者 shell 脚本。创建的子进程没有自己的终端或控制台。它的所有标准 io(即 stdin,stdout,stderr)操作都将通过三个流 (getOutputStream(),getInputStream(),getErrorStream()) 重定向到父进程。父进程使用这些流来提供到子进程的输入和获得从子进程的输出。因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。
      

  5.   

    没有找到清空
    getOutputStream(),getInputStream(),getErrorStream(),
    gameboy999,能发个例子吗?
      

  6.   

    去google搜索一下  ExecRunner  pitfallhttp://www.koders.com/java/fidE74E642685BBC1DC815063846EAB0BC63566E554.aspx