I tried to use this code to delete a file located into /data folder but it doesn't work, what's wrong in it? My device has root.
Runtime.getRuntime().exec(new String[]{"su","rm"+" "+"/data/logger"});
SOLVED USING THIS
Process p;
try {
p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("rm /data/logger"+"\n");
os.writeBytes("exit\n");
os.flush();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Use the -f switch to force the delete.
File exists case is taken care by runtime class.
Try this:
Runtime.getRuntime().exec(new String[]{"su","rm","-f","/data/logger"});
or this
Runtime.getRuntime().exec("su")
Runtime.getRuntime().exec(new String[]{"rm","-f","/data/logger"});
Related
I am running an executeable from java(1.8) on Windows 7
Process process = null;
try {
process = new ProcessBuilder("D:\\Demo\\pvaroptimizer\\bin\\pvaroptimizer.exe","-p",importantInfo[0],"D:\\Demo\\pvaroptimizer\\bin\\New structure.pvz").start();
while(process.isAlive())
{
Thread.sleep(5000);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The exeucteable runs properly but the exe which normally takes 3 Gb space in memory(checked in the taskbar) when run from commmand prompt, now reaches a maximum of 30 mb space only, hence slowing down the execution. Can someone please help me to know about this limitation.
The proble is that pvaroptimizer.exe write to output, but output buffer have limited size. When buffer is full pvaroptimizer.exe blocks until there is free space in buffer.
Next code should solve this problem.
Process process = null;
try {
ProcessBuilder builder = new ProcessBuilder("D:\\Demo\\pvaroptimizer\\bin\\pvaroptimizer.exe", "-p", importantInfo[0], "D:\\Demo\\pvaroptimizer\\bin\\New structure.pvz");
builder.redirectErrorStream(true);
process = builder.start();
InputStream stream = process.getInputStream();
while(stream.read()!=-1){}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am finding trouble in executing symlink command in java. My source filename and destination filename has space in it.
How to execute this command?
Eg. ln -sf /home/Desktop/image1 .jpg /home/Desktop/Folder/image 2.jpg
I am trying this code
String command = "ln -sf " + "/home/Desktop/\"image 1.jpg\"" + " /home/Desktop/Folder/\"image 2.jpg\"";
CommandLine oCmdLine = CommandLine.parse(command);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can do it easy with NIO.2
Path directoryTarget = Paths.get("c:/temp");
Path directoryLink = Paths.get("c:/links/linkTemp");
Files.exists(directoryTarget);
try {
Files.createSymbolicLink(directoryLink, directoryTarget);
} catch (IOException e) {
e.printStackTrace();
}
hi I have a JAVA project in which I want to display the MySQL databases.
i write this code:
try {
String []command={"mysql -u root -pmanager","show databases"};
Process p= Runtime.getRuntime().exec("mysql -u root -pmanager");
Process p1= Runtime.getRuntime().exec("show databases");
if (p.waitFor()==0){System.out.println("backup done...");}
else{System.out.println("!!!");}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but it give me this error:
Cannot run program "show": CreateProcess error=2, The system cannot find the file specified
what it should be do?
thanks...
Better to pass String object in exec method like:
Runtime.getRuntime().exec( new String [] {"mysql", "-u", "root", "-pmanager", "-e", "show databases"} )
You need to specify what is 'mysql' to command prompt...
example - C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql
What's the difference between running programs using java and run it using the command line? In the first case it does not work, but in the second case it works fine.
Java:
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("../../../my/prog \"//10.124.12.15/C:/output/*\" ../../../input/345 -N -A");
DataInputStream bis = new DataInputStream(proc.getInputStream());
int _byte;
while ((_byte = bis.read()) != -1)
System.out.print((char)_byte);
proc.waitFor();
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
AND command:
../../../my/prog "//10.124.12.15/C:/output/*" ../../../input/345 -N -A
Try using absolute path. Maybe that's your problem.
Thanks all, I solved my problem:
try {
String cmd="/progs/my/prog //10.124.12.15/C:/output/* /temp/input/345 -N -A"
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(
new String[]{"/usr/bin/bash", "-c", cmd, "1>/dev/null 2>&1"});
proc.waitFor();
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
I have a sample Java application that I registered as a service using Procrun. I am trying to execute Batch file from my application
public class Service {
public static void main(String args[]) throws IOException, InterruptedException {
if(args.length>0){
if(args[0].equals("start")){
ProcessBuilder builder = new
ProcessBuilder("cmd","/c","start","Start.bat");
builder.start();
}else if(args[0].equals("shutdown")){
ProcessBuilder builder = new
ProcessBuilder("cmd","/c","start","Stop.bat");
builder.start();
}
}
}
}
When I am starting the service, it gets started successfully but it does not launch batch file on my Windows 7.
Contents of Batch files are given below
Start.bat
#echo off
echo I am started
pause
Please let me know what am I missing here
Have you tried following
Runtime.getRuntime().exec("cmd /c start Start.bat");
To execute batch file from java application, try this piece of code:
// "D://bin/" is the location of my .bat
File dir = new File("D:/bin/");
try {
// sign.bat if my actual file.
Runtime.getRuntime().exec("cmd.exe /c sign.bat", null, dir);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}