DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Oct 2005

    How to run UNIX shell script in JAVA?

    I need to run UNIX script using JAVA.
    Requirement is like that UNIX script will return an integer value and I have to read that integer value in java code and use that integer for further processing.
    I don't have any idea about this. Please help me out of this situation with code example.

    Thanks in advance.

  2. #2
    Join Date
    Oct 2005
    You can run it using exec method of Runtime Object. use this is in your code.

    Runtime r = Runtime.getSystemRuntime();
    r.exec("whatever you want to run");


  3. #3
    Join Date
    Oct 2005
    New Jersey
    Here's a reference you should read as a precaution:
    Happiness is good health and a bad memory.

  4. #4
    Join Date
    Feb 2009

    Question Executing Linux Commands

    I cannot execute Internal Bash commands using runtime.exec()......
    eg. cd, etc....
    Please give me an alternative for this......

  5. #5
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    Quote Originally Posted by mihir123 View Post
    I cannot execute Internal Bash commands using runtime.exec
    What errors are you getting when you try?

  6. #6
    Join Date
    Sep 2004
    Surrey, UK

    I use the following code to execute commands in a Unix shell. It can deal with almost everything you can run at the shell prompt:

    import java.io.InputStream;
    String cmd = "ls -l"; // this is the command to execute in the Unix shell
    // create a process for the shell
    ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
    pb.redirectErrorStream(true); // use this to capture messages sent to stderr
    Process shell = pb.start();
    InputStream shellIn = shell.getInputStream(); // this captures the output from the command
    int shellExitStatus = shell.waitFor(); // wait for the shell to finish and get the return code
    // at this point you can process the output issued by the command
    // for instance, this reads the output and writes it to System.out:
    int c;
    while ((c = shellIn.read()) != -1) {System.out.write(c);}
    // close the stream
    try {shellIn.close();} catch (IOException ignoreMe) {}
    Instead of "bash" you can use a program or script name (replacing "-c" and cmd with the appropriate arguments for the program or script), but then you won't be able to execute commands like ls.

    Mihir123, the code above could even execute a cd command, but on its own it has no effect.

    The command is executed in a new shell so it will affect only that shell instance; it will run without errors, effectively changing the directory, but once executed the shell closes and the directory change is forgotten.

    You should put the cd together with the other commands that need the directory change in a script and run that script instead.

    Alternatively you can redirect stdin of the shell process to an output stream and write commands to this stream. In this case do not use "-c" and cmd.

  7. #7
    Join Date
    Feb 2009

    Java in Linux

    Thanks very much for your code....it really helped me in a great way.

    As you said how can i change a directory using cd and simultaneously run another program or script like find, uname, ls, etc......

    Secondly do you know how can i replace special characters like (",\,',$) etc. in a String in java...........with a space or blank ...........

    Kindly reply as soon as possible plzzzzz.....

  8. #8
    Join Date
    Aug 2012
    i solved it one week ago, but thanks for reply anyway
    p.s i am on linux

  9. #9
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    What did you do? Please post your resolution as it could help others with the same or similiar problem.


Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  2. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 02:57 PM
  3. Has Sun Given Up on the Desktop?
    By Lori Piquet in forum Talk to the Editors
    Replies: 114
    Last Post: 10-10-2002, 06:01 AM
  4. run java app on Linux at boot up
    By Alan Shiers in forum Java
    Replies: 3
    Last Post: 07-16-2002, 11:29 PM
  5. Replies: 1
    Last Post: 09-13-2001, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.