java中的构建到进程构建器的错误

时间:2015-03-04 13:19:10

标签: perl java javascript

我已经开发了自己的标记器,我想在网络上托管。我的服务器基于jsp。但是这个标记器基于svmtool,它是用Perl脚本编写的。这就是为什么,我创造了一个" .java"文件。在这个文件中,我创建了Processor builder,并通过Runtime.getRuntime()。exec通过这个过程调用了这个文件。它正在工作,但它没有显示我的输出。 Plz有助于解决这个问题。为了更好地站在下面我给我的java代码,并给出最后一行输出/停止过程: import java.io。*;

public class ExeCommand { String outS = "", errS="";
try {

    // run the Unix "type your  terminal command" command
        System.out.println ("Running tagger!");
String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt"; Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command

            System.out.println("It will take time so keep the patience:\n" + command);
System.out.println ("First error line: " + stdError.readLine());

            // read any errors from the attempted command

            System.out.println("Please check your command (if any):\n");
            while ((errS = stdError.readLine()) != null) {
                System.out.println("Error:\t" + errS);
            }
stdError.close();


        System.out.println ("First output line: " + stdInput.readLine());
            while ((outS = stdInput.readLine()) != null) {
                System.out.println("Output:\t" + outS);
            }
stdInput.close();

             System.out.println("Finished!");           
        }

        catch (Exception e) {
            System.out.println("found exception: " + e);
            e.printStackTrace();
            //System.exit(-1);  
        }
    System.out.println("Finished all!");
    }
}

在此之后,终端中没有显示/ last输出: 标记&lt; DIRECTION =从左到右,然后从右到左&gt;

2 个答案:

答案 0 :(得分:0)

首先,您的perl脚本将其标准输出重定向到文件( /home/tomcat4.1.40/webapps/pos/textfile/output.txt)。所以没有办法,你可以用Java捕获标准输出。

相反,您可以使用tee命令将输出写入输出文件和标准输出。现在,这可以用Java捕获。

其次,使用ProcessBuilder而不是Runtime。 Process Builder更灵活,为您提供捕获标准输出/输入/错误的选项。

ProcessBuilder p = new ProcessBuilder();
p.inheritIO(); // Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.
p.command("perl", "somescript.pl");
p.start();

答案 1 :(得分:0)

正如我写的here

对不起严厉的话,但你的代码是一场灾难。没有编译器对它做任何事情。

我已经没有进一步调整到您的代码,我修改了代码,以便它至少可以工作:

package stackoverflow;

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

public class ExeCommand {

    /**
     * 
     * run the Unix "type your terminal command" command
     * 
     * @param args
     */
    public static void main(String[] args) {

        System.out.println("Running tagger!");

        String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt";
//      String command = "ls -og";

        Process p = null;

        try {
            p = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // read the output from the command
        System.out.println("It will take time so keep the patience:\n" + command);
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        try {
            System.out.println(": " + stdError.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }

        // read any errors from the attempted command
        System.out.println("Please check your command (if any):\n");
        try {
            String errS = "";
            while ((errS = stdError.readLine()) != null) {
                System.out.println("Error:\t" + errS);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            stdError.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        try {
            System.out.println("First output line: " + stdInput.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String outS = "";
            while ((outS = stdInput.readLine()) != null) {
                System.out.println("Output:\t" + outS);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            stdInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Finished!");
    }
}

替换第19和20行

        String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt";
//      String command = "ls -og";

//      String command = "perl /home/svmtool_v1.3.2/bin/SVMTagger.pl -V 4 -S LRL /home/svmtool_v1.3.2/models/ih/IN < /home/tomcat4.1.40/webapps/pos/textfile/tests.txt > /home/tomcat4.1.40/webapps/pos/textfile/output.txt";
        String command = "ls -og";

然后你得到这个输出:

Running tagger!
It will take time so keep the patience:
ls -og
: null
Please check your command (if any):

First output line: insgesamt 12
Output: drwxrwxr-x 3 4096 Mai 23 20:51 bin
Output: drwxrwxr-x 3 4096 Apr 26 15:49 src
Output: -rw-rw-r-- 1   28 Apr 26 15:51 test
Finished!