2014-09-12 60 views
-1

我有這樣的bash:從Java運行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的文件,換句話說他們不跑巴斯! 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編寫的程序。

+0

檢查退出代碼。從stderr讀取。有很多事情可能發生。 – 2014-09-12 19:41:46

+0

#LeBarton什麼是退出代碼? – someone 2014-09-12 19:43:09

+0

p.exitValue()。所有程序都會向操作系統返回一個整數。如果程序返回0,它正常結束。如果它不是0,則會遇到錯誤。 – 2014-09-12 19:45:01

回答

0

- LeBarton什麼是退出代碼?

檢查p.exitValue的輸出()

+0

它是1,這是什麼意思?我該怎麼辦? – someone 2014-09-12 19:50:52

+0

1.檢查腳本權限(如果運行Java進程的用戶具有執行權限) – TheCodingFrog 2014-09-12 19:53:59

+0

#Chartar腳本權限是什麼? – someone 2014-09-12 19:54:50

0
p.waitFor() 

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

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

這將顯示你的錯誤輸出。將此添加到try .. catch下面的底部。

您將看到您將在命令行中看到的輸出。它將幫助你縮小錯誤。

+0

#LeBarton它顯示了我只是一些數字 – someone 2014-09-12 20:00:11