简体   繁体   English

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

[英]How to run a shell script .sh file in java

Objective客观的

I wrote a simple java program where I want to rename a file and then simply I want to run my permission.sh file.我写了一个简单的java程序,我想重命名一个文件,然后我只想运行我的permission.sh文件。

Problem Statement问题陈述

What is happening that my file name is getting change successfully but permission.sh file is not being executed.发生了什么我的文件名更改成功但没有执行 permission.sh 文件。 Stuck need help!卡住需要帮助!

Following is my code.以下是我的代码。

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;
  }
}

EDIT编辑

This java code is s custom class mediator of WSO2 ESB and wso2 does not show logs of java mediator is its not possible for me to debug the issue.此 java 代码是 WSO2 ESB 的自定义类中介,wso2 不显示 java 中介的日志,我无法调试该问题。

Following is my permission.sh code.以下是我的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

it is much advisable to use the Process Builder .建议使用Process Builder It is really built for this kind of tasks.它确实是为此类任务而构建的。

Your program permisions.sh is not an executable as Java understands it, even though the operating system understands it as an executable file.您的程序permisions.sh不是 Java 理解的可执行文件,即使操作系统将其理解为可执行文件。

You need to inform Java that the bash shell (or some other shell) is needed to execute your command.您需要通知 Java 需要 bash shell(或其他一些 shell)来执行您的命令。 eg.例如。 /bin/bash is the path to the program that can run or execute your script. /bin/bash 是可以运行或执行脚本的程序的路径。 With that said you follow this code snippet bellow to fix get that working.话虽如此,您可以按照下面的代码片段进行修复以使其正常工作。

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;
  }
}

Please note that the above code may need some modification especially if your script(permissions.sh) may depend on the current working directory.请注意,上面的代码可能需要一些修改,特别是如果您的脚本(permissions.sh)可能依赖于当前工作目录。 with the above the working directory of the java code is being used.上面的java代码的工作目录正在被使用。

but can be changed by calling the directory(File directory) method on the process object.但可以通过调用 process 对象上的directory(File directory)方法来更改。 then pass the new working directory path to it.然后将新的工作目录路径传递给它。 In this case在这种情况下

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

You can also call the execCommand.getErrorStream();你也可以调用execCommand.getErrorStream(); or execCommand.getInputStream();execCommand.getInputStream(); where needed.需要的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM