Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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
从java运行Bash不起作用_Java_Linux_Bash_Centos - Fatal编程技术网

从java运行Bash不起作用

从java运行Bash不起作用,java,linux,bash,centos,Java,Linux,Bash,Centos,我有一个聚会: #!/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/NERT

我有一个聚会:

#!/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

Firs 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);
    }
    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();
    }
第二个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);
    }
    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()的输出

这将显示错误输出。将此添加到try.下面的底部。。接住


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

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


检查退出代码。从stderr读取。可能会发生很多事情。#勒巴顿退出代码是什么?p.exitValue()。所有程序都会向操作系统返回一个整数。如果程序返回0,则它正常完成。如果不是0,则遇到错误。#LeBarton它是1,我该怎么办?有人能帮忙吗?我仍然被困在这里!!是1,这是什么意思?我该怎么办。检查脚本权限(如果运行Java进程的用户具有执行权限)#Chartar什么是脚本权限?我建议您首先直接运行脚本,以确保可以运行它。如果你能成功运行它,下一步就是在LeBarton建议的代码的帮助下运行suing Java程序。我在terminal中运行了bash,它运行得很好,但我不能用Java代码运行它