There is a situation here, I'm developing an Android application, using Java. I'm pretty familiar with all this stuff, but now it's the first time when I need to use SU permissions. I just need to replace (actually, rename) the file in system/app directory, but it looks like I'm not able to perform it in a usual way (renameTo method in File class), it just returns me FALSE, which means that there was some error in operation.
So can anybody tell me how to use SU? My test phone is fully rooted with SU 3.0.3.2, any app that require SU works perfectly fine.
Shall I use the same method but with some additions in manifest? Shall I use busybox in some way?
I already googled for this, and I can't find any useful information. And also, there is no documentation on official Android Superuser website.
Thanks in advance!
You'll probably also need to remount the filesystem as RW since /system is read-only. So you might need to call SU with a similar command below:
mount -o r,remount -t yaffs2 /dev/block/mtdblock3 /system
To execute the command, you can try two ways (I've notice in android sometimes one works and the other won't )
Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "mount -o r,remount -t yaffs2 /dev/block/mtdblock3 /system"});
Or you can do
Process p = Runtime.getRuntime().exec("su");
p.getOutputStream().write("mount -o r,remount -t yaffs2 /dev/block/mtdblock3 /system".getBytes());
p.getOutputStream().write(<my next command>);
Related
I want my directory to give executable permissions (by default) to all the shell scripts which are going to be created in that directory after I run my Java program. I tried the following commands:
setfacl -d -m group:name:rwx /path/to/your/dir
find ./ -name "*.sh" -exec chmod +x {} \;
The first one is not showing any response while the second one works fine but I have to execute this command manually in terminal after my Java program has created all the scripts. This is not what i seek. I want this thing to be automatic. Here is what I am trying to achieve:
My Java program creates the .sh files in a directory.
Now the program would try to execute this script file.
Here is a Java code snippet which shows how it is going to execute the script files:
ExecuteShellComand obj = new ExecuteShellComand();
String command2 = "./script.sh";
String output2 = obj.executeCommand(command2);
It doesn't run unless I give the executable permissions to the script.sh. How do I do it? Is there any way around it? If I am not doing something in a way it should be done, feel free to give your suggestions. Thanks
Default ACL permissions are masked by the file's creation mode as specified in open and mkdir syscalls.
Since files are usually created with a default mode of 0666, execute permissions will be masked instead of inherited.
It's the responsibility of the program that creates the files to create them with the right permissions. If you set the permissions correctly when creating the scripts, you won't need ACL or chmod at all. The best way to fix this would be for your program to set the mode in the open call to 0777.
Java appears to have Files.createFile for this. Additionally, you have a more fuzzy File.setExecutable to do it after the fact, which is not Unix canonical behavior, but probably fine for your use case.
I need to store files on a network drive (MyBook Live). At the moment i'm doing this:
String cmd = "mount //192.167.0.3/Public/Folder /media/Folder -o user=test,password=test";
Runtime run = Runtime.getRuntime() ;
Process pr = run.exec(cmd);
pr.waitFor();
Is there a better way to do it?
Did the same thing - executing a command to do the mount - for a project I was working on last fall. There are no Java API to do this directly (or any better way as far as I know). It worked properly.
I agree with Bailey S recommendation of an fstab entry - did the same thing.
You might want to read on the pitfalls of using Runtime.exec() and Process.waitFor() (http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=2). I prefer using Apache Commons Exec (http://commons.apache.org/proper/commons-exec/) for executing OS commands.
I can get root access to phone using 'adb root' in my PC then change a file content under /data folder. Now I want to do it in a apk using Java code.
I tried blow code:
Runtime.getRuntime().exec(new String[]{"sh", "/sdcard/my.sh"});
This function works well when shell command do not need a root permission.
Now I change my.sh into
su root
chmod 777 /data/filetochange.xml
rm -r /data/filetochange.xml
But it didn't work. Can I do this in Java code?
I think your app need have root permission. You can try to add a line 'android:sharedUserId="android.uid.system"' to AndroidManifest.xml, which property can get system permission. But this is not enough, you need to do other steps and you can google it, there are many detailed tutorials online.
I'm trying to write Java code that executes some terminal commands. The code should execute this command sudo mount -o loop system.img system. But there are several problems. First, to execute this command I have to be root. I know that I can be by sudo su, but how can I stay as root when I close the terminal window? If I use the command sudo mount -o loop system.img system how can I provide the password in the Java code?
The second issue is: can I execute the command as below?
File f2 = new File("/home/user1/Desktop/aDirectory");
String[] commands = new String[]{"sudo mount", "-o", "loop", "/home/user1/Desktop/aDirectory/system.img"};
Runtime.getRuntime().exec(commands, null, f2);
I think I can't. So how can I do it? Any ideas?
Notes: system.img is a compiled Android os file. and the system is an empty directory. The thing I'm trying to do is mount the system.img file into the system directory.
Programs like sudo read the password directly from the terminal device, not from stdin, so this is unfortunately not a trivial thing to do. I'm not sure if this is realistic for Android or not, but on a general UNIX system the easiest solution is to use expect, which is a library for simulating a terminal and thereby automating these kinds of interactions. It's often used as a standalone program embedded in Tcl, and I've thrown together systems in which Java launched expect to talk to tools like sudo, and it works fine.
expect includes a sort of declarative scripting language that tells it how to run another program and how to react to that program's output.
What you would do is use Runtime.exec() to execute the expect program, supplying a script that just runs "sudo mount", watches for the password prompt, and provides the password. The script would probably just look something like (G4rb4geB4rg3 is the password):
spawn sudo mount -o loop /home/user1/Desktop/aDirectory/system.img
expect "password:"
send "G4rb4geB4rg3\r"
expect eof
The problem was solved, by using shell script.
I wrote a script includes just this line :
echo myPassword | sudo -S mount -o loop system.img system
then I run it in my java code, such :
Runtime.getRuntime().exec("sh 1.sh");
I'm pretty sure 'sudo' and 'mount' would be separate, since it's not a single executable you're invoking. Also, if you start sudo with the -S command line switch it can take the password directly from stdin, so you just need to start the process and pass in whatever the password's configured as to the input stream.
I have an executable file (ffmpeg) that I'm trying to run with a Java program on a Mac. I used the Java program to send the command chmod 777 /path/to/ffmpeg, but when I try to run ffmpeg, I get the following error:
java.io.IOException: Cannot run program "/Users/james/WalkTheHall/ffmpeg": error=13, Permission denied
But when I run chmod 777 /path/to/ffmpeg from Terminal on my own before opening the Java application, the command to ffmpeg will run just fine in the Java program.
Is there a difference between calling chmod from within the Java program and calling it on my own? Why will it not work? Thank you!
I just had the same problem in my code.
i solved this by add waitFor after exec. The "chmod" process is not finished when next command is executed. the code may look like:
p = Runtime.getRuntime.exec("chmod 777 xxx");
p.waitFor();
Runtime.getRuntime.exec("./xxx");
I'd guess that chmod is a shell command, not an executable. Try running chmod through your shell. See more details here: Want to invoke a linux shell command from Java
Yes, there is a difference. When you run the command from the terminal, it is you who is performing the action, and thus it is performed using your credentials. The Java application is running the command using the Java application's permissions. This is to prevent an application from running and then making dangerous, unwanted changes to the file system. Perhaps someone else can elaborate and give guidance to a workaround for this.
I am currently working on a project that also makes use of FFMpeg on OSX. I store FFMpeg in the JAR and extract it and set executable on use as you seem to be doing. This is what I do, and it seems to work.
public static void setExecutable(File file, boolean executable)
{
Process p = Runtime.getRuntime().exec(new String[] {
"chmod",
"u"+(executable?'+':'-')+"x",
file.getAbsolutePath(),
});
// do stuff to make sure p finishes & capture output
}
The code is GPL, so feel free to check it out. Its not the nicest codebase, and even the FFMpeg stuff is perhaps overly complex, but it works.
Source is viewable at http://korsakow.net
These two files in particular might be interesting for you
FFMpegEncoderOSX.java
FileUtil.java
Try this:
File commandFile = new File("myFile.txt");
commandFile.setExecutable(true);
Process p = Runtime.getRuntime.exec(commandFile.getAbsoluteFile());
to start an program on OSX you need this:
Runtime.getRuntime().exec("chmod 777 "+path); //in order to execute it
Runtime.getRuntime().exec(path); //execute it
Runtime.getRuntime().exec("chmod 744 "+path); //undo every change
path should be the path to the exc of the program, for example:
AppStore -> Applications/App\ Store.app/Contents/MacOS/App\ Store