Java + Eclipse:如何调试正在接收管道/重定向的stdin的java程序? [英] Java+Eclipse: how do you debug a java program that is receiving piped/redirected stdin?

查看:347
本文介绍了Java + Eclipse:如何调试正在接收管道/重定向的stdin的java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse来开发一个Java程序,并且想到如果没有参数,我会在程序中添加一个选项来解析stdin。 (否则解析文件)



如果我执行somecommand | java -jar myjar.jar,我有问题并去调试...然后意识到我不知道如何在Eclipse中启动一个进程。如果我在命令提示符下运行,我不能附加到正在运行的进程,因为进程立即启动。



有关如何调试的任何建议? >

编辑:看,事情是,我最初写了我的程序来取一个文件名参数。那么我认为它也是有用的,因为它也采用stdin,所以我从我的程序中抽象出InputStream(如Queue先生所说)。它在文件( java -jar myjar.jar myfile )上工作正常,但是当我运行时,不运行键入myfile | java -jar myjar.jar 。我怀疑在两种情况下有不同的东西(eof检测是不同的),但我真的想调试。

  /总体程序结构如下:

public static void doit(InputStream is)
{
...
}

public static void main(String [] args)
{
if(args.length> 0)
{
//这会留下try-catch-finally块,
//但你得到的想法。
FileInputStream fis = new FileInputStream(args [0]);
doit(fis);
fis.close();
}
else
{
doit(System.in);
}
}


解决方案

运行您的应用程序与管道在命令行上,但添加JVM参数进行远程调试,如下所示:

  -Xdebug -Xrunjdwp :transport = dt_socket,server = y,suspend = y,address = 1044 

suspend = y 将告诉JVM在调试器被连接之前实际没有运行该程序。



接下来,进入Eclipse调试启动配置(运行 - >调试配置... )并创建一个远程Java应用程序连接到您的应用程序。在Eclipse中运行启动(设置一些断点后),您应该可以调试。不是很方便,但如果您无法在没有管道的情况下重现您的问题,这是一个选项。


I'm using Eclipse to develop a Java program, and figured I'd add an option to my program to parse stdin if there are no arguments. (otherwise it parses a file)

I am having problems if I execute "somecommand | java -jar myjar.jar" and went to debug... then realized I don't know how to start a process in Eclipse like that. And if I run it on the command prompt, I can't attach to a running process since the process starts immediately.

Any suggestions on how to debug?

edit: see, the thing is, I wrote my program originally to take a filename argument. Then I figured it would be useful for it to take stdin as well, so I did abstract InputStream out of my program (as Mr. Queue suggests). It works fine operating on a file (java -jar myjar.jar myfile), but not operating when I run type myfile | java -jar myjar.jar. I suspect that there's something different in the two scenarios (eof detection is different?) but I really would like to debug.

// overall program structure follows:

public static void doit(InputStream is)
{
    ...
}

public static void main(String[] args)
{
    if (args.length > 0)
    {
        // this leaves out the try-catch-finally block,
        // but you get the idea. 
        FileInputStream fis = new FileInputStream(args[0]);
        doit(fis);
        fis.close();
    }
    else
    {
        doit(System.in);
    }
}

解决方案

Run your app, with the pipe, on the command line but add JVM args for remote debugging, like this:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044

suspend=y will tell the JVM to not actually run the program until the debugger is attached.

Next, go into the Eclipse debug launch configurations (Run -> Debug Configurations...) and create a "Remote Java Application" to connect to your app. Run the launch in Eclipse (after setting some breakpoints) and you should be able to debug. Not terribly convenient, but if you can't reproduce your issues without the pipe, this is an option.

这篇关于Java + Eclipse:如何调试正在接收管道/重定向的stdin的java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆