Android studio - How to use directly the terminal from java method? - java

I am new to Android development, and I try to access to the internal terminal (/bin/bash, ...) of Android phone using a java method.
Do you know if such java method exist?
Thanks

You can use Runtime and Process to achieve your task.
private static String executeCommand(String command) {
Process process = null;
BufferedReader reader = null;
String result = "";
try {
String line;
process = Runtime.getRuntime().exec(command);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null)
result += line + "\n";
} catch (final Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (process != null)
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
Where command is any available terminal commands like PING 8.8.8.8

I'm not entirely sure as to what you mean by "accessing the internal terminal". But if you would like to execute commands take a look at the documentation of the Runtime class.
Here's an example on how to use it.

Related

How to convert ARP code of JAVA to Kotlin in Android

I am trying to get all the mac and IP from my android using the following code. But the following code only works in java. I want to use it in kotlin so I tried java to kotlin converter. But it didn't work. could anyone tell me how the following part of the code will be used in kotlin:
listNote.clear();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
Node thisNode = new Node(ip, mac);
listNote.add(thisNode);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Auto conversion does not handle your while loop since "Assignments are not expression".
You can work around it by using built-in Readers extension function forEachLine, in which case each line is passed as the only argument to the lambda expression as it:
var bufferedReader: BufferedReader? = null
try {
bufferedReader = BufferedReader(FileReader("/proc/net/arp"))
bufferedReader.forEachLine {
val splitted = it.split(" +".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (splitted.size >= 4) {
val ip = splitted[0]
val mac = splitted[3]
if (mac.matches("..:..:..:..:..:..".toRegex())) {
listNote.add(Node(ip, mac))
}
}
}
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
bufferedReader?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
You can also remove FileNotFoundException since it's a subclass of IOException, and catch block is the same.

Open files using root android

In java, after getting root permissions, how can I open files with it? Can I open them with File class or do I have to use a command?
You will have to use the su command.
I suggest you have a look at how to use su
Sample code to read a file ( I haven't tested it but it should give you an idea ):
public static void runAsRoot(String[] cmds){
try {
Process p = Runtime.getRuntime().exec("su");
BufferedReader reader = new BufferedReader(p.getInputStream());
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line); // add everything to StringBuilder
// here you can have your logic of comparison.
if(line.toString().equals(".")) {
// do something
}
}
} catch(IOException e) {
e.printStackTrace();
} catch(InterruptedException e) {
e.printStackTrace();
}

How to read Android properties with Java

I use 'adb shell getprop' in the terminal.
What interfaces can I use in Android JAVA to get the same information?
I have tried several things like:
Properties sysProps = System.getProperties();
But I don't think these are the same properties I am looking for? Specifically, I want to find values that will return similar to the following:
adb shell getprop | grep dolby
The shell 'grep dolby' command returns this:
[audio.dolby.ds2.enabled]: [true]
[dolby.audio.sink.info]: [headset]
[dolby.ds.dialogenhancer.state]: [on]
[dolby.ds.graphiceq.state]: [off]
[dolby.ds.hpvirtualizer.state]: [off]
[dolby.ds.intelligenteq.preset]: [Off]
[dolby.ds.intelligenteq.state]: [off]
[dolby.ds.platform]: [qcom]
[dolby.ds.profile.name]: [Movie]
[dolby.ds.spkvirtualizer.state]: [off]
[dolby.ds.state]: [off]
[dolby.ds.volumeleveler.state]: [on]
But I want to access this information in Android JAVA code.
Any ideas?
I cleaned up TMont's solution and made it more generic (added parameter for propertyName):
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SystemProperties {
private static String GETPROP_EXECUTABLE_PATH = "/system/bin/getprop";
private static String TAG = "MyApp";
public static String read(String propName) {
Process process = null;
BufferedReader bufferedReader = null;
try {
process = new ProcessBuilder().command(GETPROP_EXECUTABLE_PATH, propName).redirectErrorStream(true).start();
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = bufferedReader.readLine();
if (line == null){
line = ""; //prop not set
}
Log.i(TAG,"read System Property: " + propName + "=" + line);
return line;
} catch (Exception e) {
Log.e(TAG,"Failed to read System Property " + propName,e);
return "";
} finally{
if (bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {}
}
if (process != null){
process.destroy();
}
}
}
}
In case someone wanted to know my solution... with George's help I ended up using this:
private String propReader() {
Process process = null;
try {
process = new ProcessBuilder().command("/system/bin/getprop")
.redirectErrorStream(true).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = process.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("dolby"))
log.append(line + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(in);
process.destroy();
return log.toString();
}
There actually is a system side implementation of the getprop call. It is called Systemproperties.get() and can be found here. For users, that work on system code inside the AOSP, or do want to take the risk of using reflect, this is the way to go.
System.getProperties() does not return the same properties as getprop.
To get getprop properties, try executing getprop using Runtime.exec() and reading its standard output.
For getting the system properties using reflection, try the below code snippet with your required property name.
public String getSerialNumber() {
String serialNumber;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serialNumber = (String) get.invoke(c, "ril.serialnumber");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "ro.serialno");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "ro.boot.serialno");
if (serialNumber.equals(""))
serialNumber = (String) get.invoke(c, "ro.kernel.androidboot.serialno");
// If none of the methods above worked
if (serialNumber.equals("")) {
serialNumber = Build.SERIAL;
}
} catch (Exception e) {
e.printStackTrace();
serialNumber = "";
}
return serialNumber;
}

Detecting terminal command errors in Java

In my Java application I am using the exec() command to call a terminal function:
p = Runtime.getRuntime().exec(command);
p.waitFor();
The call uses the zip and unzip calls. Originally I call:
zip -P password -r encrypted.zip folderIWantToZip
When I call the unzip function through java, I specify the password as the method parameter. If the correct password is specified then the call should unzip the encrypted folder:
unzip -P password encrypted.zip
I want a way to find out if the password entered is incorrect. For example, if password is correct, then the call will correctly unzip the zip file. But I notice that no exception is thrown for an incorrect password. How can I determine this?
You could read the process's ErrorStream and InputStream to determine the process output. Sample code given below
public static void main(String[] args) {
try {
String command = "zip -P password -r encrypted.zip folderIWantToZip";
Process p = Runtime.getRuntime().exec(command);
InputStream is = p.getInputStream();
int waitFor = p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("line:" + line);
}
is = p.getErrorStream();
reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
System.out.println("ErrorStream:line: " + line);
}
System.out.println("waitFor:" + waitFor);
System.out.println("exitValue:" + p.exitValue());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You could use the exitcode to validate the process status as well but it is specific to to program. Normally zero means successfully terminated otherwise abnormal termination.
As per my comment, first thing I would do would be to capture the Process's InputStream and ErrorStream via getInputStream() and getErrorStream(), but especially the latter, the ErrorStream, and check to see what it outputs if the input is in error. Note that these would have to be done in their own thread, else you'll tie up your program. I usually use some type of StreamGobbler class for this. Also, don't ignore the int returned by p.waitFor().
e.g.,
ProcessBuilder pBuilder = new ProcessBuilder(COMMAND);
Process process = null;
try {
process = pBuilder.start();
new Thread(new StreamGobbler("Input", process.getInputStream())).start();
new Thread(new StreamGobbler("Error", process.getErrorStream())).start();
int exitValue = process.waitFor();
System.out.println("Exit Value: " + exitValue);
process.destroy();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
And:
class StreamGobbler implements Runnable {
private String name;
private Scanner scanner;
public StreamGobbler(String name, InputStream inputStream) {
this.name = name;
scanner = new Scanner(inputStream);
}
#Override
public void run() {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(name + ": " + line); // or better, log the line
}
scanner.close();
}
}

Process started from Java hangs

I am trying to execute a c++ code from java on a remote Windows machine. In order to deal with the remote part, I have created a Web service from where the actual command is run using Runtime.exec(). The c++ exe is not being called directly from the java code. I have a batch file that eventually calls the exe.
The problem is, both java and c++ processes hang. The java code on server side does handle the output stream and error stream. Also, the c++ code is logging everything in a file on Windows. The strange thing is that, when I remove the WS call and run the java code on server side as a standalone java program, it succeeds. Here is the java code:
public class RunCPlusPlusExecutable {
public int runExecutable() {
int exitValue = 0;
try {
Process p = null;
Runtime rt = Runtime.getRuntime();
System.out.println("About to execute" + this + rt);
p = rt.exec("c:/temp/execcplusplus.bat");
System.out.println("Process HashCode=" + p.hashCode());
StreamProcessor errorHandler = new StreamProcessor(p.getErrorStream(), "Error");
StreamProcessor outputHandler = new StreamProcessor(p.getInputStream(), "Output");
errorHandler.start();
outputHandler.start();
exitValue = p.waitFor();
System.out.println("Exit value : " + exitValue);
if (exitValue == 0)
System.out.println("SUCCESS");
else
System.out.println("FAILURE");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
}
return exitValue;
}
class StreamProcessor extends Thread {
private InputStream is = null;
private String type = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private FileWriter writer = null;
private BufferedWriter out = null;
StreamProcessor(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
writer = new FileWriter("*******path to log file********");
out = new BufferedWriter(writer);
String line = null;
while ((line = br.readLine()) != null) {
Date date = new Date();
out.write("[" + type + "]: " + date + " : " + line);
out.newLine();
}
writer.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (out != null)
out.close();
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Any idea what is causing the problem and how to debug it? Please note that I won't be able to debug the c++ code.
Thanks
Update 1:
Here are some more details...
The WS server is running from some admin user. And I have been running the standalone java program from some other user.
*It seems that the c++ executable is giving referenced memory error while executing from WS call. There are pop-ups citing the error with OK and Cancel buttons. *
Update 2:
The tomcat server where the WS is deployed is running as a Windows NT service. Can that be the cause of the error? If yes, how to resolve this?

Categories