Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. thread does not return from Runtime.exec

thread does not return from Runtime.exec

Scheduled Pinned Locked Moved Java
javaquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WernerP
    wrote on last edited by
    #1

    Hi, I'm new to using Runtime.getRuntime().exec on WindowsXP, JRE 6. Solution should work on all Windows NT from 5 upwards and common UNIX. I called it in the following way process = Runtime.getRuntime().exec(cmdargs); where cmdargs[0] = "D:\Somepath\NmMutex" cmdargs[1] = "lock" Program NmMutex with argument lock runs until its process is stopped externally. When Runtime.getRuntime().exec(cmdargs); is called, the calling thread remains suspended, until program NmMutex is killed. Is that the normal behaviour of Runtime.exec? I thought the calling thread would return, even if the program it started is still running, so I can observe the prog by reading its stdout and stderr from my Java app. If not it would return there would be no process created and I could not observe the program. Which way is best to solve that? Start prog in Background, like process = Runtime.getRuntime().exec("start /B NmMutex lock"); resp. process = Runtime.getRuntime().exec("& NmMutex lock"); ? Thank you Werner

    P 1 Reply Last reply
    0
    • W WernerP

      Hi, I'm new to using Runtime.getRuntime().exec on WindowsXP, JRE 6. Solution should work on all Windows NT from 5 upwards and common UNIX. I called it in the following way process = Runtime.getRuntime().exec(cmdargs); where cmdargs[0] = "D:\Somepath\NmMutex" cmdargs[1] = "lock" Program NmMutex with argument lock runs until its process is stopped externally. When Runtime.getRuntime().exec(cmdargs); is called, the calling thread remains suspended, until program NmMutex is killed. Is that the normal behaviour of Runtime.exec? I thought the calling thread would return, even if the program it started is still running, so I can observe the prog by reading its stdout and stderr from my Java app. If not it would return there would be no process created and I could not observe the program. Which way is best to solve that? Start prog in Background, like process = Runtime.getRuntime().exec("start /B NmMutex lock"); resp. process = Runtime.getRuntime().exec("& NmMutex lock"); ? Thank you Werner

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      It looks like Runtime.exec() is blocking on the process it spawned (like the old Unix exec()?). I can think of two portable solutions: 1. start a new Thread to do the exec() and let it block. 2. use ProcessBuilder.start()[^] This article[^] discusses the use of ProcessBuilder and Runtime. Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994.

      W 1 Reply Last reply
      0
      • P Peter_in_2780

        It looks like Runtime.exec() is blocking on the process it spawned (like the old Unix exec()?). I can think of two portable solutions: 1. start a new Thread to do the exec() and let it block. 2. use ProcessBuilder.start()[^] This article[^] discusses the use of ProcessBuilder and Runtime. Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994.

        W Offline
        W Offline
        WernerP
        wrote on last edited by
        #3

        It looks like that, although I'm much surprised, because this is not what Runtime.exec should do, when I read into the docs - ehm - assumed I understood them also. 1 might work, but you don't get the process handle, because calling thread is suspended before return from call 2 yes right, I think its worth trying because it seems to be more convenient thank you Werner

        P 1 Reply Last reply
        0
        • W WernerP

          It looks like that, although I'm much surprised, because this is not what Runtime.exec should do, when I read into the docs - ehm - assumed I understood them also. 1 might work, but you don't get the process handle, because calling thread is suspended before return from call 2 yes right, I think its worth trying because it seems to be more convenient thank you Werner

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          I couldn't see anywhere the blocking/non-blocking behaviour of exec() was specified. I think I read somewhere that ProcessBuilder is the preferred technology since Java 5. Certainly looks a lot more usable, although I haven't used either in anger. Cheers, Peter

          Software rusts. Simon Stephenson, ca 1994.

          W 2 Replies Last reply
          0
          • P Peter_in_2780

            I couldn't see anywhere the blocking/non-blocking behaviour of exec() was specified. I think I read somewhere that ProcessBuilder is the preferred technology since Java 5. Certainly looks a lot more usable, although I haven't used either in anger. Cheers, Peter

            Software rusts. Simon Stephenson, ca 1994.

            W Offline
            W Offline
            WernerP
            wrote on last edited by
            #5

            Hi, I tried the same with process builder

                ProcessBuilder pbuilder = new ProcessBuilder(cmdlist);
            
                process = pbuilder.start();
                procmon = new ProcessMonitor(process);
            

            where cmdlist[0] = "D:/MyPath/NmMutex" cmdlist[1] = "lock" (btw. this doesn't mean the main thread waits at lock, its try_lock) The thread returns as soon as the process, which was called ended. I tested it with a loop whithin the external process. If it is finite, the thread returns. If it is infinite the thread doesn't return. I.e. exec and start are blocking. The idea to call ProcessBuilder.start within a separate process is basically good, but I woudn't receive a process handle when start would not return. And without that, I would not be able to attach streams to the stdout and stderr. Hence the process might block as soon as the stdout and stderr buffers are filled up. If I started a background shell and within that my process, the solution would be os dependend (not KISS, complicated, not that good). Thanks Werner

            1 Reply Last reply
            0
            • P Peter_in_2780

              I couldn't see anywhere the blocking/non-blocking behaviour of exec() was specified. I think I read somewhere that ProcessBuilder is the preferred technology since Java 5. Certainly looks a lot more usable, although I haven't used either in anger. Cheers, Peter

              Software rusts. Simon Stephenson, ca 1994.

              W Offline
              W Offline
              WernerP
              wrote on last edited by
              #6

              Hi, the process started by ProcessBuilder and by Runtime.exec - which is implemented via ProcessBuilder - is started asynchronously when the java app is started from jar. The effect of the frozen thread happened exclusively within the debbuger session. I should have tried. Sorry and thanks again Werner

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups