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.
我正在尝试使用qemu-img从Java代码创建一个磁盘空间,以便实例化流程和运行时类。当运行程序时,命令不会被执行,尽管我使用了相同的机制进行类似的执行,并且它工作得很好。所以我想知道我是否漏掉了什么。
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.
为了测试它,如果我把输出复制到终端,一切都像魔法一样。
1 个解决方案
#1
1
The tilde (~
) here
波浪号(~)
" ~/"+storageName+".img "
is interpreted by the shell as meaning your home directory. I would substitute the real path.
被shell解释为您的主目录。我要用实路径代替。
Note also that you need to be consuming the process stdout/err, and collecting the error code of the process (and checking it!) via Process.waitFor()
还要注意,您需要使用进程stdout/err,并通过process . waitfor()收集进程的错误代码(并检查它!)
#1
1
The tilde (~
) here
波浪号(~)
" ~/"+storageName+".img "
is interpreted by the shell as meaning your home directory. I would substitute the real path.
被shell解释为您的主目录。我要用实路径代替。
Note also that you need to be consuming the process stdout/err, and collecting the error code of the process (and checking it!) via Process.waitFor()
还要注意,您需要使用进程stdout/err,并通过process . waitfor()收集进程的错误代码(并检查它!)