Help me with some Java!

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Help me with some Java!

Post by centip3de on Sat Jul 14, 2012 1:32 pm
([msg=68055]see Help me with some Java![/msg])

Hey all,

I'm currently somewhat new to Java, so I appologize if there's an obvious way to do this. Anyway, I'm currently writing a peice of code that I hope will be a graphical front end to GDB because I hate all the other front-ends, it seemed fairly easy yet challenging enough to push me, and I dislike using GDB from the command line (personal preferance). Anywho, I got most of my issues ironed out, except the whole, inputing to GDB part. I can currently get GDB to run, or any othere bash command, but once GDB is running, I can no longer input to it. It seems as though the process I created closes right after I issue one command, which is the opposite of what I want happening... Anywho, hope there's an easy fix! My code is as below, oh and thanks for your help!

Code: Select all
package com.gdb;
import java.io.*;
import com.gui.Window;
import com.gui.newCommand;
public class IO implements Runnable{
    private int lineNumber = 1;
    public Runnable runner = new Runnable()
    {
        public void run()
        {
            if(newCommand.customCommand.equals("clear"))
            {
                Window.textArea.setText("");
                Window.lineNumber = 1;
                return;
            }
            try
            {
                File file = new File("/");
                Process p = null;
                Runtime r = Runtime.getRuntime();
                p = r.exec("bash -c " + newCommand.customCommand, null, file);
                if(p != null)
                {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = reader.readLine();
                    while(line != null)
                    {
                        Window.textArea.append(Window.lineNumber + ": " + line + "\n");
                        line = reader.readLine();
                        Window.lineNumber++;
                    }
                }
            }catch(IOException e1) {
                Window.textArea.append(e1.toString() + "\n");
            }
        }
    };
    @Override
    public void run() {
        // TODO Auto-generated method stub
    }
}
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1449
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Help me with some Java!

Post by WallShadow on Sat Jul 14, 2012 5:22 pm
([msg=68059]see Re: Help me with some Java![/msg])

Hey cent,

Your code got me wondering about this whole thing for a while now.

If by 'inputting' you mean sending info to the process, then there are a few things you need to consider;

I'm guessing that the bash command is similar to the cmd command on win, so you should look at the switch variants:


'cmd /?' wrote:...
/C Carries out the command specified by the string and then terminates
/K Carries out the command specified by the string but remains
...


I have learned from experience that the /c switch doesn't allow any input to it except for the args. I'm guessing that there might be a similar thing going on with the bash command.

I don't know if you know this, but the p.getInputStream() method returns a stream from which you can read what the process output, while the p.getOutputStream() method should return a stream with which you can send it input (testing that out right now).


If I'm mistaken, I apologize, I'm kind of new to Unix and I've never actually played around with the processes in Java.

-- Sat Jul 14, 2012 5:50 pm --

hmmm, found some interesting stuff about this.

http://stackoverflow.com/questions/3643 ... put-stream
User avatar
WallShadow
Contributor
Contributor
 
Posts: 614
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Help me with some Java!

Post by centip3de on Sat Jul 14, 2012 7:15 pm
([msg=68062]see Re: Help me with some Java![/msg])

WallShadow wrote:Hey cent,

Your code got me wondering about this whole thing for a while now.

If by 'inputting' you mean sending info to the process, then there are a few things you need to consider;

I'm guessing that the bash command is similar to the cmd command on win, so you should look at the switch variants:


'cmd /?' wrote:...
/C Carries out the command specified by the string and then terminates
/K Carries out the command specified by the string but remains
...


I have learned from experience that the /c switch doesn't allow any input to it except for the args. I'm guessing that there might be a similar thing going on with the bash command.

I don't know if you know this, but the p.getInputStream() method returns a stream from which you can read what the process output, while the p.getOutputStream() method should return a stream with which you can send it input (testing that out right now).


If I'm mistaken, I apologize, I'm kind of new to Unix and I've never actually played around with the processes in Java.

-- Sat Jul 14, 2012 5:50 pm --

hmmm, found some interesting stuff about this.

http://stackoverflow.com/questions/3643 ... put-stream



Actually, it is similar in bash (think cmd) except it's:

bash -c "Command here"

However, GDB is a completely different program than bash, you just happen to launch it through the terminal. Anyway, thanks for the link, I'll make sure to check it out!
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1449
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Help me with some Java!

Post by WallShadow on Sat Jul 14, 2012 9:53 pm
([msg=68065]see Re: Help me with some Java![/msg])

FINALLY, seems I've been working all day at this. I've managed to make a program along the lines of what you want, it's a CLI java program which starts a specified process and it allows line by line user input to the process while reading the programs output and displaying it to the user console. I've already tested it with a cmd process and a gdb process (AMAZING program btw). If you want the source code, just ask.

What I did was mainly create the main process, and then pass a pointer to it to 3 seperate new threads, 2 for output and err output, and the 3rd for user input to it. Together, they provide all the needed functionality.


This was my method of sending input to the program:
Code: Select all
      xBufferedOutputStream = new BufferedOutputStream(rCommandProcess.getOutputStream());
      
      xSTDIN = new BufferedReader(new InputStreamReader(System.in));



...   ...   ...   ...   ...   ...   ...   ...   ...   ...   ...   




         try {
            
            tInput = xSTDIN.readLine();
            
            tInput += "\n";
            
            xBufferedOutputStream.write(tInput.getBytes(), 0, tInput.length());
            xBufferedOutputStream.flush();

         } catch (IOException e) {
            
            e.printStackTrace();
            System.exit(2);
            
         }


-WallShadow <3
User avatar
WallShadow
Contributor
Contributor
 
Posts: 614
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests