Runtime.exec with an argument containing multiple spaces

Can someone do the next run?

public class ExecTest { public static void main(String[] args) { try { //Notice the multiple spaces in the argument String[] cmd = {"explorer.exe", "/select,\"C:\\New Folder\\file.txt\""}; //btw this works //String cmd = "explorer.exe /select,\"C:\\New Folder\\file.txt\""; //and surprisingly this doesn't work //String[] cmd = {"explorer.exe", "/select,\"C:\\New Folder\\file.txt\""}; //Update: and (as crazy as it seems) the following also worked //String[] cmd = {"explorer.exe", "/select,\"C:\\New", "Folder\\file.txt\""}; Runtime.getRuntime().exec(cmd); } catch (Exception e) { e.printStackTrace(); } } } 

Using Java 6. Tested under Vista x64. By the way, taking the string that will be executed (you will have to use the version of String exec to get it), and using it in the Search field in the Vista launch menu will execute as expected. Any help would be appreciated. I'm going crazy..!

Update: I added a solution for the 2nd strange that my post indicates that 2 versions of exec behave differently. The decision is based on the cutout response. Thnx again.

+6
source share
8 answers

Well, this is not just an update, but an answer, so I submit it as one. From all the information I could find, theoretically this should do the following:

String [] cmd = {"explorer.exe", "/ select, \" C: \ New "," "," "," "," "," "," "," Folder \ file. TXT \ ""};

Several spaces were broken into blank lines and the version of the exec array is used. Using the array above, I debugged the loop in lines 50-75 from java.lang.ProcessImpl, where the line is finally built. Summary line:

explorer.exe / select, "C: \ New Folder \ file.txt"

This is what is passed as the first argument to the ProcessImpl native create method (line 118 of the same class), which, as it seems, fails to execute this command correctly .

So, I think it ends here ... unfortunately.

Thnx notch for indicating java error. Thnx each for his time and interest!

+6
source

Always use Runtime.exec (String []), not Runtime.exec (String), unless the command line is very simple.

+5
source

Miracle, it works!

Don’t ask me why, but when I, after quite a long study of nerve damage on the Internet, was close to giving up and using a temporary batch file as a workaround, I forgot to add / select, a parameter to the command, and who could think in my win 7 32bit system the following works.

 String param = "\"C:\\Users\\ME\\AppData\\Local\\Microsoft\\Windows\\Temporary Internet Files\\\""; try { String[]commands = new String[]{"explorer.exe", param}; Process child = Runtime.getRuntime().exec(commands); } catch (IOException e1) { System.out.println("..."); } 

Common decision:

The bug database solution mentioned in his post ( http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6511002 ) did a great job with me.

Cause:

Apparently, the problem is commenting out some of the characters executed by java that it executes before actually executing the command line. You have to do the commenting yourself, giving the token command line to prevent java-1 from being corrupted before spring and ruin everything.

How to fix:

So, in my case, I had to do the following (tokenize my command line so that there are no spaces in the line):

 String param[] = { "explorer.exe", "/select,C:\\Users\\ME\\AppData\\Local\\Microsoft\\Windows\\Temporary", "Internet", "Files\\"}; try { Process child = Runtime.getRuntime().exec(param); } catch (IOException e1) { System.out.println("..."); } 

As you can see, I basically started a new line, wherever the space takes place, so the “Temporary Internet files” have become “temporary”, “Internet”, “files”.

+5
source

First use new File(pathName).canExecute() to check if it is executable or not

EDIT:

 public static void runAll(String... cmd) { for(String s : cmd) { try { Runtime.getRuntime().exec(cmd); } catch(Exception e) { e.printStackTrace(); } } } 

and then you can use it like: runAll("explorer.exe", "taskmgr.exe");

+2
source

May be a Java error. See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6511002

I did a bit of debugging out of curiosity, I think things are sticking up in java.lang.ProcessImpl (see the constructor). Noticed that when he actually received a call to the basic Windows API, the string turned into

explorer.exe "/ select," c: \ New Folder \ test.txt ""

Thus, this may explain why, as workarounds, see the error database link.

+1
source

Symbols ,-& and double spaces, all together - it's a nightmare!

All of the answers given here did not work for "\\NAS\media\Music\Artistes\E\Earth, Wind & Fire\1992 - The eternal dance - Vol. 1 (1971-1975) (double space between" 1 and 1 "( 1971).

I have no choice but to write a temporary batch file:

  void openFolderOf( Album album ) { try { final String path = album._playList.getParent(); final File batch = File.createTempFile( getClass().getSimpleName(), ".bat" ); try( PrintStream ps = new PrintStream( batch )) { ps.println( "explorer.exe \"" + path + '"' ); } Runtime.getRuntime().exec( batch.getAbsolutePath()); } catch( final Throwable t ) { t.printStackTrace(); } } 

Note: on cmd.exe, the explorer "\\NAS..." line explorer "\\NAS..." works well, but not with Runtime.exec () and ProcessBuilder.

+1
source

An easy way to solve this problem for files is java.awt.Desktop Starting with version 1.6 Example:

  Desktop.getDesktop().open(new File(fullFileName)); 
0
source

For your specific case, when the show / select command is required, I adore the Windows quotes nightmare using the cmd / c start command:

 String[] cmd = {"cmd", "/c", "start explorer.exe /select," + path}; 

Where path is the absolute path from the File object.

0
source

Source: https://habr.com/ru/post/892717/


All Articles