当我们用命令(输入和输出)给出两个参数时,如何在Java中执行Perl脚本

时间:2015-03-06 17:28:00

标签: java perl ubuntu

我开发了自己的标记器,我想在网络上托管。我的服务器基于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!");
    }
}

并且,它有点工作。在流输入阅读器之后它永远不会工作。在这里,请建议我如何解决这个问题,我也给我的终端输出:

$ java ExeCommand 

It will take time so keep the patience:
Please check your command (if any):
SVMTool v1.3 (C) 2006 TALP RESEARCH CENTER.
Written by Jesus Gimenez and Lluis Marquez.
MODEL = /home/svmtool_v1.3.2/bin/HIN
T = 0 (F = 1) :: S = LRL :: K = 0 :: U = 0
READING DICTIONARY </home/svmtool_v1.3.2/bin/HIN.DICT>...
[DONE] READING MODELS < DIRECTION = left-to-right :: MODEL = ambiguous context > (1) READING MODELS (weights and biases) FOR KNOWN WORDS ... (2) READING MODELS (weights and biases) FOR UNKNOWN WORDS ... READING MODELS < DIRECTION = right-to-left :: MODEL = ambiguous context > (1) READING MODELS (weights and biases) FOR KNOWN WORDS ... (2) READING MODELS (weights and biases) FOR UNKNOWN WORDS ... TAGGING < DIRECTION = left-to-right then right-to-left

1 个答案:

答案 0 :(得分:0)

再次看一下命令:

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";

该命令的所有输出都被重定向到/home/tomcat4.1.40/webapps/pos/textfile/output.txt,这就是输入流没有获取任何数据的原因。删除重定向应解决问题。