【问题标题】:Is there anyway to give cmd prompt command in java无论如何在java中给出cmd提示命令
【发布时间】:2020-10-03 07:11:15
【问题描述】:

我正在尝试使用 java 运行 cmd 提示命令,我有一个源路径和目标路径,并且我正在翻转图像。翻转的图像应该被复制到目的地。图像被复制,但不是翻转的图像。我将在下面分享我的代码,如果需要其他任何内容,请告诉我。

@Test
public void executeCommandJpgDifferentPaths() throws IOException{
    Path resourceDirectory = Paths.get("src\\main\\resources\\download.jpg");
    Path destination = Paths.get(demoFolder.getAbsolutePath(),"download_output.jpg");
    String command = "magick convert -flip resourceDirectory destination " ;
    Boolean output = flipImplementation.executeCommand(command);
    Files.copy(resourceDirectory,destination);
    Assert.assertTrue(output);
    try {
        Thread.sleep(123456789L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java file-io path


    【解决方案1】:

    我已经尝试过使用'destinationDirectory.resolve'。我们需要使用如下:

     @Test
    public void executeCommandJpgDifferentPaths() throws IOException{
        Path resourceDirectory = Paths.get("src\\main\\resources");
        Path resourcePath = resourceDirectory.resolve("download.jpg");
        Path destinationDirectory = Paths.get(demoFolder.getAbsolutePath());
        Path destinationPath = destinationDirectory.resolve("download_output.jpg");
        String command = "magick convert -flip " + resourcePath + " " + destinationPath ;
        Boolean output = flipImplementation.executeCommand(command);
        Assert.assertTrue(output);
        try {
            Thread.sleep(123456789L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      【解决方案2】:

      您使用的是纯字符串。但是你应该在里面使用变量。

      替换

      String command = "magick convert -flip resourceDirectory destination " ;
      

      String command = "magick convert -flip " + resourceDirectory.getAbsolutePath() + " " + destination.getAbsolutePath();
      

      【讨论】:

      • 感谢您的回复,当我使用 resourceDirectory.getAbsolutePath() 时,它要求我重命名引用。我尝试使用“+ resourceDirectory”,它引发了如下异常
      • C:\Users\chitr\AppData\Local\Temp\junit8466679054766850042\ImageMagick\output.jpg java.nio.file.FileAlreadyExistsException: C:\Users\chitr\AppData\Local\Temp\junit8466679054766850042 \ImageMagick\output.jpg 在 sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:124) 在 sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:278) 在 java.nio.file.Files.copy (Files.java:1274)“文件实际上不在该位置”
      猜你喜欢
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多