【问题标题】:Change PWD of linux from JSP从 JSP 更改 linux 的密码
【发布时间】:2013-02-27 23:31:45
【问题描述】:

我需要从 JSP 执行 linux 命令。 它工作正常。 但我需要通过 JSP 在 linux 的特定目录中启动一些 sh ​​文件。说 /home/username/something/start.sh

try{
String command= "cd /home/username/something";

Runtime.getRuntime().exec(command);
Runtime.getRuntime().exec("./start.sh")


out.println("Child");
}
catch(Exception e)
{ out.println("Error");
}

它说找不到文件或目录。

我试过Runtime.getRuntime().exec("pwd"),它显示类似“java.lang.UNIXProcess@fc9d2b”的东西!! :O

我需要更改密码并通过jsp执行一些命令。我怎样才能做到这一点?? 任何帮助将不胜感激。

【问题讨论】:

标签: java linux jsp runtime.exec absolute-path


【解决方案1】:

您不应该(实际上,您似乎不能)设置这样的工作目录。 Runtime.exec() 给出的每个Process 对象都将有自己的工作目录。

正如How to use “cd” command using java runtime? 中的回答,您应该使用Runtime.exec() 的三参数版本,其中您提供了一个File,它将作为工作目录。来自其javadoc

在具有指定环境和工作目录的单独进程中执行指定的命令和参数。

或者更好的是,使用 ProcessBuilderProcessBuilder.directory() 代替:

ProcessBuilder pb = new ProcessBuilder("start.sh");
pb.directory(new File("/home/username/something"));
Process p = pb.start();

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2015-09-18
    • 2012-11-26
    相关资源
    最近更新 更多