views:

314

answers:

3

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);
    }
}
+3  A: 

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".

Jon Quarfoth
You got the question right, but see my edits (forthcoming...)
Jason S
A: 

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

Amro
no, because as far as I can tell, the debugger doesn't let me construct the commandline "java -jar ..." (FWIW, if I understand right, the JVM is already running in Eclipse)
Jason S
+1  A: 

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.

Dave Ray
thanks, that works! now to securely fasten my debug hat and move forward :/
Jason S
No problem. Good luck!
Dave Ray