Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

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());}
  }
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

ASKER

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
Avatar of Tuvok
Tuvok

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.
https://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.
https://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:
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11862698
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11874078
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20059847
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20138628
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20158216
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20158215
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20167637
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20167641
https://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.

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.
Recommendation: points to jim_cakalic (for a partial answer) OR delete and refund points.