1 2 Previous Next 19 Replies Latest reply: May 2, 2013 10:30 AM by 1006764 RSS

    Runtime.exec since Java 1.7.0_21

    854719
      Since the last update to JRE 1.7.0_21 the following windows system command
      no longer works with Runtime.exec:

      cmd /c dir /p [Directory-Path] > [File-Path]

      Both '[Directory-Path]' and '[File-Path]' contains no spaces.

      Has anyone an idea how to solve this problem ?
        • 1. Re: Runtime.exec since Java 1.7.0_21
          gimbal2
          Posting the actual code would help a lot to actually make it possible to provide any kind of answer. Use \
           tags when doing so.                                                                                                                                                                                                                                                                        
          • 2. Re: Runtime.exec since Java 1.7.0_21
            854719
            Thank you for your answer !

            The essential code is:
            ...
            String commandLine = "cmd /c dir /p c:\\temp > c:\\temp\\out.txt";
            Process process = Runtime.getRuntime().exec(commandLine);
            int exitStatus = process.waitFor();
            ...
            exitStatus is: 1
            The command error message shows: File not found

            It does not work anymore since the last java update, iit still worked
            for example with 1.7.0_17

            EDIT: code tags fixed. the code tags are {noformat}
            code here
            {noformat}

            Edited by: PhHein on 24.04.2013 13:44

            Edited by: user13779470 on 24.04.2013 04:56
            • 3. Re: Runtime.exec since Java 1.7.0_21
              gimbal2
              I just cannot believe that it actually worked in update 17 or any other version.

              http://www.javaworld.com/jw-12-2000/jw-1229-traps.html

              Pay especially close attention to page 4.
              • 4. Re: Runtime.exec since Java 1.7.0_21
                sabre150
                The single argument version of Runtime.exec(String command) has been flawed for a long time in that it splits the String argument at spaces and then uses the resulting String[] array as the argument for the the Runtime.exec(String[] command). The proper way to do what you want is to use something along the lines of -
                        String[] commandLine = {"cmd", "/c", "dir /p c:\\temp > c:\\temp\\out.txt"};
                        Process process = Runtime.getRuntime().exec(commandLine);
                        new Thread(new SyncPipe(process.getErrorStream(), System.err)).start();
                        new SyncPipe(process.getInputStream(), System.out).run();
                        int exitStatus = process.waitFor();
                        System.out.println("Exit code = " + exitStatus);
                where I have used my class SyncPipe to properly handle the Process stdout and stderr streams correctly according to the 'traps' article cited by 'gimbal2' . This works when your version does not.


                Please please please read the 'traps' article.

                P.S. I don't know whether or not your code would work with earlier versions of the JRE since I no longer have the earlier JDK versions on my Windows XP virtual machine. Even it it did your code is heavily flawed and needs to be fixed.
                • 5. Re: Runtime.exec since Java 1.7.0_21
                  gimbal2
                  sabre150 wrote:
                  correctly according to the 'traps' article cited by 'gymbal' .
                  Oi. I know my nickname is terrible but no need to make it worse, 'cybre'.

                  I still find it mysterious that it apparently did in fact work. Given your excellent description it should have never ever worked.
                  • 6. Re: Runtime.exec since Java 1.7.0_21
                    sabre150
                    gimbal2 wrote:
                    sabre150 wrote:
                    correctly according to the 'traps' article cited by 'gymbal' .
                    Oi. I know my nickname is terrible but no need to make it worse, 'cybre'.
                    :-) I quite like 'cybre'. I think I will change may handle!

                    >
                    I still find it mysterious that it apparently did in fact work. Given your excellent description it should have never ever worked.
                    Ultimately the exec() gets delegated to one of the Windows exec???() 'C' library methods (I can't remember which) and these seem to be poor version of the Unix/Linux implementations. I have seen the silliest commands work on Windows JRE that should not have worked. Fortunately, since I now pretty much exclusively work with *Nix, I usually don't have to live with it!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                    • 7. Re: Runtime.exec since Java 1.7.0_21
                      854719
                      Thank you all for support !!

                      Maybe my code is flawed, but it definitly works in earlier versions of the JRE
                      (for example 1.7.0_17) and it works for example under Windows XP and
                      Windows Server 2008, when i input the command at "Windows-Taskbar - Start - Execute..."
                      It is incomprehensible for me, why Oracle does not wait with such modifications until Java 8,
                      but that's life :-)

                      Bye
                      • 8. Re: Runtime.exec since Java 1.7.0_21
                        gimbal2
                        user13779470 wrote:
                        It is incomprehensible for me, why Oracle does not wait with such modifications until Java 8,
                        What modifications? This code was flawed for Java versions going back since the creation of the Runtime class. If you look at the article, its from 2000. People have been running into this exact Runtime.exec() trap long before the existence of that article.

                        Be glad that in this version of Java you finally know that your code was not correct.
                        • 9. Re: Runtime.exec since Java 1.7.0_21
                          854719
                          There must have been modifications or why does the following code
                          work with Java 1.7.0_17 and earlier Versions and does not work with
                          1.7.0_21 ???
                          String commandLine = "cmd /c dir /p c:\\temp > c:\\temp\\out.txt";
                          try
                          {
                             Process process = Runtime.getRuntime().exec(commandLine);
                             int exitStatus = process.waitFor();
                          }
                          catch(Exception e)  { }
                          Try it for example under Windows XP !!!
                          • 10. Re: Runtime.exec since Java 1.7.0_21
                            rp0428
                            >
                            There must have been modifications or why does the following code
                            work with Java 1.7.0_17 and earlier Versions and does not work with
                            1.7.0_21 ???
                            String commandLine = "cmd /c dir /p c:\\temp > c:\\temp\\out.txt";
                            try
                            {
                               Process process = Runtime.getRuntime().exec(commandLine);
                               int exitStatus = process.waitFor();
                            }
                            catch(Exception e)  { }
                            Try it for example under Windows XP !!!
                            >
                            Why don't YOU try that in a command window and see what happens?

                            This is the command line you used
                            String commandLine = "cmd /c dir /p c:\\temp > c:\\temp\\out.txt";
                            So try this in a command window
                            help dir
                            And one of the lines of output you get should be this
                             /P          Pauses after each screenful of information.
                            You are trying to capture the directory output into a file 'without human intervention' but you use '/P' which requires 'human intervention'.

                            Does that make sense to you? What do you think will happen after that first 'screenful'?
                            • 11. Re: Runtime.exec since Java 1.7.0_21
                              sabre150
                              rp0428 wrote:
                              You are trying to capture the directory output into a file 'without human intervention' but you use '/P' which requires 'human intervention'.
                              Interesting! I had assumed that the OP knew what he was doing from the point of view of the Windows cmd.exe executable and 'dir' so I didn't bother to check the detail of the /P switch.
                              • 12. Re: Runtime.exec since Java 1.7.0_21
                                854719
                                Maybe you should try for example
                                cmd /c dir /p c:\windows > c:\temp\out.txt
                                The command outout is redirected to a file !!
                                I tried it in a command window under Windows XP and it still works and works...
                                without the necessity of human intervention, believe me !!!
                                • 13. Re: Runtime.exec since Java 1.7.0_21
                                  sabre150
                                  user13779470 wrote:
                                  Maybe you should try for example
                                  cmd /c dir /p c:\windows > c:\temp\out.txt
                                  The command outout is redirected to a file !!
                                  I tried it in a command window under Windows XP and it still works and works...
                                  without the necessity of human intervention, believe me !!!
                                  Then why are you using the /P switch at all when you say it has no effect i.e. it does not page the output? It certainly does page and ask for input if you don't re-direct the output to a file because I have just tried it on Windows XP. It does seem that if one re-directs the output to a file then 'dir /p' does not page; what a silly OS Windows is !

                                  This does not change the fact that your code is flawed.
                                  • 14. Re: Runtime.exec since Java 1.7.0_21
                                    854719
                                    Sorry i made a mistake.
                                    In my first post I reduced the code to the essential commands
                                    and took the code from my memory, so /b changed to /p,
                                    i think i'm getting older :-)
                                    Following the protests :-) i tried it again with my posted code
                                    and it also works. so i don't noticed this mistake.
                                    The commandline i really meant is
                                    cmd /c dir /b c:\windows > c:\temp\out.txt
                                    But this does not change the fact, that the commandline,
                                    even if it is flawed, works via
                                    Runtime.getRuntime().exec... etc.
                                    in earlier versions of the JRE etc.

                                    Thank you for your posted solution, it works fine !!!
                                    1 2 Previous Next