【问题标题】:Why my command line instruction doesn't eject the cd?为什么我的命令行指令没有弹出 cd?
【发布时间】:2015-05-11 11:15:18
【问题描述】:

在我使用 ISO Writer 刻录 CD 的程序中,由于 link 我的代码应该在写入后弹出 cd,因为 -e 命令行,它会刻录到 cd 但写入后不会弹出它,我不知道是什么问题?

//Library that use to create iso file
File mkisofs = new File("lib/cdrtools/mkisofs.exe");

//The root of file that we want to write on DVD
File source = new File(new ProductUtil().getProductDir()+ "\\output\\Autorun");

//Destination that the iso file will be save on it.
File destination = source.getParentFile();

//Library that use to write iso file
File isoWriter = new File("lib/isowriter/ISOWriter.exe");


String command = mkisofs.getPath()+" -UDF -o \'"+destination.getAbsolutePath()+"\\cd.iso\' \'"+source.getAbsolutePath()+"\'";
Process createIso = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(createIso.getErrorStream()));

String line = "";
String all = "";

while((line = reader.readLine()) != null) {
    all += line+"\r\n";
}

if(createIso.waitFor() != 0) {
    JOptionPane.showMessageDialog(null, all,"Error on creating ISO file: ("+createIso.waitFor()+")",JOptionPane.ERROR_MESSAGE);
    return null;
}

command = isoWriter.getPath()+" -s 16 -e  \""+destination.getAbsolutePath()+"\\cd.iso\"";
System.out.println(command);
Process writeIso = Runtime.getRuntime().exec(command);

这是我在以这种格式添加驱动器名称时遇到的错误:

command = isoWriter.getPath()+" f: -s 16 -e  \""+destination.getAbsolutePath()+"\\cd.iso\"";

【问题讨论】:

  • 使用 ProcessBuilder,它提供了更好的方式来形成要执行的命令。
  • 我对此一无所知,请您提供更多指导,我该怎么做? @SachinGorade
  • 试试看official documentation,介绍中有例子。
  • 示例:ISOWrite -re: -e C:\Data\image.iso 使用可刻录驱动器E:,完成后弹出。烧录 c:\data\image.iso
  • 我理解这个例子,但为什么我的代码即使使用 -e 也不起作用? @SantoshShinde

标签: java command-line cd-rom cd-burning


【解决方案1】:

您可以使用ProcessBuilder,而不是使用 + 运算符形成命令。

//  Library that use to create iso file
                File mkisofs = new File("lib/cdrtools/mkisofs.exe");
                //  The root of file that we want to write on DVD
                File source = new File(new ProductUtil().getProductDir()+ "\\output\\Autorun");
                //  Destination that the iso file will be save on it.
                File destination = source.getParentFile();
                //  Library that use to write iso file
                File isoWriter = new File("lib/isowriter/ISOWriter.exe");
                String command[] = {mkisofs.getPath(), "-UDF", "-o", destination.getAbsolutePath()+"\\cd.iso", source.getAbsolutePath() };
                 Process createIso = new ProcessBuilder(command).start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(createIso.getErrorStream()));
            String line = "";
            String all = "";
            while((line = reader.readLine()) != null){
                all += line+"\r\n";
            }
            if(createIso.waitFor() != 0){
                JOptionPane.showMessageDialog(null, all,"Error on creating ISO file: ("+createIso.waitFor()+")",JOptionPane.ERROR_MESSAGE);
                return null;
            }
            command = {isoWriter.getPath(), "-s", "16", "E:", "-e",  destination.getAbsolutePath()+"\\cd.iso"};
            System.out.println(command);
            Process writeIso = new ProcessBuilder(command).start();

我对您正在使用的库一无所知,但根据@SantoshShinde,我已将驱动器号添加到参数中。您也可以尝试跳过它以检查它是否有效。

【讨论】:

  • 我在我的问题中使用您的代码或这个代码时添加了错误: command = isoWriter.getPath()+" -s 16 -e \""+destination.getAbsolutePath()+"\ \cd.iso\"";
【解决方案2】:

尝试通过替换代码中的以下代码来实现这一点

            command = isoWriter.getPath()+" -s 16 -e  \""+destination.getAbsolutePath()+"\\cd.iso\"";
            System.out.println(command);
            Process writeIso = Runtime.getRuntime().exec(command); 

由..

         command = isoburn.exe /q D: \""+destination.getAbsolutePath()+"\\cd.iso\"";
         ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
         builder.redirectErrorStream(true);
         Process p = builder.start();

【讨论】:

  • 我将使用您的代码得到的错误添加到我的问题中
  • 你需要在你的命令中添加你的可刻录驱动器是F,command = isoWriter.getPath()+" -s 16 f: -e \""+destination.getAbsolutePath()+"\\ cd.iso\"";
  • 我添加了驱动器名称,但我在问题中添加了一个错误!
  • 请阅读这个,可能对你有用superuser.com/questions/712271/…
猜你喜欢
  • 2021-03-12
  • 2020-06-17
  • 2023-03-23
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多