aspose file tools*
The moose likes Java in General and the fly likes Invoking a .bat file within my program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Invoking a .bat file within my program" Watch "Invoking a .bat file within my program" New topic
Author

Invoking a .bat file within my program

Frank Hanner
Greenhorn

Joined: Jan 11, 2013
Posts: 4
Hey guys,

I'm developing a GUI, and when a certain button is pushed, a .bat file is invoked that performs several commands that manipulate a directory (copy, move, etc). When I invoke the .bat and an error occurs somewhere within the .bat script, my Java program hangs even though the .bat process is finish. But when I invoke the .bat file and no errors occur from within the script, all is dandy and my Java program carries on. Nothing in the my Java program relies on the manipulation done from the .bat script, so I don't see why it hangs.

Here is my code



Sure I could do away with the .bat file and do it all primarily in Java, but I'm interested in why this occurs for my own sanity.

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18999
    
  40

Frank Hanner wrote:Hey guys,

I'm developing a GUI, and when a certain button is pushed, a .bat file is invoked that performs several commands that manipulate a directory (copy, move, etc). When I invoke the .bat and an error occurs somewhere within the .bat script, my Java program hangs even though the .bat process is finish. But when I invoke the .bat file and no errors occur from within the script, all is dandy and my Java program carries on. Nothing in the my Java program relies on the manipulation done from the .bat script, so I don't see why it hangs.

Here is my code



Sure I could do away with the .bat file and do it all primarily in Java, but I'm interested in why this occurs for my own sanity.


Perhaps it would be a good idea to worry about the error? What is the Exception / Error that is returned? When you get the output / error stream (from the Process instance), what are the error messages that you get back? Etc.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Frank Hanner
Greenhorn

Joined: Jan 11, 2013
Posts: 4
Henry Wong wrote:

Perhaps it would be a good idea to worry about the error? What is the Exception / Error that is returned? When you get the output / error stream, what are the values that you get back? Etc.

Henry


Isn't that catch statement only executed if the Process has a problem creating? If I put an output statement in the catch clause, nothing ever gets outputted; thus, there isn't an issue with the actual creation of the Process. I'll put in code that retrieves the output/error stream and report back.

Frank
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1084
    
  10

Frank Hanner wrote:When I invoke the .bat and an error occurs somewhere within the .bat script, my Java program hangs even though the .bat process is finish


This is most likely the classic deadlock caused by not handling the process 'stderr' stream. You should study the 4 sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and implement ALL the recommendations. Failure to do so will likely to result in you pulling your hair out by the roots.
Frank Hanner
Greenhorn

Joined: Jan 11, 2013
Posts: 4
Richard Tookey wrote:
Frank Hanner wrote:When I invoke the .bat and an error occurs somewhere within the .bat script, my Java program hangs even though the .bat process is finish


This is most likely the classic deadlock caused by not handling the process 'stderr' stream. You should study the 4 sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and implement ALL the recommendations. Failure to do so will likely to result in you pulling your hair out by the roots.


That's what I was beginning to realize, too.

Why does this happen? It doesn't really make any sense to me. It makes me a little bitter towards Java.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1084
    
  10

Frank Hanner wrote:
Why does this happen? It doesn't really make any sense to me. It makes me a little bitter towards Java.


The Windows or Unix or whatever OS process invoked has finite size stdout and stderr buffers. If you fill either of them then the process is suspended until there is room in the buffer to take more output. More room is made available by reading from stdout or stderr so if you don't read from them then the process suspends forever i.e. a deadlock. This is not peculiar to Java but a function of the OS's exec() C library call that Java invokes.

The solution is simple - just read the streams but follow the threading guidelines of the 'traps' article.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoking a .bat file within my program