Runtime.exec on argument containing multiple spaces

前端 未结 8 869
闹比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.

提交回复
热议问题