qemu-img를 사용하여 Java 코드에서 디스크 공간을 만들려고하므로 프로세스 및 런타임 클래스를 인스턴스화했습니다. 프로그램을 실행할 때 비슷한 실행을 위해 동일한 메커니즘을 사용했지만 명령은 실행되지 않습니다. 그래서 나는 여기서 무언가를 놓칠 지 궁금합니다.Java 코드에서 Linux 프로그램을 실행하는 방법
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
하지만 디렉토리로 이동하면 파일이 생성되지 않습니다
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();
}
출력은 다음과 같습니다.
터미널에 출력을 복사하면 테스트해도됩니다. 모든 것이 매력처럼 작동합니다. 여기
" ~/"+storageName+".img "
을 통해 당신은 ~가 '적절하게 확장되고있는'확인 할 수 있나요? 거기에 완전한 절대 경로를 두어 작동하는지 확인하십시오. 때로는 그런 종류의 확장 동작이 셸에서 제공되기 때문에 일부 명령 호출 작업을 수행하지 않습니다. – Gian
예 : 절대 경로를 제공하는 중 : StringBuilder commandBuilder = new StringBuilder ("qemu-img create -f"+ selectedStorageType + "/home/dartron/"+storageName+".img"+storageSize+"M"); 똑같은 일을 그냥 출력하지만 명령을 실행하지 않습니다. – user2023107