• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run .sh file in UNIX using Java class, getting exitVal for waitFor() is 1  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the below code to run my startGWR.sh file. I am able to see the process ID of the current java program. But am getting exitVal is 1, Hence my client is unable to connect. Is this code is correct or How can I read the .sh file to execute successfully.



 
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

you should always use code tags; I have added them for you and doesn't the code look better
I presume you are familar with the content of Michael Daconta's article “When Runtime.exec() won't”; if you aren't, don't go anywhere near exec(). You are not starting threads to empty the Process' input and error Streams, for a start.
There is something wrong about your assigning isStarted before you start, and reassigning it again later. By the way is itSee §10.5.2.
 
Mahesh Dwarapureddi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You should always use code tags; I have added them for you and doesn't the code look better
I presume you are familar with the content of Michael Daconta&apos's article “When Runtime.exec() won't”; if you aren't, don't go anywhere near exec(). You are not starting threads to empty the Process' input and error Streams, for a start.
There is something wrong about your assigning isStarted before you start, and reassigning it again later. By the way is itSee §10.5.2.



Thanks for you response, but this issue am facing is as below.
Am tried in the below ways to run the .sh file
Step1: p = Runtime.getRuntime().exec("sh /upapps/ptc/app/tests/gwr/startGWR.sh");

Step2: p = Runtime.getRuntime().exec("sh ../startGWR.sh");

But am getting the Nullpointer exception and return value for exitVal=1, because it is not reading the sh file or am calling the .sh file in wrong way.
Please help.
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better for you to use ProcessBuilder but your code should do well enough if you

a) change to use

b) you remove that sleep() call. The waitFor() method only returns when the script is complete.

c) you empty the script 'stdout' and 'stderr'' streams in the manner recommended in http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html . You should read the whole article and implement ALL the recommendation. You may think you don't need to but I assure you you do and the traps will jump up and bite you sometime if you don't.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Dwarapureddi wrote:I am using the below code to run my startGWR.sh file.

I'm afraid my advice is slightly different from the others: Ask yourself why you're doing this.

You certainly can do it, and there might be reasons for doing so; but it seems a bit pointless to me to run a Java program to execute an OS-centric script that someone could presumably just execute themselves.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!