Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoking a .bat file within my program

 
Frank Hanner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frank Hanner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic