I am trying to create a disk space from Java code using qemu-img so I instantiated process and runtime classes. When run the program the command is not executed though I used the same mechanism for similar execution and it work fine. So I am wondering whether I miss something here.
StringBuilder commandBuilder = new StringBuilder("qemu-img create -f "+chosenStorageType+" ~/"+storageName+".img "+storageSize+"M");
System.out.println(commandBuilder);
String command = commandBuilder.toString();
System.out.println("this is the correct one: "+command);
System.out.println("Creating the image...");
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec(command);
System.out.println("from process try..catch");
} catch (IOException e1) {
e1.printStackTrace();
System.out.println(e1.getMessage());
}finally{
System.out.println("from finally entry");
process.destroy();
}
the output is as following:
qemu-img create -f raw ~/testSPACE.img 23.0M
this is the correct one: /qemu-img create -f raw ~/testSPACE.img 23.0M
Creating the image...
from process try..catch
from finally entry
But if I go to the directory the file isn't created.
Just to test it if I copy the output into terminal everything works like a charm.
~is being expanded properly? Try putting a full absolute path in there and see if it works. Sometimes that kind of expansion behaviour is provided by the shell and is therefore not going to work for some command-invocation tasks.