1

I've done many research for executing an external program (e.g. iTunes) by some simple code, however the suggestions did never work. Sometimes nothing happend, sometimes I got this error message: English: Unable to find "Discord". Be sure the name is written correctly and try again.

My Code is the following:

try {
    String name = (String) "start " + table.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
    ProcessBuilder p = new ProcessBuilder("cmd.exe", "cd /D %HOMEDRIVE%%HOMEPATH%/Desktop", "/c", name);
    p.start();
} catch (Exception e) {
    e.printStackTrace();
}

In my example I get the name of the external program from a JTable, this part is working fine. The ProcessBuilder is changing the directory to the desktop first. Then the external program should be executed by the start <program name> command. With this code I get the mentioned error message.

If you have a solution with cmd, please include changing the directory to the desktop.

1

2 Answers 2

1

You should pass each argument as a single entry to ProcessBuilder. In your current code, you sometimes take multiple arguments together (like cd /D %HOME...). Try passing every argument as it's own parameter, including the command to start and its argument:

String name = (String) table.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
ProcessBuilder p = new ProcessBuilder("cmd.exe", "cd", "/D", "%HOMEDRIVE%%HOMEPATH%/Desktop", "/c", "start", name);
Sign up to request clarification or add additional context in comments.

1 Comment

Sadly it does not work, the same error message occurs.
0

According to: cmd.exe,

/D Ignore registry AutoRun commands HKLM | HKCU \Software\Microsoft\Command Processor\AutoRun

Did you mean start.exe /D not cmd.exe /D?

and also they told that

If /C or /K is specified, then the remainder of the command line is processed as an immediate command in the new shell. Multiple commands separated by the command separator '&' or '&&' are accepted if surrounded by quotes.

Did you mean cmd.exe /C "cd %HOMEDRIVE%%HOMEPATH%\Desktop & Discord"?

Comments

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.