permaculture playing cards*
The moose likes Java in General and the fly likes Problems while executing unix script from Java... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problems while executing unix script from Java..." Watch "Problems while executing unix script from Java..." New topic
Author

Problems while executing unix script from Java...

Alexandre Folgueras
Ranch Hand

Joined: Jan 22, 2005
Posts: 38
Hi there, I hope I'm in the right forum. I'm having trouble executing a unix script from a java program. The script receives multiple parameters and it seems that one of them ( or more ) is not well interpreted. When I run the script directly from a shell, the script executes without any errors. But, when I run the exact same script with the exact same parameters, it does not work.

Here is the command to execute the script with all the parameters that is executed from the shell ( with success ) and also from the java program ( with errors ):
/z/opus/dcap/intfu001.csh 'NODEBUG^NOTKPROF^NOSTACK' opus dcap JACQUES JACQUES '2625781' 'ORA$PIPE$001D00D70001' JOB^DEBUG^SINISIMU^STATPROD^DECTIDANC^81^503^4080^86^87^151^97^98^99 BUT^N^N^SPRI^0^13^24^3^J^05^N^19^19^19 >> /tmp/switcher8.999.null.pgm.test

And here is my portion of code that executes the script:
...
Runtime wShell = Runtime.getRuntime();
Process wProc = wShell.exec(cmd_unix_final);
wProc.waitFor();

return wProc.exitValue();
}


Please give me your thoughts on this!
Thanks in advance!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you please post the error message you get when you try to do this in Java? Also what is cmd_unix_final? I would guess that it is a String, but what is its value?

These details should help us to be able to diagnose your problem more accurately.

Layne


Java API Documentation
The Java Tutorial
Alexandre Folgueras
Ranch Hand

Joined: Jan 22, 2005
Posts: 38
Thank you for your reply.

First of all, I dont get any error messages... All I get is the exitValue from the process. Success should return 0 but I always get 1 or 2.

cmd_unix_final is in deed a string and it contains the following:
/z/opus/dcap/intfu001.csh 'NODEBUG^NOTKPROF^NOSTACK' opus dcap JACQUES JACQUES '2625781' 'ORA$PIPE$001D00D70001' JOB^DEBUG^SINISIMU^STATPROD^DECTIDANC^81^503^4080^86^87^151^97^98^99 BUT^N^N^SPRI^0^13^24^3^J^05^N^19^19^19 >> /tmp/switcher8.999.null.pgm.test

I also tried eliminating the redirection but it does not do any goog, I still get an error.

Thanks a lot!
Alexandre Folgueras
Ranch Hand

Joined: Jan 22, 2005
Posts: 38
Forgot to mention that the script I'm trying to run is written in csh...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The problem is that the command line you're trying to run is bad. The redirection part (>> filename) is being passed to the script as arguments, as are the quotes around the arguments. Runtime.exec() is doing an exec() system call -- it's not invoking a shell and passing the command line to the shell to interpret, as you're assuming.

The quickest way to make some progress here would be to manually execute a shell, and pass your command line to it as an argument, like



[Jess in Action][AskingGoodQuestions]
Alexandre Folgueras
Ranch Hand

Joined: Jan 22, 2005
Posts: 38
Thank you for your response. I tried the code you suggested me. I'm still getting errors but they are different this time...

These are the errors I am getting ( not sure if it helps printing them here ):
/bin/sh: DEBUG: not found
/bin/sh: SINISIMU: not found
/bin/sh: STATPROD: not found
/bin/sh: DECTIDANC: not found
/bin/sh: 81: not found
/bin/sh: 503: not found
/bin/sh: 4080: not found
/bin/sh: 86: not found
/bin/sh: 87: not found
/bin/sh: 151: not found
/bin/sh: 97: not found
/bin/sh: 98: not found
/bin/sh: 99: not found
/bin/sh: N: not found
/bin/sh: N: not found
/bin/sh: SPRI: not found
/bin/sh: 0: not found
/bin/sh: 13: not found
/bin/sh: 19: not found
/bin/sh: 24: not found
/bin/sh: 3: not found
/bin/sh: J: not found
/bin/sh: 05: not found
/bin/sh: N: not found
/bin/sh: 19: not found
/bin/sh: 19: not found

Is it possible that I am getting these errors because I'm using sh in my command line and that the script is written in csh??? If so, must I use csh while executing the script from java? If so, is there an equivalent to -c, because if there is I cant seem to find it in the documentation.

Thank you very much for your help, it is greatly appreciated!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It looks like it's just the long string of tokens separated by ^ that's causing problems; could you just quote that argument in the command line?
Alexandre Folgueras
Ranch Hand

Joined: Jan 22, 2005
Posts: 38
I tried quoting the arguments and it did not work either...

But is it ok to run a csh script using sh like your first post suggested?

Really appreciate your help by the way!
Alexandre Folgueras
Ranch Hand

Joined: Jan 22, 2005
Posts: 38
Just to let you know... I made a few changes to my code and got and something interesting happened... Here is the change I made ( just declared my process differently ):
Process wProc = wShell.exec("/bin/csh "+cmd_unix_final);

Meanwhile, I've modified my script ( intfu001.csh ) so that it would print all the arguments it receives...

I caught the output of my script the following way:

BufferedReader err = new BufferedReader( new InputStreamReader(wProc.getInputStream()));
String line = null;

while ((line = err.readLine()) != null)
System.out.println("OUTPUT: "+line);


To my surprise, the arguments are ok. Even the characters '^' printed fine. But what was an even bigger surprise is that the 'normal' output also printed ( the normal output is what I get when the script runs successfully, the script first prints a few things and then does stuff that takes at least 2 or 3 seconds ).

So is it possible that my return code is sent before the script finishes his job? I did use waitFor() so it shouldn't...

Anyhow, here is my complete code, please let me know what you think:

...
Runtime wShell = Runtime.getRuntime();
Process wProc = wShell.exec("/bin/csh "+cmd_unix_final);
wProc.waitFor();

BufferedReader err = new BufferedReader( new InputStreamReader(wProc.getInputStream()));
String lineErr = null;

while ((lineErr = err.readLine()) != null)
System.out.println("OUTPUT: "+lineErr);


return wProc.exitValue();
}
Alexandre Folgueras
Ranch Hand

Joined: Jan 22, 2005
Posts: 38
Turns out that the problem was only that the command line was bad because of the special characters. Works well now.

Thank you very much for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems while executing unix script from Java...