可以从Android应用程序调用它们的合法linux命令?

可以从Android应用程序调用它们的合法linux命令?,android,c++,linux,android-ndk,java-native-interface,Android,C++,Linux,Android Ndk,Java Native Interface,我需要从C++中的本机方法生成一个linux命令,即iwconfig,还需要使用popen来获取该命令的输出 这是我的密码 std::string exec(const char* cmd) { FILE* pipe = popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[128]; std::string result = ""; while(!feof(pipe)) { if(fgets(buffer, 128, pipe) !

我需要从
C++
中的本机方法生成一个linux命令,即iwconfig,还需要使用
popen
来获取该命令的输出

这是我的密码

std::string exec(const char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
  if(fgets(buffer, 128, pipe) != NULL)
  result += buffer;
  }
  pclose(pipe);
  return result;
 }
我想知道是否可以称之为


因此,我在这个

中有这段代码,将“iwconfig”放在一个shell脚本中,您可以直接从android运行这个过程,处理stdout,直到它完成。。此示例执行shell name=./bin/pars\u submit

        public String getFfmpeg(@QueryParam("infil1") String infil1, 
                @QueryParam("infil2") String infil2, @QueryParam("otfil") String otfil,
                @QueryParam("t") String time) {         
        String outfil = "dummy.mp4";

          List<String> command = new ArrayList<String>();
            command.add("vendor/bin/pars_submit");

            command.add(infil1);     

            command.add(infil2);
            command.add(otfil);
            command.add(time);

System.out.println("Starting process " +command.toString());
            ProcessBuilder builder = new ProcessBuilder(command);
            Map<String, String> environ = builder.environment();
//          for(Entry<String, String> entry : environ.entrySet()){
    //          System.out.println("ENV " +entry.getKey() + " " +entry.getValue());
      //      }
//          builder.redirectErrorStream(true);
            Process process = null;
            try {
                process = builder.start();

            InputStream is = process.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {  
              //System.out.println(line);
                outfil=line;
            }

//          int exitVal = process.waitFor();
 //           System.out.println("Process exitValue: " + exitVal);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            finally {
                  if (process != null) {
                    process.destroy();
                    process = null;
                  }


                }           


            return outfil;


                }
公共字符串getFfmpeg(@QueryParam(“infl1”)字符串infl1,
@QueryParam(“infil2”)字符串infil2、@QueryParam(“otfil”)字符串otfil、,
@QueryParam(“t”)字符串时间){
字符串outfil=“dummy.mp4”;
List命令=new ArrayList();
添加命令(“供应商/bin/pars_提交”);
添加命令(infil1);
添加命令(infil2);
添加命令(otfil);
添加(时间);
System.out.println(“启动进程”+命令.toString());
ProcessBuilder=新的ProcessBuilder(命令);
Map environ=builder.environment();
//for(条目:environ.entrySet()){
//System.out.println(“ENV”+entry.getKey()+“”+entry.getValue());
//      }
//builder.redirectErrorStream(true);
Process=null;
试一试{
process=builder.start();
InputStream=process.getInputStream();
InputStreamReader isr=新的InputStreamReader(is);
BufferedReader br=新的BufferedReader(isr);
弦线;
而((line=br.readLine())!=null){
//系统输出打印项次(行);
出口=管线;
}
//int exitVal=process.waitFor();
//System.out.println(“进程exitValue:+exitVal”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}  
最后{
if(进程!=null){
process.destroy();
进程=空;
}
}           
回流排水;
}

上一个链接中的“是”显示了我的工作,但没有返回值在C/C++中这样做的好处在哪里。我认为Java对执行shell命令本身有相当不错的支持?