6

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);
    }
}
2
  • Would putting the args in your Eclipse run configuration do the trick? Commented Oct 16, 2009 at 22:49
  • These aren't args. It's the fact that I have to pipe in somecommand. Commented Oct 16, 2009 at 23:15

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that works! now to securely fasten my debug hat and move forward :/
4

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

1 Comment

You got the question right, but see my edits (forthcoming...)
0

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

1 Comment

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)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.