在java应用程序中运行位于服务器上的unix shell脚本并映射为Windows上的U:// dirver?

发布于 2025-01-08 20:50:09 字数 1281 浏览 0 评论 0原文

当我运行runShellScript(unixCommand);时,我收到以下错误:sh.exe已停止工作。 谁能告诉我问题是什么以及如何解决它?

#!/bin/sh
# this assumes webserver is running on port 8080
echo "Deploy everything first"
echo "These next 3 should work..."
echo "The rest of these should fail... (nicely of course)"
echo "This should work but print debug info on the client and server"
# Now undeploy everything

String unixCommand = "sh U:\\home\\ash\\test.sh";
            try {
                runShellScript(unixCommand);
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }



 public static void runShellScript(String unixCommand) throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", unixCommand);
    processBuilder.redirectErrorStream(true);
    Process shellProcess = processBuilder.start();
    InputStream inputStream = shellProcess.getInputStream();
    int consoleDisplay;
    while ((consoleDisplay = inputStream.read()) != -1) {
        System.out.println(consoleDisplay);
    }
    try {
        inputStream.close();
    } catch (IOException iOException) {
    }
}

When i run runShellScript(unixCommand); i get following error: sh.exe has stoped working.
Can anyone tell what is the problem and how to solve it?

#!/bin/sh
# this assumes webserver is running on port 8080
echo "Deploy everything first"
echo "These next 3 should work..."
echo "The rest of these should fail... (nicely of course)"
echo "This should work but print debug info on the client and server"
# Now undeploy everything

String unixCommand = "sh U:\\home\\ash\\test.sh";
            try {
                runShellScript(unixCommand);
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }



 public static void runShellScript(String unixCommand) throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", unixCommand);
    processBuilder.redirectErrorStream(true);
    Process shellProcess = processBuilder.start();
    InputStream inputStream = shellProcess.getInputStream();
    int consoleDisplay;
    while ((consoleDisplay = inputStream.read()) != -1) {
        System.out.println(consoleDisplay);
    }
    try {
        inputStream.close();
    } catch (IOException iOException) {
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长发绾君心 2025-01-15 20:50:10

这里有两个问题。

首先,Rekin 询问您是否正在使用 Cygwin 是件好事,因为这是高度相关的。 Cygwin 通过在前面添加 cygdrive/ 和驱动器名称来处理 Windows 驱动器。它还更喜欢 Unix 风格的路径。因此,您应该将 Unix 命令更改为:

String unixCommand = "sh /cygdrive/u/home/ash/test.sh";

这应该会成功执行您的脚本。

然而,接下来您将看到输出只是数字,而不是任何清晰的文本。这是因为在您的 runShellScript 中,您使用的是最低级别的 read() 方法,并且一次读取一个字节的进程输出,然后打印每个字节,每个字节它自己的单独的线路。至少,您应该使用更高级别的流,例如 DataInputStream - 或者基本上任何具有获取 Strings 方法的类字节。更好的是,使用 Reader 子类,例如 BufferedReader,因为这是自 JDK 1.1 以来的现代方法。完成此操作后,您应该会看到输出。

Two issues here.

First, it is good that Rekin asked whether you are using Cygwin, because that is highly relevant. Cygwin handles Windows drives by prepending cygdrive/, then the drive name. It also prefers Unix-style paths. So you should change your Unix command to:

String unixCommand = "sh /cygdrive/u/home/ash/test.sh";

That should execute your script successfully.

However you will next see that the output is just numbers instead of any legible text. That's because in your runShellScript, you are using the lowest level read() method, and are reading the process output one byte at a time, then printing each byte, each on its own separate line. At the very least, you should use a higher level stream, like DataInputStream - or basically any class with a read*-method obtaining Strings instead of bytes. Even better, use a Reader subclass like BufferedReader, since this is the modern approach since JDK 1.1. Once you do that, you should see the output.

旧情别恋 2025-01-15 20:50:09

这里有两个问题。

首先,Rekin 询问您是否正在使用 Cygwin 是件好事,因为这是高度相关的。 Cygwin 通过在前面加上 cygdrive/ 和驱动器名称来处理 Windows 驱动器。它还更喜欢 Unix 风格的路径。因此,您应该将 Unix 命令更改为:

String unixCommand = "sh /cygdrive/u/home/ash/test.sh";

这应该成功执行您的脚本。

然而,接下来您将看到输出只是数字,而不是任何清晰的文本。这是因为在您的 runShellScript 中,您使用的是最低级别的 read() 方法,并且一次读取一个字节的进程输出,然后打印每个字节,每个字节它自己的单独的线路。至少,您应该使用更高级别的流,例如 DataInputStream - 或基本上任何具有获取 Strings 方法的类,而不是字节。更好的是,使用 Reader 子类,例如 BufferedReader,因为这是自 JDK 1.1 以来的现代方法。完成此操作后,您应该会看到输出。

Two issues here.

First, it is good that Rekin asked whether you are using Cygwin, because that is highly relevant. Cygwin handles Windows drives by prepending cygdrive/, then the drive name. It also prefers Unix-style paths. So you should change your Unix command to:

String unixCommand = "sh /cygdrive/u/home/ash/test.sh";

That should execute your script successfully.

However you will next see that the output is just numbers instead of any legible text. That's because in your runShellScript, you are using the lowest level read() method, and are reading the process output one byte at a time, then printing each byte, each on its own separate line. At the very least, you should use a higher level stream, like DataInputStream - or basically any class with a read* method obtaining Strings instead of bytes. Even better, use a Reader subclass like BufferedReader, since this is the modern approach since JDK 1.1. Once you do that, you should see the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文