处理错误流消息的最佳方式

处理错误流消息的最佳方式

本文介绍了处理/处理错误流消息的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理不同的错误/异常消息生成的编译/运行时间。



我执行一个Java程序,读取如此生成的流:

  final进程p2 = builder.start(); 
BufferedReader in = new BufferedReader(new
InputStreamReader(p2.getInputStream()));

对于每个成功,都会生成并显示输出。没有问题。但是我需要为每个错误消息显示自定义消息。



EG:

 错误:在类dummy.helloParse10.hello中找不到主方法,请将主要方法定义为:
public static void main(String [] args)
或JavaFX应用程序类必须扩展javafx .application.Application

可以自定义为:错误:未找到主方法我目前的做法是非常丑陋和有限的,



我正在查看错误流中是否存在异常字符串,然后取出子字符串。如:

  if (tError.contains(Exception))
tError = tError.substring(tError.indexOf(main\)+ 5,tError.indexOf(at))
+(在线:+ tError.substring(tError.indexOf(。java)+ 6);

但是它并没有以广泛的方式定制我的方法。



我能做什么最好的事情?



编辑



我认为我的问题不清楚,基本上我通过
$ code执行Java程序> ProcessBuilder 。

  //编译程序
进程p = Runtime.getRuntime( ).exec(javac filename);

//现在可以得到错误流:
BufferedReader in = new BufferedReader(new
InputStreamReader(p.getOutputStream() ));

String line = null;
while((line = in.readLine())!= null){

//编译时的进程错误。
}
...
...
// ProcessBuilder执行program.java
//读取输出或运行时异常

没有进程的输出可能是te java程序的结果或从进程中获取的异常/错误是String形式。需要操纵这些错误。



更新:



我现在可以通过由@Miserable Variable建议的。我如何处理类似的运行时异常?



编辑:
实际上不可能修改程序以在新进程中运行。它们是用户特定的。

解决方案

为了识别编译中的错误,而不是运行 javac code> ProcessBuilder 更好的选择可能是使用。



我从来没有使用过它,但它似乎很简单。


I need to process different Error/Exception messages generated compile/run time.

I execute a Java program an read the stream thus generated :

final Process p2 = builder.start();
BufferedReader in = new BufferedReader(new
        InputStreamReader(p2.getInputStream()));

For each success , the output is generated and shown. No problem there. But I need to show custom message for every Error message.

EG:

Error: Main method not found in class dummy.helloParse10.hello, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

May be customized as : Error: Main method not found

My current approach is very ugly and limited . I am looking if "Exception" string is present in error stream and then taking out the sub- strings.Something like :

if(tError.contains("Exception"))
      tError=tError.substring(tError.indexOf("main\"")+5,tError.indexOf("at"))
        + "( At Line: "+tError.substring(tError.indexOf(".java")+6);

But it do not customize my approach in a broad way.

What is the best thing I could do?

Edit :

I think my question is unclear. Basically I am executing a Java program viaProcessBuilder.

    //Compile the program
  Process p = Runtime.getRuntime().exec("javac filename ");

    // Now get the error stream if available :
  BufferedReader in = new BufferedReader(new
                    InputStreamReader(p.getOutputStream()));

 String line = null;
 while ((line = in.readLine()) != null) {

       //process error in compilation.
       }
    ...
    ...
  // ProcessBuilder to execute the program.java
  //Read the output or runtime Exception

No the output of the process can be the result of te java program or an exception/error which is taken from the process steam an is in String form.Need to manipulate those Errors.

Update :

I can now solve the compile time errors via Java Compiler API as suggested by @Miserable Variable .How can I deal with Runtime Exceptions similarly ?

EDIT :In actual it is not possible to modify the program to be run in a new process.They are user specific.

解决方案

For identifying errors in compilation, instead of running javac using ProcessBuilder a better alternative might be to use Java Compiler API.

I have never used it myself but it seems pretty straightforward.

这篇关于处理/处理错误流消息的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:35