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

error in build to process builder in java

本文关键字:构建 错误 过程 java      更新时间:2023-09-26

我已经开发了自己的tagger,我想在网上托管。我的服务器是基于jsp的。但是这个标签是基于svmtool的,它是用Perl脚本编写的。这就是为什么,我创建了一个".java"文件。在这个文件中,我创建了Processor生成器,并通过Runtime.getRuntime().exec调用这个文件。它正在工作,但没有显示我的输出。Plz帮助解决这个问题。为了更好地理解下面的情况,我给出了我的java代码,并给出了输出/停止进程的最后一行:导入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!");
    }
}

在这之后,无所事事/最后一个输出显示在终端中:标记<DIRECTION=从左到右,然后从右到左>

首先,您的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();

正如我在这里所写:

很抱歉说了这么严厉的话,但你的代码是一场灾难。没有编译器对它做任何操作。

我对你的代码做了没有进一步的调整,我修改了代码,使其至少可以工作:

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!