java中的构建到过程构建器错误
error in build to process builder in java
我已经开发了自己的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!
相关文章:
- 对于grunt构建中的(let.of)错误
- Javascript Sencha cmd 应用程序构建错误发生在执行此行时
- 使用 jQuery 构建一个元素会给我一个错误
- 为什么当我尝试为iframe-src属性构建URL时,AngularJS会抛出错误
- java中的构建到过程构建器错误
- JSON解析错误在一个系统中我没有't构建
- 为Android构建时出现本机描述推送通知错误
- "npm运行构建“;模块解析错误”;您可能需要一个适当的加载程序来处理此文件类型&”;
- SlickGrid根据日期错误动态构建列
- 构建错误列表
- Windows 7 上 Android 的 Phonegap 3.0 构建错误
- 安装 socket.io 返回(node-gyp 重建 2> 构建错误.log) ||(0 号出口)
- 在浏览器中查看时出现煎茶构建错误
- 煎茶触摸生产构建错误
- JavaScript日期对象构建错误的日期
- 在heroku中部署HTML应用程序时出现构建错误
- Visual Studio 2015 Apache Cordova构建错误
- Webpack构建错误
- 节点.js电子预构建错误找不到模块“浏览器窗口”窗口 10
- 为什么我会收到这个节点.js构建错误