Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Problems with threads

Thread view: 
Thomas Nelson - 14 Mar 2006 22:03 GMT
I am trying to execute a sh script file from a Java method.

The key part of the code looks like this:

        Process p = null;

        try {
            p = Runtime.getRuntime().exec("start_team.sh");
        } catch (IOException e) {
            System.out.println(e.toString());
        }

        try {
            (Thread.currentThread()).sleep(30000);
        } catch (Exception e) {
            System.out.println(e.toString());
        }

The script runs fine on its own ("./start_team.sh" at command line),
but when we
execute this code, it freezes up about midway through, while the main
thread is still sleeping.

Does anyone know where to look to fix this code?  Is there something
obvious I'm missing?

Thanks for your time.
Gordon Beaton - 14 Mar 2006 22:13 GMT
> I am trying to execute a sh script file from a Java method.

[...]

> The script runs fine on its own ("./start_team.sh" at command line),
> but when we execute this code, it freezes up about midway through,
> while the main thread is still sleeping.

You need to either read the output of the child process as it runs, or
prevent it from producing any output (for example, with redirection).

BTW Thread.sleep() is a static method, so calling sleep() with
currentThread() is bad style (and unnecessary).

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Oliver Wong - 14 Mar 2006 23:46 GMT
>I am trying to execute a sh script file from a Java method.
>
[quoted text clipped - 21 lines]
> Does anyone know where to look to fix this code?  Is there something
> obvious I'm missing?

   See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html

<quote>
Because some native platforms only provide limited buffer size for standard
input and output streams, failure to promptly write the input stream or read
the output stream of the subprocess may cause the subprocess to block, and
even deadlock.
</quote>

   - Oliver
Thomas Nelson - 15 Mar 2006 01:11 GMT
Thanks to you both, I read the input stream into a buffer, and piped
the error stream away to /dev/null, and now the code seems to work.

THN


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.