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();
}
Related
I am trying to run some servers from a bot, but I can't get it run my screen and host the server. I've tried debugging and getting some kind of code to debug, but I come up with nothing. Maybe it's something in my syntax?
These are the value you can run in: name = factorio, path = /opt/factorio/bin/x64/factorio, args = --start-server map.zip
Together makes:
screen -dmS factorio -m bash -c "/opt/factorio/bin/x64/factorio --start-server map.zip"
public boolean run() {
try {
System.out.println("screen -dmS "+serverName+" -m bash -c \""+path+" "+args+"\"");
Process proc = rt.exec("screen -dmS "+serverName+" -m bash -c \""+path+" "+args+"\"");
BufferedReader read = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String str = null;
while ( (str = read.readLine()) != null)
System.out.println(str);
try {
proc.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Exit value: "+proc.exitValue());
this.setStatus(ONLINE);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
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 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
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();
}