SoFunction
Updated on 2025-04-08

Methods of executing java commands and java code execution and parsing shell commands in Android

Do you know the method of executing java commands in android? The following paragraph brings you a specific analysis.

Android's program is developed based on Java. When we connect to the debugger and execute the adb shell, we can execute Linux commands, but we cannot execute Java commands.

So, can't Java programs be executed in android shell?

The answer is no. We can execute Java programs through app_process.

Let’s write a hello world when I first started learning Java. I wrote the hello world, which will run on Android this time.

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

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

Get the file and execute "java hello" to see the output result

So how to make this simplest java program run on android.

The .class file can be run on ordinary jvm. If it is placed on Android, it needs to be converted into dex. It needs to be converted using the DX tool in Android sdk.

dx --dex --output= 

Get it, this can be executed on android.

Connect to the phone and turn on usb debugging

Copy the codeThe code is as follows:

adb push /sdcard/

adb shell enters the Android command line

Run with app_process

Copy the codeThe code is as follows:

app_process -=/sdcard/ /sdcard hello

OK, so far we have successfully run ordinary java programs on android.

You should know that this is Android code written in notepad, it is really unheard of! Show it off quickly as a friend.

PS: JAVA code executes shell command and parses

On Android, there may be system information that does not directly provide API interface for access. In order to obtain system information, we have to use shell instructions to obtain information. At this time, we can execute commands in the code. The ProcessBuilder class is mainly used here.

Code part:

package .system_analysis; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class MainActivity extends Activity { 
 private final static String[] ARGS = {"ls","-l"}; 
 private final static String TAG = ""; 
 Button mButton; 
 TextView myTextView; 
 public void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setContentView(); 
  mButton = (Button) findViewById(); 
  myTextView = (TextView) findViewById(); 
  (new OnClickListener() { 
   public void onClick(View v) { 
    (getResult()); 
   } 
  }); 
 } 
 public String getResult(){ 
   ShellExecute cmdexe = new ShellExecute ( ); 
   String result=""; 
   try { 
   result = (ARGS, "/"); 
  } catch (IOException e) { 
   (TAG, "IOException"); 
   (); 
  } 
  return result; 
 } 
 private class ShellExecute { 
  /*
    * args[0] : shell command 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 ) 
   ( new File ( directory ) ) ; 
   (true) ; 
  Process process =  ( ) ; 
  //Get the result after the command is executed  InputStream is =  ( ) ; 
  byte[] buffer = new byte[1024] ; 
  while ( (buffer) != -1 ) { 
  result = result + new String (buffer) ; 
  } 
   ( ) ; 
  } catch ( Exception e ) { 
    ( ) ; 
  } 
  return result ; 
  } 
 } 
}