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

I use Eclipse to develop a Java program and I believe that I will add a parameter to my program to parse stdin if there are no arguments. (otherwise it parses the file)

I am having problems if I execute "somecommand | java -jar myjar.jar" and go for debugging ... then I realized that I don’t know how to start the process in Eclipse like this. And if I run it on the command line, I can’t connect to the running process, since the process starts right away.

Any suggestions for debugging?

edit : see, the fact is that I first wrote my program to accept the file name argument. Then I thought it would be useful to accept stdin as well, so I made an abstract InputStream from my program (as Mr. Queue suggests). It works fine in the file ( java -jar myjar.jar myfile ) but does not work when I run type myfile | java -jar myjar.jar type myfile | java -jar myjar.jar . I suspect there is something else in the two scenarios (eof detection 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); } } 
+6
java eclipse stdin
Oct 16 '09 at 22:38
source share
4 answers

Launch the application with the channel on the command line, but add JVM arguments for remote debugging, for example:

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

suspend=y will tell the JVM that it does not actually run the program until a debugger is connected.

Then go to the Eclipse debugging startup configuration ( Run -> Debug Configurations... ) and create a “Remote Java Application” to connect to your application. Run the launch in Eclipse (after setting some breakpoints) and you should be able to debug it. Not very convenient, but if you cannot reproduce your problems without the handset, this is an option.

+11
Oct 17 '09 at 0:20
source share

If I interpret your question correctly, I believe that you just want to know how to send input according to the standard and debug it in eclipse.

If this is a simple input, you can manually enter System.in data through the eclipse console window while the program is running. Just start typing in the console window and press the enter key to send the text to standard.

If this is something more complex, I would suggest abstracting the reading you are trying to do to take an InputStream. In your program, you can send System.in as an InputStream. For debugging you can send any other InputStream. For example, you can put your file in a file and pass FileInputStream to a method to verify it.

EDIT: Without seeing any more code, I'm not sure, but you may be with something with eof detection. FileInputStream has a specific end to the file, but I would suggest that System.in has nothing of the kind. Your reader may just wait to read the next character and never move forward. You may need to manually stop reading after you know that you have read "enough."

+4
Oct 16 '09 at 23:25
source share

Perhaps this solution to create a named pipe can be applied here.

 mkfifo foo somecommand > foo 

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

java -jar myjar.jar <Foo

0
Oct 17 '09 at 0:12
source share

I was late for the party. But you can find the “general” tab in the debug configuration for your project. There you should see the section "Standard input and output". You can select the console with input files and configure the output files in this way.

0
Jan 19 '19 at 7:43
source share



All Articles