This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


I think the two marked lines need to be made conditional on elts[i]
containing whitespace? (whitespace in this case is any of \t, \n, \r, \f -
this is taken from the parameterless constructor of StringTokenizer
http://java.sun.com/j2se/1.4.1/docs/api/java/util/StringTokenizer.html )

I did a little experiment with trying to get the *actual* command line passed to a program by Sun's JRE's Runtime.exec( ) using the Win32 GetCommandLine( ) function rather than the C runtime's argv array and it more or less confirms your hypothesis:

  The elements of the command line array are quoted *only if*
  there's a space or a tab (\n, \r and \f are *not* "escaped").

(The MSVC runtime library seems to do its own command
line quotes processing that initially threw me off track.)


I'd try to send a fix for this but don't know C++ :(

One simple way to do this would be to remove the auto-quoting bits from the C++ file (natWin32Process.cc) and modify the Java code (Win32Process.java) something like this:

  public ConcreteProcess (String[] progarray,
                          String[] envp,
                          File dir)
    throws IOException
  {
    for (int i = 0; i < progarray.length; i++)
      if ((progarray[i].indexOf (' ') >= 0)
        || (progarray[i].indexOf ('\t') >= 0))
        {
          progarray[i] = "\"" + progarray[i] + "\"";
        }

    startProcess (progarray, envp, dir);
  }

That shouldn't require too much of C++ skills, should it now? ;-)

BTW, I'm attaching a small Java program and a small C
program (for Win32) that demonstrates this distinction
that Sun's JRE makes.

Ranjit.

--
Ranjit Mathew          Email: rmathew AT hotmail DOT com

Bangalore, INDIA. Web: http://ranjitmathew.tripod.com/
import java.io.*;

public class First 
{
  public static void main(String arg[]) throws Exception
  {
    String[] cmdStr = { 
      "foo", "spa ce", "newline\n", "tab\t", "linefeed\r", "formfeed\f"
    };

    Process proc 
      = Runtime.getRuntime( ).exec( cmdStr);

    InputStream is = proc.getInputStream( );

    int inCh = is.read( );
    while( inCh != -1)
    {
      System.out.print( (char )inCh);
      inCh = is.read( );
    }
  }
}
#include <stdio.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>


int main( int argc, char* argv[])
{
   int i;
   for( i = 0; i < argc; i++)
   {
     printf( "%s\n", argv[i]);
   }

   MessageBox( NULL, GetCommandLine( ), "MESSAGE", MB_OK);
   return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]