Java+Eclipse: how do you debug a java program that is receiving piped/redirected stdin?

隐身守侯 提交于 2019-11-30 15:05:06

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.

If I'm interpreting your question right, I believe you just want to know how to send input across standard in and debug through it in eclipse.

If it's simple input, you can actually manually enter System.in data via the eclipse Console window while the program is running. Just start typing in the console window, and press enter to send the text to Standard in.

If it's something more complicated, I'd suggest abstracting the read you're trying to do to take an InputStream. In your program, you can send System.in as the InputStream. To Debug, you can send any other InputStream. For example, you could put your input in a file, and pass a FileInputStream to the method to test it.

EDIT: Without seeing some more code, I'm not sure, but you might be on to something with eof detection. A FileInputStream has a defined end of file, but I'd guess System.in has nothing of the sort. Your reader might just be waiting to read the next character and never advancing. You might have to manually stop the read after you know you've read "enough".

Amro

Perhaps this solution of creating a named pipe might apply here.

mkfifo foo
somecommand > foo

Next in the debug configuration, add < foo in the args, so that your program is debugged as:

java -jar myjar.jar < foo

I'm late to the party. But you can look for the "common" tab in the debug configuration for your project. There, you should be able to see a section labeled "Standard Input and Output". You can allocate the console with input files and set output files this way.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!