The method of executing Java commands in Android and Java code Execution and parsing shell command _android

Source: Internet
Author: User
Tags static class

How to execute Java commands in Android Everyone knows that, the following section of the content for you to bring a specific analysis.

The Android program is based on Java development, and when we connect to the debugger, execute the ADB shell, we can execute the Linux commands, but we can't execute the Java commands.

So is it possible to execute Java programs in the Android shell?

The answer is in the negative. We can execute Java programs through app_process.

Write a Hello world, that is, when you first started learning Java, that Hello World, this time to run on Android.

Create a new Hello.java file with Notepad and write the following code:

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

Get hello.class File Execution "Java hello" to see output results

So how do you make this simplest Java program run on Android?

The. class file can be run on an ordinary JVM, and it needs to be converted to Dex under Android, and it needs to be converted using the DX tools in the Android SDK

DX--dex--output=hello.dex Hello.class

With Hello.dex, this hello.dex can be implemented on Android.

Connect the phone, turn on USB debugging

Copy Code code as follows:

ADB push hello.dex/sdcard/

ADB Shell enters Android command line

Use App_process to run Hello.dex

Copy Code code as follows:

App_process-djava.class.path=/sdcard/hello.dex/sdcard Hello

Well, so far we've managed to run the normal Java program on Android.

You know this is the Android code written in Notepad, it's unheard of! Hurry up and show off like a little buddy.

Ps:java code executes shell commands and parses

The system information that Android may have does not provide the API interface directly to access, in order to obtain the system information we need to use the shell instruction to obtain the information, then we can execute the command in the code, here mainly uses the Processbuilder this class.

Code section:

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 commands such as "LS" or "ls-1";
   * args[1]: Command execution path such as "/"; 
  */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 (); 
  Gets the result of the command execution 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;  } 
 } 
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.