java 通过java运行shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5855768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:08:24  来源:igfitidea点击:

to run shell script through java

javashell

提问by Nilesh Shukla

I want to run a shell script through java .I am using license generation tool,It can be call with the help of ./LicenseGen.sh command,under it I require to execute another command create licensekey -x license-input.xml which create a new licensekey.xml file where license-input.xml is a input file and licensekey is a output xml file how it is posssible in java please help me.

我想通过java运行一个shell脚本。我正在使用许可证生成工具,它可以在./LicenseGen.sh命令的帮助下调用,在它下面我需要执行另一个命令create licensekey -x license-input.xml创建一个新的 licensekey.xml 文件,其中 license-input.xml 是一个输入文件,而 licensekey 是一个输出 xml 文件,它在 Java 中如何可能,请帮助我。

my code is

我的代码是

import java.io.*;
import java.util.*;

public class ProcessExample {

/**
 * @param args
 */
 public static void main(String args[]) throws IOException {

       File file=new File("/opt");
      // List<String> list=new List<String>();
       ProcessBuilder processBuilder = new ProcessBuilder("./LicenseGen.sh");
       processBuilder.directory(file);

        Process process=processBuilder.start();      
       //processBuilder.command("create licensekey -x license-input.xml");
       //process=processBuilder.start();
       InputStream is = process.getInputStream();
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader br = new BufferedReader(isr);

       String line;

       System.out.printf("Output of running %s is:", 
          Arrays.toString(args));

       while ((line = br.readLine()) != null) {
         System.out.println(line);
       }

     }
}

回答by stacker

You can't execute the script directly since it has to be interpreted by a shell like bash. Note that bash is an executeable.

你不能直接执行脚本,因为它必须由像 bash 这样的 shell 解释。请注意,bash 是一个可执行文件。

ProcessBuilder pb = new ProcessBuilder("/bin/bash", "/path/LicenseGen.sh");  

回答by aviad

Use commons cli http://commons.apache.org/cli/Good luck!

使用 commons cli http://commons.apache.org/cli/祝你好运!

回答by nIKUNJ

I have used JSchextensively for remote login and script executions. I used google Expect4jwith Jschfor executing scripts on remote machines in expect mode(send/wait). Since, you have to execute command one after another, you can try this.

JSch广泛用于远程登录和脚本执行。我使用google Expect4jwithJsch以期望模式(发送/等待)在远程机器上执行脚本。既然,你必须一个接一个地执行命令,你可以试试这个。

It can also be used for local execution that you require. The only worry is that you need to login (into your local machine) for execution.

它还可以用于您需要的本地执行。 The only worry is that you need to login (into your local machine) for execution.

For jsch, go to http://www.jcraft.com/jsch/
For Expect4j, go to http://code.google.com/p/expect4j/

对于 jsch,请访问http://www.jcraft.com/jsch/
对于 Expect4j,请访问http://code.google.com/p/expect4j/

Thanks.

谢谢。