简体   繁体   English

如何使用Java在UNIX中从Windows运行bash程序

[英]how to run a bash program from windows in unix using java

I am trying to run a program which process a file from window using jcraft library in Unix. 我正在尝试运行一个程序,该程序使用Unix中的jcraft库从窗口处理文件。 what i found after establishing the channel it always try to run the program in home directory but i need to run in a separate directory.please have a look what i tried so far and let me know what i am missing. 建立频道后我发现它总是尝试在主目录中运行该程序,但是我需要在单独的目录中运行。请看一下到目前为止我尝试过的内容,让我知道我所缺少的内容。

String strRemoteDir = "/home/process/input" channel = session.openChannel("sftp"); 字符串strRemoteDir =“ / home / process / input” channel = session.openChannel(“ sftp”);

    channel.connect();
    System.out.println("sftp channel opened and connected.");
    channelSftp = (ChannelSftp) channel;
    // Printing Home Directory in Unix Server
    System.out.println(channelSftp.getHome());
    channelSftp.cd(strRemoteDir);
   System.out.println(channelSftp.pwd());

  // for uploading a file where i need to run the program
    File f = new File(fileName);
    channelSftp.put(new FileInputStream(f), f.getName());
    System.out.println("File transfered successfully to host.");
    fileTransfer = true;

    channel=session.openChannel("exec");
    InputStream in=channel.getInputStream();
    // it is printing the desired directory where i want to go
    System.out.println(channelSftp.pwd());
    ((ChannelExec)channel).setCommand("sh process.ksh "a.txt");
    channel.setInputStream(null);
    ((ChannelExec)channel).setErrStream(System.err);
     channel.connect();

output : process.ksh not found 输出:找不到process.ksh

But through putty i was able to run the program. 但是通过腻子,我得以运行该程序。 just to let you know process.ksh is not in the input directory, but has the capability to run from anywhere with arguments. 只是为了让您知道process.ksh不在输入目录中,但是具有可以从任何位置运行带有参数的功能。 ((ChannelExec)channel).setcommand("ls") prints out all the files from home directory. ((ChannelExec)channel).setcommand(“ ls”)打印出主目录中的所有文件。 i believe i am establishing a channel to home directory, i just don't know how to run bash program using jcraft in a desired location.Please let me know what i am missing or is it possible to make it happen. 我相信我正在建立一个通往主目录的通道,我只是不知道如何在所需的位置使用jcraft运行bash程序。请让我知道我所缺少的或者有可能实现它。

Thanks in advance. 提前致谢。 Nur 努尔

Others have provided examples of how to set the working directory. 其他人提供了有关如何设置工作目录的示例。 However, a more defensive style of programming is to assume nothing and specify everything explicitly. 但是,一种更具防御性的编程风格是不采取任何假设并明确指定所有内容。 So for specifying the command to execute, something like: 因此,为了指定要执行的命令,类似于:

/bin/sh /path/to/bin/process.ksh /path/to/data/ "a.txt"

Notice that I have added a new first argument to your command, the directory you want to run it from. 注意,我已经在命令中添加了新的第一个参数,即您要从中运行命令的目录。

Now, change your script so that it changes to this directory (given as parameter $1) before continuing. 现在,更改脚本,以便在继续之前将其更改为该目录(作为参数$ 1)。

This can be done by adding cd at the beginning, something like: 这可以通过在开头添加cd来完成,例如:

cd $1
shift

The shift command shifts all other arguments, so that $2 becomes $1 and so on, so that the rest of the script will find the arguments where it expects. shift命令移动所有其他参数,以便$ 2变为$ 1,依此类推,以便脚本的其余部分将在期望的位置找到参数。

"sftp" channel are not made to execute shell command but sftp command only. 不是使“ sftp”通道执行shell命令,而仅使sftp命令执行。

    Channel channel = session.openChannel("shell");
    cmdSend = channel.getOutputStream();
    InputStream cmdRcv = channel.getInputStream();
    // Start a Thread reading and displaying cmdRcv

    channel.connect(3000);
    Thread.sleep(1000);

    cmdSend.write("cd /to/the/right/dir\n".getBytes());
    cmdSend.flush();
    cmdSend.write("sh process.ksh \"a.txt\"\n".getBytes());
    cmdSend.flush();

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

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