0
ProcessBuilder pb = new ProcessBuilder(commandInformation);
Process process = pb.start();

Above code is returning error:

java.lang.IllegalArgumentException
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:69)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
    at com.ConvertsImages.SystemCommandExecutor.executeCommand(SystemCommandExecutor.java:51)
    at com.ConvertsImages.ImageConversion.runConvertCommand(ImageConversion.java:115)
    at com.ConvertsImages.ImageConversion.runConvert(ImageConversion.java:80)
    at com.ConvertsImages.ImageConversion.main(ImageConversion.java:26)

List commandInformation has only one entry as below:

["D:\Program Files\ImageMagick-6.8.6-Q16\convert.exe"  "D:\ConvertFiles\ReImport_2507_1.jpg"  -resize 40x40 "D:\ConvertFiles\proxy-40\ReImport_2507_1.jpg.jpg" ]

Please suggest.

3 Answers 3

5

Looking at the source code for ProcessBuilder, there is a method isQuoted that checks if a String argument is quoted and throws IllegalArgumentException if it is and a flag is checked.

private static boolean isQuoted(boolean noQuotesInside, String arg,
        String errorMessage) {
    int lastPos = arg.length() - 1;
    if (lastPos >=1 && arg.charAt(0) == '"' && arg.charAt(lastPos) == '"') {
        // The argument has already been quoted.
        if (noQuotesInside) {
            if (arg.indexOf('"', 1) != lastPos) {
                // There is ["] inside.
                throw new IllegalArgumentException(errorMessage);
            }
        }
        return true;
    }
    if (noQuotesInside) {
        if (arg.indexOf('"') >= 0) {
            // There is ["] inside.
            throw new IllegalArgumentException(errorMessage);
        }
    }
    return false;
}

The above is called from getExecutablePath, note the true flag

 boolean pathIsQuoted = isQuoted(true, path,
            "Executable name has embedded quote, split the arguments");

which is called inside the ProcessImpl constructor

String executablePath = getExecutablePath(cmd[0]);

where cmd is the array created from your list. Index 0 matches the executable (in your case the whole String). In the String you showed us, your executable is quoted (or at least starts with one), so the method will throw an IllegalArgumentException.

This is confirmed by your stack trace

at java.lang.ProcessImpl.<init>(ProcessImpl.java:69)

That means inside your constructor.

Split each argument in your command list as a separate String element in the list. Don't put quotes around the executable.

Sign up to request clarification or add additional context in comments.

Comments

0

I suspect you need a list of 'n' arguments.

It's currently interpreting your first list entry as the executable, and that's clearly not right since it incorporates your arguments too.

1 Comment

Would that throw IllegalArgumentException or command not found?
0

Please make sure, that the parameter passed in ProcessBuilder, commandInformation is a String array or a List<String>.

Parameters: command A string array containing the program and its arguments

Parameters:
command The list containing the program and its arguments

Source

Oracle Docs. which states one of the reasons for IllegalArgumetnException.

Also I just noticed the file name that you have is included with .jpg.jpg . Shouldn't it be fileName.jpg

6 Comments

That's still doesn't answer the question. The OP stated List commandInformation, with the list's toString() being ["D:\Program Files\ImageMagick-6.8.6-Q16\convert.exe" "D:\ConvertFiles\ReImport_2507_1.jpg" -resize 40x40 "D:\ConvertFiles\proxy-40\ReImport_2507_1.jpg.jpg" ].
I'm sorry but how is java.awt.Desktop#open() relevant? The ProcessBuilder uses a native method to create a process using the win32 function CreateProcess.
That was a wrong link, updated it. Also do you think the filename that the OP has is correct? It has filename.jpg.jpg? "D:\ConvertFiles\proxy-40\ReImport_2507_1.jpg.jpg
Regardless of the filename and its existence, the ProcessBuilder#start() method would no throw an IllegalArgumentException for that reason. The underlying process would just fail with its own error message. Go through the source code, it's because of the starting " in his command. Check my answer.
Ok. True. From where did you find that method? I am trying to find it in Oracle docs, but cannot find it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.