无法运行 Shell 脚本

发布于 2024-12-17 13:18:56 字数 1666 浏览 1 评论 0 原文

我正在尝试使用 Cygwin 从 Windows 操作系统运行 shell 脚本。当我从命令提示符运行时,它工作正常,因为我深入了解C:\cygwin\bin并运行sh my_script.sh

当我通过代码尝试此操作时出现以下错误

C:\Java\jdk1.6\bin>java CmdProcessBuilder
Exception in thread "main" java.io.IOException: Cannot run program "C:\cygwin\bi
n\my_script.sh": CreateProcess error=193, %1 is not a valid Win32 application
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
        at CmdProcessBuilder.main(CmdProcessBuilder.java:13)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32
 application
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
        at java.lang.ProcessImpl.start(ProcessImpl.java:30)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
        ... 1 more

以下是代码:

public static void main(String args[])
     throws InterruptedException,IOException
  {
    List<String> command = new ArrayList<String>();
    command.add(System.getenv("cygwin") +"\\bin\\"+"sh my_script.sh");

    ProcessBuilder builder = new ProcessBuilder(command);

    final Process process = builder.start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    System.out.println("Program terminated!");
  }

当我从命令提示符尝试 cd cygwin\bin\sh my_script.sh 时,它不会执行并给出错误消息系统找不到指定的路径。

I am trying to run an shell script from my Windows OS using Cygwin. When i run from command prompt it works fine as i go insight C:\cygwin\bin and Run sh my_script.sh.

When i am trying this through code getting following error

C:\Java\jdk1.6\bin>java CmdProcessBuilder
Exception in thread "main" java.io.IOException: Cannot run program "C:\cygwin\bi
n\my_script.sh": CreateProcess error=193, %1 is not a valid Win32 application
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
        at CmdProcessBuilder.main(CmdProcessBuilder.java:13)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32
 application
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
        at java.lang.ProcessImpl.start(ProcessImpl.java:30)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
        ... 1 more

Following is the Code:

public static void main(String args[])
     throws InterruptedException,IOException
  {
    List<String> command = new ArrayList<String>();
    command.add(System.getenv("cygwin") +"\\bin\\"+"sh my_script.sh");

    ProcessBuilder builder = new ProcessBuilder(command);

    final Process process = builder.start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    System.out.println("Program terminated!");
  }

When i am trying cd cygwin\bin\sh my_script.sh from command prompt it is not executing giving error msg The system cannot find the path specified.

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

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

发布评论

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

评论(3

九歌凝 2024-12-24 13:18:56

像这样使用 Runtime.exec 可能会有更好的运气:

String fs = System.getProperty("file.separator");
String command = System.getenv("cygwin") + fs + "bin" + fs + "sh my_script.sh";

Process p = Runtime.getRuntime().exec(command);

避免在路径中使用依赖于平台的字符串也是一个很好的做法。

You might have better luck using Runtime.exec like this:

String fs = System.getProperty("file.separator");
String command = System.getenv("cygwin") + fs + "bin" + fs + "sh my_script.sh";

Process p = Runtime.getRuntime().exec(command);

It is also good practice to avoid using platform-dependent strings in your paths as well.

优雅的叶子 2024-12-24 13:18:56

您是否尝试过将命令替换为

./cygdrive/c/cygwin/bin/my_script.sh

?如果您想从现有的内容出发,您可能需要将命令替换为

command.add(System.getenv("cygwin") +"\\\\bin\\\\"+"sh my_script.sh");

执行,

  C:\cygwin\\bin\\my_script.sh  

这就是 cygwin 中使用 Windows 样式路径的方式。

Have you tried replacing your command with

./cygdrive/c/cygwin/bin/my_script.sh

? If you want to go from what you have, you probably need to replace your command with

command.add(System.getenv("cygwin") +"\\\\bin\\\\"+"sh my_script.sh");

to execute

  C:\cygwin\\bin\\my_script.sh  

which is how windows styled paths are used in cygwin.

一刻暧昧 2024-12-24 13:18:56

关于您的构造,我注意到的一件事是您可能没有正确构建命令字符串。阅读 ProcessBuilder 文档(之前没有使用过这个),您可以像您所做的那样使用字符串列表构建一个流程命令,但第一个字符串是命令,其他字符串是参数。您构建 comamnd 的方式:

List<String> command = new ArrayList<String>();
command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh " + "my_script.sh"); 

ProcessBuilder builder = new ProcessBuilder(command);

似乎创建了一个包含脚本名称的命令名称。将调用更改为:

List<String> command = new ArrayList<String>();
command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh ");
command.add("my_script.sh"); 

ProcessBuilder builder = new ProcessBuilder(command);

并检查错误。然后,检查是否存在未找到c:\cygwin\bin\sh.exe或未找到脚本本身的问题。您提到尝试 cd 失败了,那么 my_script.sh 脚本相对于 java 类文件位于哪里?当您调用 sh 命令时,它可能无法找到 my_script.sh 文件,因为工作目录可能不在您认为的位置。

您可以尝试的一件事是确定 sh.exe 认为它正在运行的位置。从您的 Java 代码中,您应该能够使用以下命令调用 shell:

  List<String> command = new ArrayList<String>();
  String fs = System.getProperty("file.separator");
  command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh");
  command.add("-c");
  command.add("pwd"); 

该输出应该是 shell 命令认为它正在运行的目录。如果这不是实际 my_script.sh 所在的位置,您可能需要脚本本身的完整路径(作为 cygwin 格式的示例路径),

  command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh");
  command.add("/usr/local/scripts/my_script.sh");

这就是我开始调试的位置。

One thing I note about your construction is that you might not be building the command string correctly. Reading the ProcessBuilder docs (hadn't used this one before), you build a process command with a List of Strings as you are doing, but the first string is the command, the others are the arguments. The way you are constructing the comamnd:

List<String> command = new ArrayList<String>();
command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh " + "my_script.sh"); 

ProcessBuilder builder = new ProcessBuilder(command);

appears to create a command name that includes the script name. Change the call to:

List<String> command = new ArrayList<String>();
command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh ");
command.add("my_script.sh"); 

ProcessBuilder builder = new ProcessBuilder(command);

and check the error. Then, check if the issue that the c:\cygwin\bin\sh.exe is not being found or that the script itself is not being found. You mention attempting a cd that is failing, so where is the my_script.sh script located relative to the java class files? When you invoke the sh command it may not be able to locate the my_script.sh file because the working directory may not be where you think it is.

One thing you might try is to determine where sh.exe thinks it is running. From your Java code you should be able to invoke the shell with this:

  List<String> command = new ArrayList<String>();
  String fs = System.getProperty("file.separator");
  command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh");
  command.add("-c");
  command.add("pwd"); 

The output of this should be the directory that the shell command thinks it is running from. If this isn't where the actual my_script.sh is located, you may need to fully path to the the script itself (as an example path in cygwin format)

  command.add(System.getenv("cygwin") + fs + "bin" + fs + "sh");
  command.add("/usr/local/scripts/my_script.sh");

This is where I'd start debugging.

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