Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 779

WebServlet not support Command line call? [duplicate]

$
0
0

I am trying to call a simple command line executable (shell script) from a WebServlet, but seems it not work at all.

OS, Rocky Linux 9;

Java: JDK 17.0.3;

Application server: Payara community edition 6.2024.6

The test.sh as:

#!/bin/bashecho "User home is $HOME"

As you can see the shell script only echo a user home. I tested it, it works fine.

Function as:

protected void runCommandLine(){    ProcessBuilder processBuilder = new ProcessBuilder();    processBuilder.command("/home/payara/bin/test.sh");    try {        Process process = processBuilder.start();        StringBuilder output = new StringBuilder();        BufferedReader reader = new BufferedReader(                new InputStreamReader(process.getInputStream()));        String line;        while ((line = reader.readLine()) != null) {            output.append(line +"\n");        }        int exitVal = process.waitFor();        if (exitVal == 0) {            System.out.println("Success!");            System.out.println(output);            System.exit(0);        } else {            //abnormal...        }    } catch (IOException e) {        e.printStackTrace();    } catch (InterruptedException e) {        e.printStackTrace();    }}

Then put function in WebServlet

@WebServlet("/restAccessor")public class RestAccessor extends HttpServlet implements Serializable {  private static final ILogger logger = Logger.getLogger(RestAccessor.class.getName());  @Override  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    logger.info("hit by post request.");    String inString = getBody(req);    logger.info("Income request context: "+inString);    runCommandLine();    JSONObject jo = new JSONObject();    jo.put("result", "touched");    try {        sendAsJson(resp, jo);    } catch (IOException e) {        throw new RuntimeException(e);    }}

Then hit web service by curl

curl -X POST http://rocky9thss1:28080//WebAppThss/restAccessor -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"name":"Leo","age":26}'

Server log print out as:

[2024-09-09T18:55:45.418+1000] [Payara 6.2024.6] [INFO] []       [com.longz.thss.web.thss.RestAccessor] [tid: _ThreadID=90 _ThreadName=http-thread-pool::http-listener-1(2)] [timeMillis: 1725872145418] [levelValue: 800] [[  hit by post request.]][2024-09-09T18:55:45.419+1000] [Payara 6.2024.6] [INFO] []       [com.longz.thss.web.thss.RestAccessor] [tid: _ThreadID=90 _ThreadName=http-thread-pool::http-listener-1(2)] [timeMillis: 1725872145419] [levelValue: 800] [[  Income request context: {"name":"Leo","age":26}]]

From the server side log you can see, WebServlet catch curl post request properly. but curl command call response as:

curl: (52) Empty reply from server

That means, WebServlet trapped in Command Line call and don't response until time out.

My question is: Does WebServlet support Process, Runtime and/or other command line executable call or not support it at all.

If support, Please advise!


Viewing all articles
Browse latest Browse all 779

Trending Articles