无法从java调用shell脚本

fkvaft9z  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(404)

我使用 ProcessBuilder ,但我没有得到任何价值。它显示一个空结果。
这是我的密码:

package webservice;

import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}/{param2}/{param3}")
    public Response getMsg(@PathParam("param") String bedroom,@PathParam("param2") String bathroom,@PathParam("param3")String area) throws IOException {
        ProcessBuilder pb = new ProcessBuilder("/script/spark-submit.sh");  
        pb.environment().put("a", bedroom);
        pb.environment().put("b", bathroom);
        pb.environment().put("c", area);
        Process p=pb.start();
        InputStream is = p.getInputStream();
        int i =0;
        StringBuffer sb = new StringBuffer();
        while((i=is.read())!=-1)
            sb.append((char)i);
            System.out.println("value of sb:"+sb.toString());
            String a =sb.toString();
            System.out.println("value of a:"+a);
            String[] arr = a.split(" ");
            if (arr != null) 
            {
                System.out.println("predicted value is " + arr[arr.length - 1]);
            }
            String output = "predicted value is " + arr[arr.length - 1];
            return Response.status(200).entity(output).build();
        }
    }

shell脚本代码

spark-submit --class scalatest.scalatest.scalatest --master local[4] /home/ec2-user/scalatest-0.0.1-SNAPSHOT.jar $a $b $c
slmsl1lt

slmsl1lt1#

尝试使用

try {
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(cmd);
    p.waitFor();
    BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while((line = buff.readLine()) != null){
        temp += line;
    }
    System.out.println(temp);
} catch (Exception e){
        e.printStackTrace();
}
xn1cxnb4

xn1cxnb42#

尝试使用

Process p=pb.start();
p.waitFor();

你的过程似乎从未真正开始过

相关问题