Runtime.exec on argument containing multiple spaces

前端 未结 8 809
闹比i
闹比i 2020-11-30 15:15

Can anyone make the following run?

public class ExecTest {
  public static void main(String[] args) {
    try {
      //Notice the multiple spaces in the arg         


        
相关标签:
8条回答
  • 2020-11-30 15:29

    The characters ,-& and double spaces, all combined are a nightmare!

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

    I have no other choice than writing 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 line explorer "\\NAS..." works well but not with Runtime.exec() nor ProcessBuilder.

    0 讨论(0)
  • 2020-11-30 15:29

    For your specific case of needing the reveal/select command, I get around the windows quote nightmare by using cmd /c start:

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

    Where path is the absolute path from a File object.

    0 讨论(0)
  • 2020-11-30 15:33

    Use new File(pathName).canExecute() first to check whether it's 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");

    0 讨论(0)
  • 2020-11-30 15:37

    Ok, this is not simply an update but also an answer so I'm filing it as one. According to all information I could find, the following should theoretically do it:

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

    The multiple spaces have been broken into empty strings and the array version of exec is used. Using the above array, I debugged the loop in lines 50-75 of java.lang.ProcessImpl where a string is finally constructed. The resulting string was:

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

    This is what is passed as the 1st argument to ProcessImpl's native create method (line 118 same class), which as it seems fails to run properly this command.

    So I guess it all ends here... sadly.

    Thnx prunge for pointing out the java bug. Thnx everyone for their time and interest!

    0 讨论(0)
  • 2020-11-30 15:45

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

    0 讨论(0)
  • 2020-11-30 15:48

    A miracle, it works!

    Don't ask me why, but when i, after quite a while of nerve-wrecking research in the internets, was close to give up and use a temporary batch file as a workaround, i forgot to add the /select, parameter to the command, and, who would have thought, the following works on my Win 7 32Bit System.

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

    General Solution:

    The solution of the bug-database mentioned by prunge in his post (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6511002) worked fine for me.

    Reason:

    Apparently the problem lies with the commenting of some characters done by java which it does before actually executing the command string. You have to do the commenting yourself by tokenizing your command string, to prevent the faulty java one to spring into action and mess everything up.

    How to fix:

    So, in my case i had to do the following (tokenizing my command string, so that no spaces are left inside the string):

    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 String wherever a space occured, so "Temporary Internet Files" became "Temporary","Internet","Files".

    0 讨论(0)
提交回复
热议问题