如何 运行 shell 脚本 .sh 文件 java

How to run a shell script .sh file in java

Objective

我写了一个简单的 java 程序,我想重命名一个文件,然后我想 运行 我的 permission.sh 文件.

问题陈述

我的文件名更改成功但 permission.sh 文件未被执行,这是怎么回事。卡住需要帮助!

以下是我的代码。

package nl.ggmd.file.Permission;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class FilePermission extends AbstractMediator
{

  public boolean mediate(MessageContext context) { 
      try {
      File oldfile =new File("/ggmd/files/uploads/FIle contract1.csv");
        File newfile =new File("/ggmd/files/uploads/contract1.csv");

        if(oldfile.renameTo(newfile)){
            System.out.println("Rename succesful");
        }else{
            System.out.println("Rename failed");
        }


          Process proc = Runtime.getRuntime().exec("/opt/file/contracts/tst/permissions.sh"); 
          BufferedReader read = new BufferedReader(new InputStreamReader(
                  proc.getInputStream()));
          try {
              proc.waitFor();
          } catch (InterruptedException e) {
              System.out.println(e.getMessage());
          }
          while (read.ready()) {
              System.out.println(read.readLine());
          }
      } catch (IOException e) {
          System.out.println(e.getMessage());
      }


    return true;
  }
}

编辑

此 java 代码是 WSO2 ESB 的自定义 class 中介,而 wso2 不显示 java 中介的日志,我无法调试该问题。

以下是我的 permission.sh 代码。

#!/bin/bash
cd /ggmd/files/uploads/
file=contract1.csv

if [ ! -a $file ]
then
  sudo chmod 664 $file && echo "The file is now writable"
else
  echo "The file is already writable"
fi

建议使用 Process Builder。它确实是为此类任务而构建的。

您的程序 permisions.sh 不是 Java 所理解的可执行文件,即使操作系统将其理解为可执行文件。

您需要通知 Java 需要 bash shell(或其他一些 shell)来执行您的命令。例如。 /bin/bash 是可以 运行 或执行脚本的程序的路径。话虽如此,您可以按照下面的代码片段进行修复以使其正常工作。

public class FilePermission extends AbstractMediator
{

  public boolean mediate(MessageContext context) { 
      try {
      File oldfile =new File("/ggmd/files/uploads/FIle contract1.csv");
        File newfile =new File("/ggmd/files/uploads/contract1.csv");

        if(oldfile.renameTo(newfile)){
            System.out.println("Rename succesful");
        }else{
            System.out.println("Rename failed");
        }

private final String commandPath = "/bin/bash";

private final String scriptPath = "/opt/file/contracts/tst/permissions.sh";


try {
        Process execCommand = new ProcessBuilder(commandPath, scriptPath).start();
        execCommand.waitFor();
    } catch (IOException e) {
        // handle exceptions
        System.out.println(e.getMessage());
    } catch (InterruptedException e) {
        System.out.println(e.getMessage());
    }


    return true;
  }
}

请注意,以上代码可能需要一些修改,尤其是当您的脚本(permissions.sh) 可能依赖于当前工作目录时。使用上面的 java 代码的工作目录。

但可以通过调用进程对象的 directory(File directory) 方法来更改。然后将新的工作目录路径传递给它。在这种情况下

Process execCommand = new ProcessBuilder(commandPath, scriptPath).directory(new File("/some/directory/to/be/used/")).start();

您也可以在需要时调用 execCommand.getErrorStream();execCommand.getInputStream();