Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将shell脚本的输出放入Java程序中的变量中_Java_Shell_Output - Fatal编程技术网

将shell脚本的输出放入Java程序中的变量中

将shell脚本的输出放入Java程序中的变量中,java,shell,output,Java,Shell,Output,有没有一种方法可以将shell脚本程序的输出转换成Java程序中的变量(而不是转换成输出文件)。shell脚本的输出是数据库查询执行时间,我需要将该时间值分配给Java变量。(我从Java程序中调用该shell脚本)。然后我需要在Java中对这些值进行一些其他计算 我只是用谷歌搜索一下,这里有一个很好的教程,里面有很多例子: 我知道人们更喜欢复制/粘贴,但让我们尊重其他人的工作并进入他们的网站:p下面是一个程序,它将帮助您将任何脚本或任何命令的完整输出存储到字符串对象中。 import java

有没有一种方法可以将shell脚本程序的输出转换成Java程序中的变量(而不是转换成输出文件)。shell脚本的输出是数据库查询执行时间,我需要将该时间值分配给Java变量。(我从Java程序中调用该shell脚本)。然后我需要在Java中对这些值进行一些其他计算

我只是用谷歌搜索一下,这里有一个很好的教程,里面有很多例子:


我知道人们更喜欢复制/粘贴,但让我们尊重其他人的工作并进入他们的网站:p下面是一个程序,它将帮助您将任何脚本或任何命令的完整输出存储到字符串对象中。

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

    public static void main(String[] args) {

        ExecuteShellComand obj = new ExecuteShellComand();

        String output = obj.executeCommand("sh /opt/yourScriptLocation/Script.sh");

        System.out.println(output);

    }

    private String executeCommand(String command) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }

}
更新旧问题 自Java 7以来,有一个新类可以轻松处理操作系统进程: .
假设我们需要将
ip_conf.bat
的输出存储到java
字符串中。
c:\tmp\ip_conf.bat的内容

@echo off
REM will go to standard output
ipconfig
REM will go to stadnard error
hey there!
您可以读取连接到子流程的标准输出和错误输出的输入流:

ProcessBuilder pb = new ProcessBuilder("C:\\tmp\\ip_conf.bat");
Process p = pb.start();
String pOut = "";
try (InputStream stdOut = p.getInputStream();
        BufferedInputStream bufferedStdOut = new BufferedInputStream(stdOut);
        ByteArrayOutputStream result = new ByteArrayOutputStream()) {

    int bytes = 0;
    while ((bytes = bufferedStdOut.read()) != -1) {
        result.write(bytes);
    }
    pOut = result.toString(Charset.defaultCharset().toString());
}
System.out.println(pOut);

InputStream stdErr = p.getErrorStream();
// same with the error stream ...

int exit = p.waitFor();
System.out.println("Subprocess exited with " + exit);

你从哪里得到这个输出?我的shell脚本中有一个变量:TIME=`echo“$END-$START”,现在我需要得到TIME的输出,并将TIME变量的值赋给Java类中的其他变量。我正在为一个遗传算法做适应度函数,因此,通过对从shell脚本中获得的Java变量进行进一步计算,我设置适应度并将其发送给ecj(ecj是一个用Java编写的用于进化优化的工具包),您是否通过Java执行脚本?如果是,您可以获取一个
进程
InputStream
并读取/解析它。@SotiriosDelimanolis是的,我正在从Java调用shell脚本。谢谢这应该是一条评论。在您的答案中包含内容的原因之一是,即使原始页面脱机,它也将始终保留在此处。