Android中执行java命令的方法及java代码执行并解析shell命令


android中执行java命令的方法大家都晓得吗,下面一段内容给大家带来了具体解析。

android的程序基于java开发,当我们接上调试器,执行adb shell,就可以执行linux命令,但是却并不能执行java命令。

那么在android的shell中是否就不能执行java程序了呢。

答案是否定的。我们可以通过app_process来执行java程序。

写一个hello world吧,就是刚开始学java的时候 写得那个hello world,这次要在android上运行。

用记事本新建hello.java文件,编写如下代码:

public static class hello {
  public void main(String args[]){
    System.out.println("Hello Android");
  }
}

得到hello.class文件 执行"java hello" 可以看到输出结果

那么如何让这个最简单的java程序 在android上运行呢。

.class文件可以在普通的jvm上运行,要放到android下还需要转换成dex,需要用android sdk中的dx工具进行转换

dx --dex --output=hello.dex hello.class

得到hello.dex,这个hello.dex就可以放到android上执行了。

连接手机,打开usb调试

复制代码 代码如下:

adb push hello.dex /sdcard/

adb shell 进入android命令行

使用app_process 运行hello.dex

复制代码 代码如下:

app_process -Djava.class.path=/sdcard/hello.dex /sdcard hello

好了,至此我们成功的在android上运行了普通的java程序。

要知道这可是用记事本写的android代码,真是闻所未闻啊!赶快像小伙伴炫耀一下吧。

PS:JAVA代码执行shell命令并解析

在Android可能有的系统信息没有直接提供API接口来访问,为了获取系统信息时我们就要在用shell指令来获取信息,这时我们可以在代码中来执行命令 ,这里主要用到ProcessBuilder 这个类.

代码部分  :

package com.yin.system_analysis; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
public class MainActivity extends Activity { 
 private final static String[] ARGS = {"ls","-l"}; 
 private final static String TAG = "com.yin.system"; 
 Button mButton; 
 TextView myTextView; 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  mButton = (Button) findViewById(R.id.myButton); 
  myTextView = (TextView) findViewById(R.id.textView); 
  mButton.setOnClickListener(new OnClickListener() { 
   public void onClick(View v) { 
    myTextView.setText(getResult()); 
   } 
  }); 
 } 
 public String getResult(){ 
   ShellExecute cmdexe = new ShellExecute ( ); 
   String result=""; 
   try { 
   result = cmdexe.execute(ARGS, "/"); 
  } catch (IOException e) { 
   Log.e(TAG, "IOException"); 
   e.printStackTrace(); 
  } 
  return result; 
 } 
 private class ShellExecute { 
  /*
   * args[0] : shell 命令 如"ls" 或"ls -1";
   * args[1] : 命令执行路径 如"/" ;
   */ 
  public String execute ( String [] cmmand,String directory) 
  throws IOException { 
  String result = "" ; 
  try { 
  ProcessBuilder builder = new ProcessBuilder(cmmand); 
  if ( directory != null ) 
  builder.directory ( new File ( directory ) ) ; 
  builder.redirectErrorStream (true) ; 
  Process process = builder.start ( ) ; 
  //得到命令执行后的结果 
  InputStream is = process.getInputStream ( ) ; 
  byte[] buffer = new byte[1024] ; 
  while ( is.read(buffer) != -1 ) { 
  result = result + new String (buffer) ; 
  } 
  is.close ( ) ; 
  } catch ( Exception e ) { 
   e.printStackTrace ( ) ; 
  } 
  return result ; 
  } 
 } 
} 

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3