B4J Question Raspberry CMDs

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to all,
how to call linux commands (rasbian) within a web app ?
 
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: Copy
#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
 
Upvote 0
Back
Top