1

Currently I'm trying to make my own front-end to GDB. Everything thus far is going swimmingly, except one part; piping input to the process, once it's been open. I'll include an example from my code below:

    private JButton run = new JButton("Run");
    JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
    vertical.add(run);
    add(vertical, BorderLayout.WEST);

    run.addActionListener(new ActionListner()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            Process proc;
            proc = Runtime.getRuntime().exec("gdb");
            proc = Runtime.getRuntime().exec("r");
        }
    }

This would allow me to run the command 'gdb' and pipe all of it's output to my TextArea, but after that, the process closes and I can't run anymore commands such as 'r' to the same process, instead it opens another one and tries to execute the command 'r' on it's own; so is there any way for me to execute these in the same process? Also, because this will be a GUI, I'll need to have multiple buttons be able to interact with the same process, i.e. the button 'Run' needs to be able to execute 'r', while the button 'Breakpoint' needs to execute 'b' to the same process, is this possible?

TL;DR: Need to run multiple system commands from the same process in Java through the use of a GUI, is it possible?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
centip3de
  • 107
  • 1
  • 7

3 Answers3

4
final PrintWriter out = new PrintWriter(proc.getOutputStream());
out.println("r");

They aren't separate processes, they're input to gdb. :-)

obataku
  • 29,212
  • 3
  • 44
  • 57
  • Hmmm... How would I get the output of that and pass it into my JTextArea? – centip3de Sep 03 '12 at 22:34
  • @centip3de the normal output of the process is already being piped to your text area according to the OP; doing this shouldn't necessitate any change. – obataku Sep 03 '12 at 22:36
  • I'm attempting to use BufferedReaders to output to my TextArea, which has worked with Runtime thus far (pastebin.com/hSK27piR (Couldn't get my code to format correctly in the commetns)). However, when I add in your code, after executing my Runtime() call, both my BufferedReader and lines variable are null – centip3de Sep 03 '12 at 23:55
0

you need to abstract your interactions with GDB. I would make special interface "DebugWithGDB" and declare methods like start/stop/breakpoint/etc

in your case you are trying to couple GDB with your event invocation (actionPerformed), and moreover - spawn two processes whose are different.

I would have a variable in my class, and this variable would hold the implementation of that interface. So all buttons will have reference to that variable and will be able to invoke commands on it.

Also consider using http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html in order to not freeze your GUI (it will put action into main queue thread for you)

jdevelop
  • 12,176
  • 10
  • 56
  • 112
  • 1
    You don't want to be doing the work on the EDT, right; but using `SwingUtilities` does that. Try instead using an `Executor` :-) – obataku Sep 03 '12 at 19:31
0

You just need to get the streams of the process to manipulate it. You must read this: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html.

Here is an example of executind commands in one process: Sending commands to a console application?

Community
  • 1
  • 1
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50