B4J Question Raspberry CMDs

Discussion in 'B4J Questions' started by luke2012, Jul 2, 2015.

  1. luke2012

    luke2012 Well-Known Member Licensed User

    Hi to all,
    how to call linux commands (rasbian) within a web app ?
     
  2. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

    various ways, like jShell or InLine Java.
    Look here for an open source example using InLine Java to run commands. Check the files rpiconsole.b4j and rpiconsole.bas.

    Code:
    #If JAVA
    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    //Execute a command AND capture the output In a String
    //Parameter: Command To run
    //Returns: String with the command output
    //Example:
    //Dim cmd as String = "ls"
    //Dim result As String = Main.joInlineJava.RunMethod("ExecuteCommand", Array(cmd))

    public static String ExecuteCommand(String command) {
     
        StringBuffer output = new StringBuffer();
        Process p;
        try
        {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(p.getInputStream()));
                 String line = "";           
                while ((line = reader.readLine())!= null)
                {
                    output.append(line + "\n");
                }
        }
        catch (Exception e)
        {
            //e.printStackTrace();
            return "Error executing command " + command;
        }
        //System.out.println(output);
        return output.toString();
    }

    #End If
     
    luke2012 likes this.
  3. luke2012

    luke2012 Well-Known Member Licensed User

    Great Work and thanks for your reply!
     
Loading...