如何在java中运行unix shell脚本,在windows环境中使用cygwin在内部使用rSync? [英] How to code in java to run unix shell script which use rSync internally in windows environment using cygwin?

查看:229
本文介绍了如何在java中运行unix shell脚本,在windows环境中使用cygwin在内部使用rSync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cygwin在windows上获取unix环境。



我有一个shell脚本,在cygwin上运行以执行同步工作和其他事情。



在cygwin上执行脚本时,使用简单的echo命令在终端上显示某些信息。



如何做到这一点

解决方案

使用 Runtime 类运行Cygwin。这是非常脆弱的,取决于你的设置,但在我的机器上我会做:

  Runtime r = Runtime.getRuntime ); 
Process p = r.exec(C:\\dev\\cygwin\\bin\\mintty.exe --exec /cygpath/to/foo.sh);

然后等待 Process 来完成,并获得一个处理它的InputStream对象来查看发送到stdout和stderror的内容。



命令的第一部分是运行cygwin,第二部分是执行一些脚本或命令(使用 -e - exec )。我将在DOS提示符下测试此命令,看看它是否在切割任何代码之前首先工作。此外,请查看可用的选项:

  C:\dev\cygwin\bin\mintty .exe --help 

也在DOS提示符下。



EDIT:以下适用于我打印版本信息



 

class RuntimeFun {
public static void main(String [] args)throws Exception {
Runtime r = Runtime.getRuntime();
Process p = r.exec(C:\\dev\\cygwin\\bin\\mintty.exe --version);
p.waitFor();

BufferedReader buf = new BufferedReader(
new InputStreamReader(
p.getInputStream()));
String line = buf.readLine();
while(line!= null){
System.out.println(line);
line = buf.readLine();不幸的是,不幸的是,我们已经把它们放在一起了,似乎不能使用 - exec ,所以你必须在那里做一些更多的研究。


I am using cygwin to get unix environment on windows.

I have some shell script that run on cygwin to perform syncing works and other things. I want to executes these script through java code.

Also during executing of scripts on cygwin , certain information is displayed on terminal by using simple echo command.. I want to show all that information in my application.

How can I do this??

解决方案

Use the Runtime class to run Cygwin. This is very brittle, and dependent upon your setup, but on my machine I would do:

Runtime r = Runtime.getRuntime();
Process p = r.exec("C:\\dev\\cygwin\\bin\\mintty.exe --exec /cygpath/to/foo.sh");

Then wait for the Process to complete, and get a handle to it's InputStream objects to see what was sent to stdout and stderror.

The first part of the command is to run cygwin, and the second is to execute some script, or command (using -e or --exec). I would test this command on the DOS prompt to see if it works first before cutting any code. Also, take a look at the options available by doing:

C:\dev\cygwin\bin\mintty.exe --help

Also from within the DOS prompt.

EDIT: The following works for me to print version information


public class RuntimeFun {
   public static void main(String[] args) throws Exception {
      Runtime r = Runtime.getRuntime();
      Process p = r.exec("C:\\dev\\cygwin\\bin\\mintty.exe --version");
      p.waitFor();

      BufferedReader buf = new BufferedReader(
               new InputStreamReader(
                        p.getInputStream()));
      String line = buf.readLine();
      while (line != null) {
          System.out.println(line);
          line = buf.readLine();
      }
   }
}

Unfortunately, can't seem to get it working with --exec, so you're going to have to do some more research there.

这篇关于如何在java中运行unix shell脚本,在windows环境中使用cygwin在内部使用rSync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆