本文主要是介绍Andorid利用sserver/runtime实现java执行linux程序或脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
方案一:利用sserver
external/server_mount/socket_server.c
#define THE_PORT 8090
方案二:利用runtime
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
下面介绍可以独立与Android的方案一:
1.sserver部分可以移植出Android部分代码
socket_server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>#define THE_PORT 8090
#define CLIENT_NUM 10
#define BUFFSZ 1024int main()
{int socketfd;int clientfd;char buff[BUFFSZ];struct sockaddr_in sa;struct sockaddr_in cliAdd;socketfd = socket(AF_INET, SOCK_STREAM, 0);if(socketfd == -1){printf("socket create error\n");exit(1);}bzero(&sa, sizeof(sa));sa.sin_family = AF_INET;sa.sin_port = htons(THE_PORT);sa.sin_addr.s_addr = htons(INADDR_ANY);bzero(&(sa.sin_zero), 8);if(bind(socketfd, (struct sockaddr *)&sa, sizeof(sa))!= 0){printf("bind failed\n");exit(1);}if(listen(socketfd ,CLIENT_NUM) != 0){printf("listen error\n");exit(1);}else{printf("listening\n");}socklen_t len = sizeof(cliAdd);int closing =0;while( closing == 0 && (clientfd = accept(socketfd, (struct sockaddr *)&cliAdd, &len)) >0 ){int n;while((n = recv(clientfd,buff, BUFFSZ-1,0 )) > 0){printf("received data = %s\n",buff);printf("received bytes = %d\n", n);buff[n] = '\0';proc_cmd(clientfd , buff);if(strcmp(buff, "quit") == 0){//connection closebreak;}else if(strcmp(buff, "close") == 0){//server closeclosing = 1;printf("server is closing\n");break;}}close(clientfd);}close(socketfd);return 0;
}
void proc_cmd(int sockfd,char buf[])
{char cmdbuf[BUFFSZ];char fpath[64] = "/data/mtinfo";FILE *fp;printf("proc_cmd now\n");sprintf(cmdbuf,"%s 2> %s",buf,fpath);if(-1 == system(cmdbuf)){printf("system call failed\n");exit(1);}if((fp = fopen(fpath,"r")) == -1){printf("open file %s failed\n",fpath);exit(1);}if(!fgets(cmdbuf,sizeof(buf),fp))//cmd successed{printf("----->cmd successed\n");if(send(sockfd,"ok",2,0)<0){perror("send error");exit(1);} }else//fail{printf("----->cmd fail\n");if(send(sockfd,"fail",4,0)<0){perror("send error");exit(1); } }
}
gcc socket_server.c -o server
HelloWorld.java
public class HelloWorld{public static void main(String args[]){System.out.println("Hello World!");RootSeeker.exec("ls"); }
}
RootSeeker.java
//package com.android.server.am.utils;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;//import android.util.Log;public class RootSeeker {private static final String TAG = "RootSeeker";private static final int SOCKET_PORT = 8090;private static final String SOCKET_IP = "127.0.0.1";public static int exec(String cmd) {Log.d(TAG, "<-----------------in exec() start --------------------->");//Log.d(TAG, "exec cmd: " + cmd);Socket socket;BufferedReader in;PrintWriter out;char []buf = new char[256];int ret = 0;try {socket = new Socket(SOCKET_IP, SOCKET_PORT);in = new BufferedReader(new InputStreamReader(socket.getInputStream()));out = new PrintWriter(socket.getOutputStream(),true);BufferedReader line = new BufferedReader(new InputStreamReader(System.in));out.println(cmd);if (in.read(buf) != -1 && new String(buf).equals(new String("ok")))ret = 0;else ret = -1;//Log.d(TAG, "command execute " + ((ret == 0) ? "ok" : "false") + ", return: -->" +buf);line.close();out.close();in.close();socket.close();return 0; //FIXME}catch (IOException e){//Log.d(TAG, e.toString());}return 0;}public static boolean chmod(String fileName) {String cmd = "chmod 777 " + fileName;System.out.println("cmd =" + cmd);try {Runtime runtime = Runtime.getRuntime();Process process = runtime.exec(cmd);return true;} catch (Exception e) {e.printStackTrace();return false;}}}
javac RootSeeker.java HelloWorld.java
3.运行
一个终端执行./server
另一个终端执行
CLASSPATH=$CLASSPATH:.
echo $CLASSPATH
java HelloWorld
结果分别为:
listening
received data = lsreceived bytes = 3
proc_cmd now
HelloWorld.class HelloWorld.java RootSeeker.class RootSeeker.java server socket_server.c
Hello World!
这篇关于Andorid利用sserver/runtime实现java执行linux程序或脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!