【问题标题】:Running Bash from java does not work从 java 运行 Bash 不起作用
【发布时间】:2014-11-07 01:35:36
【问题描述】:

我有这个 bash:

#!/bin/bash
# File to be tagged
inputfile="/dfs/sina/SinaGolestanirad-Project-OneTextEachTime/SinaGolestanirad-Project/Text.txt"
#inputfile="test/SampleInputs/longParagraph.txt"
# Tagged file to be created
#outputfile="test/SampleOutputs/NERTest.conll.tagged.txt"
outputfile="/dfs/sina/SinaGolestanirad-Project-OneTextEachTime/SinaGolestanirad-Project/1.Generate-Basic-Questions/Tagged-Named-Entites-Text.txt"
# Config file
#configfile="config/conll.config"
configfile="config/ontonotes.config"
# Classpath
cpath="target/classes:target/dependency/*"
CMD="java -classpath  ${cpath} -Xmx8g edu.illinois.cs.cogcomp.LbjNer.LbjTagger.NerTagger -annotate ${inputfile} ${outputfile} ${configfile}"
echo "$0: running command '$CMD'..."
$CMD

当我运行下面的任一 java 代码时,它们不会给出任何错误,但它们只会在我的 Eclipse 控制台中显示 bash 文件,换句话说,它们不会运行 bash !!而process.exitValue()的值为1,顺便说一下,我的操作系统是CentOS,linux。

第一个 JAVA 代码:

try {
     Process process = new ProcessBuilder(command).start();
     process.waitFor();
     System.out.println(process.exitValue());
        BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        while ((line = buf.readLine()) != null) {
            System.out.println("exec response: " + line);
        }
    } catch (Exception e) {
        System.out.println(e);
    }

第二个JAVA代码:

    String command = "/dfs/sina/SinaGolestanirad-Project-OneTextEachTime/"
            + "SinaGolestanirad-Project/1.Generate-Basic-Questions/1.IllinoisNerExtended-DO-NOT-OPEN-BY-ECLIPSE/plaintextannotate-linux.sh";
     StringBuffer output = new StringBuffer();

    Process p;
    try {
        String[] cmd = new String[]{"/bin/bash",command};
        p = Runtime.getRuntime().exec(cmd);

        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));

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

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

我还检查了 bash 文件的权限,它可以作为程序执行。

如何运行 bash 文件? bash 应该运行另一个用 java 编写的程序。

【问题讨论】:

  • 检查退出代码。从标准错误中读取。有很多事情可能会发生。
  • #LeBarton 退出代码是什么?
  • p.exitValue() 。所有程序都向操作系统返回一个整数。如果程序返回 0,则它正常结束。如果不为0,则遇到错误。
  • #LeBarton 是 1 ,我该怎么办?
  • 有人可以帮忙吗?我还是被困在这里!!

标签: java linux bash centos


【解决方案1】:

-- LeBarton 退出代码是什么?

检查 p.exitValue() 的输出

【讨论】:

  • 是1,是什么意思?我该怎么办?
  • 1.检查脚本权限(如果运行Java进程的用户有执行权限)
  • #Chartar 脚本权限是什么?
  • 我建议您先直接运行脚本以确保可以运行它。如果可以成功运行,下一步就是借助 LeBarton 建议的代码运行 suing Java 程序。
  • 我在终端运行 bash,效果很好,但是我不能通过 java 代码运行它
【解决方案2】:
p.waitFor()

InputStreamReader inputStreamReader = new InputStreamReader(p.getErrorStream());

While (inputStreamReader.ready()) { System.out.println(inputStreamReader.read(); }

这将显示错误输出。将此添加到 try.. catch 下方的底部。

您将看到在命令行上看到的输出。它将帮助您缩小错误范围。

【讨论】:

    【解决方案3】:

    如果您的 bash 读取了一些环境变量,我找到了一个可能会有所帮助的链接。

    $PATH variable isn't inherited through getRuntime().exec

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 2013-12-01
      • 1970-01-01
      • 2016-06-21
      • 2012-01-07
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多