有没有办法在Java中使用cmd提示符命令

huangapple go评论89阅读模式
英文:

Is there anyway to give cmd prompt command in java

问题

@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();
}
}

英文:

I'm trying to run cmd prompt command using java, i have a source path and destination path and i'm flippig the image. Flipped image should get copied to destination. The image is getting copied but not the flipped image. I'll share my code below, kindly let me know if anything else is needed.

@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();
    }
}

答案1

得分: 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();
    }
}
英文:

I have tried this using, 'destinationDirectory.resolve'.We need to use as below :

 @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

得分: 0

你正在使用普通字符串。但是你应该在内部使用变量。

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

替换为

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

You are using a plain string. But you should be using variables inside.

Replace

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

with

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

huangapple
  • 本文由 发表于 2020年10月3日 15:11:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64181652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定