problems with exec("rundll32.exe shell32.dll...")

Andrew.Ferguson@arm.com Andrew.Ferguson@arm.com
Tue May 20 14:39:00 GMT 2003


>However, we had a good reason for doing this:
>
>http://gcc.gnu.org/ml/java-patches/2003-q1/msg00502.html
>
>(Also see the messages following it).
>
>I do not know why this behaviour changes for these
>programs - a simple test program in C that I wrote
>that merely prints out argv[] never manages to
>see the difference between quoted and unquoted
>command lines. :-(

hi Ranjit,

>The problem that you are reporting as well as that
>reported by Oywind can be traced to the fact that
>we put a double quote (") around every argument
>on a command line before calling Win32 CreateProcess( )

 I think this might not be the right thing to do?

Is the difference that

      exec(String s)

            parses s into an array of arguments using whitespace as the
delimiter* so

                  cmd /C dir c:\program files

            would be parsed (correctly) to
{"cmd","/C","dir","c:\program","files"}, and then make the dir fail?

If the user in java wants to execute this they should do this:

      exec(new String [] {"cmd","/C","dir","c:\\program files"});

(demonstrated in code included below)

Once you have the String [], I think only the entries containing whitespace
need to be quoted - this should then bring the behaviour of the JRE + gcj
together.

thanks,
Andrew

======================================
*from the API
Executes the specified string command in a separate process.
      The command argument is parsed into tokens and then executed as a
      command in a separate process. The token parsing is done by a
      StringTokenizer created by the call:
             new StringTokenizer(command)

      with no further modifications of the character categories. This
      method has exactly the same effect as exec(command, null).

======================================

import java.io.*;
import java.util.*;

public class GCJTest2 {
   public static void main(String [] arg) throws Exception {
      System.out.println("Using String[] gives ");
      Process a = Runtime.getRuntime().exec(new String []
{"cmd","/C","dir","C:/Program Files"});
      new Thread(new Threader(a.getInputStream(),"(out)")).start();
      new Thread(new Threader(a.getErrorStream(),"(err)")).start();
      a.waitFor();
      System.out.println("String[] exit value: "+a.exitValue());

      System.out.println("\n\nUsing String gives ");
      Process b = Runtime.getRuntime().exec("cmd /C dir C:/Program Files");
      new Thread(new Threader(b.getInputStream(),"(out)")).start();
      new Thread(new Threader(b.getErrorStream(),"(err)")).start();
      b.waitFor();
      System.out.println("String exit value: "+b.exitValue());
   }

   static class Threader implements Runnable {
      BufferedReader br;
      String ss;

      Threader(InputStream is, String s) {
         ss=s;
         br = new BufferedReader(new InputStreamReader(is));
      }

      public void run() {
         try{
            for(String line; (line=br.readLine())!=null; ){
               System.out.println("\t "+ss+" "+line);
            }
         }catch(Exception e){e.printStackTrace();}
      }
   }
}








More information about the Java mailing list