Using Runtime() on UNIX platform

Here I am using a Runtime object to execute a small script file (through the Java Program) using the exec() method
of the Runtime class. I am using the exec command which is available in UNIX. When I run this it tells me that the command exec is not found. However I know it is there and when I try it by hand on the normal command line it works fine but nit through the Java program.

I have used the Runtime() before to do things like
  "cd DirNAme" - to change directories
and
  "cp  thisfile thatfile"
 to copy a files to another place etc. they worked and works just fine. But do not understand why this. I have also tried using the "source <scriptfilename>" through the Java Runtime() object. But it's the same problem.

Any expert out there would like to put his 2 cents?

ps: i am running on a SUN Ultra3500 plat form.


private static void runThis()
  {
    String fileNameToRun = "testScript";


    try{
         Runtime rt =  Runtime.getRuntime();
         p = rt.exec("exec " + fileNameToRun);

    }
    catch (Exception e) {System.out.println(e.getMessage());}
  }
prainAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim CakalicSenior Developer/ArchitectCommented:
Unless I've forgotten more Unix than I realized, exec is a builtin command of the shell. So when you try exec from the command line (your shell) it works fine because the shell knows what to do. But when you try to 'exec' exec you cannot because there is no executable in your PATH with that name. The answer is typically to 'exec' the shell and then supply exec as part of the command line. In other words:

    try{
        Runtime rt = Runtime.getRuntime();
        p = rt.exec("sh exec " + fileNameToRun);
    } catch (Exception e) {
        e.printStackTrace();
    }

You might have to play with this because there are likely additional shell arguments and quoting that you'll have to do to get it run correctly. For example, to get bash to run correctly on my system to invoke cat on a local file, this is the incantation I had to use:

    public static void main(String[] args) throws Exception {
        Process proc = Runtime.getRuntime().exec("bash -c \"exec cat " + args[0] + "\"");
        InputStream pin = proc.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(pin));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }

Best regards,
Jim Cakalic
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prainAuthor Commented:
Jim:
I am really thankful for your input.

Well, I tried it. But this time what I can see is that the
exception I was receiving previously is gone. I can see it is passing the call to the exec method in the Runtime object. But it is not really executing it.

This is what I did. It's very simple.
I have a script file named testme.scr within which I have put a simple echo statement like this:
 
     echo "Reading A Script File"

Then I do this:

private static void runThis()
 {
   String fileNameToRun = "testme.scr";


   try{
        Runtime rt =  Runtime.getRuntime();
        p = rt.exec("sh source " + fileNameToRun);

   }
   catch (Exception e) {System.out.println(e.getMessage());}
 }

 **** No Luck

Further I changed the mode of the file testme.scr by using the chmod (by hand, and I can see its now an executable)

Then I ran this:

private static void runThis()
 {
   String fileNameToRun = "testme.scr";


   try{
        Runtime rt =  Runtime.getRuntime();
        p = rt.exec("sh exec " + fileNameToRun);

   }
   catch (Exception e) {System.out.println(e.getMessage());}
 }

Its the same story. In both cases I do not see the string
   "Reading A Script File"

coming up on the screen.

But when I run the testme.scr by hand on the command line...

  itsMe<25> source tetsme.scr

I get the result. I tried several other ways as you have suggested. But no luck.

Thanks
prain
0
TuvokCommented:
This question appears to have been abandoned. In fact, I will include links to ALL your open questions

below:

Your options are:
1.  Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you

wish to do this, and allow for Expert response.
3.  Ask Community Support to help split points between participating experts, or just comment here with

details and we'll respond with the process.
4.  Delete the question. Again, you must tell the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below, include the question

QID/link.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process:

Click you Member Profile to view your question history and keep them all current with updates as the

collaboration effort continues.
http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp


PLEASE DO NOT AWARD THE POINTS TO ME.  

------------>  EXPERTS:

Please leave any comments regarding this question here on closing recommendations if this item remains

inactive another three days.

Thank you everyone.

Open Qs:
http://www.experts-exchange.com/jsp/qShow.jsp?qid=11862698
http://www.experts-exchange.com/jsp/qShow.jsp?qid=11874078
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20059847
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20138628
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20158216
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20158215
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20167637
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20167641
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20270929


Tuvok
Moderator @ Experts Exchange

P.S.  For year 2000 question, special attention is needed to ensure the first correct response is awarded,

since they are not in the comment date order, but rather in Member ID order.

0
CleanupPingCommented:
prain:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
Jim CakalicSenior Developer/ArchitectCommented:
Recommendation: points to jim_cakalic (for a partial answer) OR delete and refund points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.