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();
}
Related
I have a processbuilder that runs a .sh script. it opens a terminal. I want to destroy this terminal later. I tried process.destroy() but it did not do the job.
Code:
Process p = new ProcessBuilder("/usr/bin/gnome-terminal", "-e", "/home/omar/ros_ws/./baxter2.sh").start();
try {
Thread.sleep(10000); // wait for one second
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
p.destroy();
try
{
Process[] proc = new Process[2];
proc[0] = new ProcessBuilder("calc.exe").start();
proc[1] = new ProcessBuilder("notepad.exe").start();
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
}
proc[0].destroy();
proc[1].destroy();
}
catch (IOException ioe)
{
ioe.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();
}
i am trying to save the logcat of an Android device by executing cmd commands through java,although the file i specify in which the logcat to be saved to is never created.Can anybody tell me why this happens?
the cmd command i want to perform is "adb logcat > C:/Users/user1/Desktop/log1.txt".
and the code I use to execute it is:
try {
// create a new array of 4 strings
String[] cmdArray = new String[4];
cmdArray[0] = "adb";
cmdArray[1] = " logcat";
cmdArray[2] = " >";
cmdArray[3] = " C:/Users/amantsakov/Desktop/logcat.txt";
// create a process and execute cmdArray
Process process = Runtime.getRuntime().exec(cmdArray);
} catch (Exception ex) {
ex.printStackTrace();
}
Hope this will work for you
String[] exeCmd = new String[] { "ffmpeg", "-i", "C:\\test\\Veham.mp3",
"-i", "C:\\test\\test.mp4", "-acodec", "copy", "-vcodec",
"copy", "C:\\test\\outPut.mp4" };
ProcessBuilder pb = new ProcessBuilder(exeCmd);
boolean exeCmdStatus = executeCMD(pb);
return exeCmdStatus;
private static boolean executeCMD(ProcessBuilder pb) {
pb.redirectErrorStream(true);
Process p = null;
try {
p = pb.start();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("oops");
p.destroy();
return false;
}
// wait until the process is done
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("woopsy");
p.destroy();
return false;
}
return true;
}// End function executeCMD
these method will help to run cmd command from java.
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"});
I am executing a self-defined command like following code.I know that append a >nul to command will prevent from outputing error or other message to console.But in my code,>nul is treated as a parameter of my own command deletefile.So the command can not execute as expected.How to resolve it?
Runtime runtime = Runtime.getRuntime();
try {
Process p = runtime.exec("deletefile /name:\"ABC\" /owner:\"Wolfman\" >nul");
int status = p.waitFor();
System.out.println(status);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Solution is:
cmd deletefile /name:\"ABC\" /owner:\"Wolfman\" >nul