Posts: 10
Joined: Tue May 29, 2018 2:26 pm

Sending terminal commands using Java

Wed May 30, 2018 5:50 pm

I am using a Raspberry Pi to send gcode files to a TinyG controller. I am able to successfully do so using the terminal and a program called Plink, and now I would like to make a graphical user interface that can be used on the Raspberry Pi's touchscreen. There are 12 dfferent gcode files, so I would like to make 12 buttons on the GUI which will each send a specific file. I have already written a GUI program in Java, and now I just need to figure out how to send commands to the terminal in my Java code. After doing a lot of googling and forum searching, I am still lost on how to do so. Any help is appreciated, and I am still very with Java and Linux!

User avatar
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Re: Sending terminal commands using Java

Wed May 30, 2018 8:34 pm

Short example:

Code: Select all


public class process {
    public static void main(String args[]) {
        String tmp;
        String s = new String();
        Process p;
        try {
            p = Runtime.getRuntime().exec("ls -aF");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((tmp = br.readLine()) != null)
                System.out.println("[LINE]: " + tmp);
                s += tmp + "\n";
            System.out.println ("[EXIT]: " + p.exitValue());
        } catch (Exception e) {}
Tell me if you want it explained. Otherwise, you are welcome. :-)
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

Return to “Java”