Ok I'm learning how shell commands work, so I decided to develop a app to send the commands. This is what I got.
moveDirectory.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
try{
Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/", "cp pic1 /sdcard/pic1"});
send.waitFor();
} catch (Exception ex){
String toast = null;
Log.i(toast, "Couldn't copy file", ex);
}
}
});
But it isn't working, the first command is working, but not the second one. What should I add to it?
Thanks
EDIT: forgot to add the send.waitFor(); line
Use normal command delimeter ;
moveDirectory.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
try{
Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/ ; cp pic1 /sdcard/pic1"});
} catch (Exception ex){
String toast = null;
Log.i(toast, "Couldn't copy file", ex);
}
}
});
In this code you
1) go to the /sdcard/music
2) copy from /sdcard/music pic1 to /sdcard/pic1
I'm speculating, but you may have misunderstood what the parameter to exec really is. It's not a list of commands to be executed (effectivly a batch/shell script), but a single command WITH it's arguments to be executed by a shell. Making it a one-liner like Pepelac suggests or putting the series of commands into a single file that you execute with exec later may be what you are looking for. For the command you are trying to execute there is absolute no reason why you can not make it a one-liner with the full source path included (instead of changing to it), but there may be other reasons why you need to do this that you have not mentioned.
Related
In a program I am developing using jcef, I want to allow the camera to be used inside. I am trying to give --enable-media-stream switch for the program that I am building using jcef. I am tried to turn on switch with the following cases:
settings.windowless_rendering_enabled = useOSR;
settings.command_line_args_disabled = false;
String[] args = new String[]{
"--enable-media-stream=true"
// "--enable-media-steam",
// "enable-media-stream", "1"
};
cefApp = CefApp.getInstance(args, settings);
Program gives
[1031/094701.279:INFO:CONSOLE(0)] "Uncaught (in promise) NotAllowedError: Permission denied", source: theUrl
error in every cases.
However, the camera is allowed if I run *.jar app from command line with: "java -jar appName.jar --enable-media-stream" command.
What might be the issue here?
Thanks in advance.
I solved it with next code:
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
#Override
public void onBeforeCommandLineProcessing(String process_type, CefCommandLine command_line) {
super.onBeforeCommandLineProcessing(process_type, command_line);
if (process_type.isEmpty()) {
command_line.appendSwitchWithValue("enable-media-stream","true");
}
}
});
I have a Java program, that on runtime, extracts some executables to a specific folder, and tries to run them. Of course, before running the executable, its permissions need to be changed. For that purpose, I am using the following piece of code:
public static void changePermissions(String filename,String path){
String[] cmd=new String[3];
cmd[0]="chmod";
cmd[1]="u+x";
cmd[2]=filename;
BetterRunProcess process=new BetterRunProcess();
process.runProcessBuilderInDifferentDirectory(cmd,path,1,0,0,"");
}
In the above code snippet,the variable path contains the path to the executable, and filename is the name of the executable. The line:
process.runProcessBuilderInDifferentDirectory(cmd,path,1,0,0,"");
executes the command "chmod u+x ...". On my own computer, the code works just fine, but when I run it on someone else's computer, the following error is thrown:
chmod: changing permissions of deviceQuery.out. Operation not permitted.
Can someone figure-out what might be the problem behind this?
Here is some more code, that might be helpful.
public void runProcessBuilderInDifferentDirectory(String[] cmd,String path,int printToConsole,int printToExternalFile,int append,String fileName){
ProcessBuilder builder;
if(cmd.length==1) builder=new ProcessBuilder(cmd[0]);
else if(cmd.length==2) builder=new ProcessBuilder(cmd[0],cmd[1]);
else if(cmd.length==3) builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2]);
else if(cmd.length==4) builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2],cmd[3]);
else builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2],cmd[3],cmd[4]);
builder.directory(new File(path));
try {
Process pr=builder.start();
if(printToConsole==1) printToConsole(pr);
if(printToExternalFile==1) printToExternalFile(pr,fileName,append);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thanks!
Run your java code from that user which has the permission for that file.
So my problem here is that I'm not too sure how to print a set of commands to cmd. I have a batch file that runs a Minecraft server, and I need to be able to run commands through the command prompt that shows up when I run the batch file, which will in turn perform commands to the server.
Here is my code so far:
package com.Kaelinator;
import java.io.IOException;
public class ServerManager {
public static void main(String[] args){
try {
System.out.println("Opening");
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("cmd /C start /min " + "C:/Users/Owner/Desktop/rekt/Run.bat");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println("Closing");
//process.destroy();
} catch (IOException e){
e.printStackTrace();
}
}
}
Yes, I understand that all this does so far is open the batch file. :P I am expecting something like this that I need to add to my code:
process.InputStream(command1);
But I am certain that there is more to it, something along the lines of bufferedWriters or something like that...
Whenever I try to get answers from Google, the answers always have a whole load of extra code, or have something completely different about them.
Thanks!
I am trying to run a .cmd program from Java. Doesn't run.
I'm using Runtime.exec as advised in some other posts.
public void mouseClicked(MouseEvent arg0) {
Runtime runtime = Runtime.getRuntime();
String path = "E:/Marvin/Marvin_Cleanup.CMD";
try {
runtime.exec(path);
} catch (Exception e1) {
e1.printStackTrace();
}
}
Im not familiar with windows executeables but using the process builder combined with that url should work fine.
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
If you call further batches from your batch file you might need to use cmd /c prefix. https://superuser.com/questions/712279/commands-run-in-a-batch-file-only-when-writing-cmd-c-before
String path = "cmd /c E:/Marvin/Marvin_Cleanup.CMD";
I'm developing a simple app that injects lines on build.prop by executing a shell command. My main problem is that every time I check a toggle that create the function a toast displaying the shell string appear. Is there any way to avoid this? also, if you have any suggestion to clean a bit the code would be appreciated! (First app for me).
Code:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View v = (LinearLayout)inflater.inflate(R.layout.performance,container,false);
final CheckBox hwdebug = (CheckBox) v.findViewById(R.id.hwDebug);
final String[] mountrw = {"su","-c","mount -o remount,rw /system"};
final String[] enhwdebug1 = {"su","-c","sed -i '/debug.sf.hw=*/d' /system/build.prop"};
final String[] enhwdebug2 = {"su","-c","echo '## Rendering GPU Enabled ##' >> /system/build.prop"};
final String[] enhwdebug3 = {"su","-c","echo debug.sf.hw=1 >> /system/build.prop"};
final String[] dishwdebug1 = {"su","-c","sed -i '/debug.sf.hw=1/d' /system/build.prop"};
final String[] dishwdebug2 = {"su","-c","sed -i '/## Rendering GPU Enabled ##/d' /system/build.prop"};
final SharedPreferences hwdebugpref = this.getActivity().getSharedPreferences("hwdebugck",0);
// GPU Rendering Checkbox
boolean hwdebugck = hwdebugpref.getBoolean("hwdebugck", false);
if (hwdebugck) {
hwdebug.setChecked(true);
} else {
hwdebug.setChecked(false);
}
hwdebug.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((hwdebug.isChecked())) {
SharedPreferences.Editor editor = hwdebugpref.edit();
editor.putBoolean("hwdebugck", true); // value to store
editor.commit();
ArrayList<String[]> enhwdebug = new ArrayList<String[]>();
enhwdebug.add(mountrw);
enhwdebug.add(enhwdebug1);
enhwdebug.add(enhwdebug2);
enhwdebug.add(enhwdebug3);
for(String[] cmd:enhwdebug){
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.fillInStackTrace();
}
}
} else {
SharedPreferences.Editor editor = hwdebugpref.edit();
editor.putBoolean("hwdebugck", false); // value to store
editor.commit();
ArrayList<String[]> diswdebug = new ArrayList<String[]>();
diswdebug.add(mountrw);
diswdebug.add(dishwdebug1);
diswdebug.add(dishwdebug2);
for(String[] cmd:diswdebug){
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.fillInStackTrace();
}
}
}
}
});
So, my main problem is that su -c show that annoying toast. I tried to pass it to busybox or toolbox but without success since they need to be ran with su.
Thank you!
It is possible , by having the same thread that makes the call to root commands stay and let it always be the only one that handles them.
This way, the toast will only appear the first time you use root operations.
Also, on the end user side, some apps (like super-su) allow to avoid the toast, even per app.
Ok first to answer your question the answer is yes and no.
Easy Answer:
No its not possible using one of the current SU managers like SuperUser or SuperSU you cant. The toast is a safety mechanism. Both apps features to remove the toast for specific apps, but you as a dev can not control this.
Hard Answer:
Yes it is possible, but it would require you compiling your own su binary and using it in place of the su binary already installed. You would need to remove the code that references the current manager (Which ever source you compiled from). Would be recommend to add checks so that ONLY your app can run that binary. This can lead to security risks though and is probably is not a good idea.
I did not look through your code very much but i would say one thing do NOT under any circumstance run SU commands on the UI thread. This is only asking for problems.