Tuesday, December 22, 2009

How to Execute Shell Script From Java

Here is an example shows how to execute the shell scripts from java programs.

If you want to execute the below program set the java path in Unix environment.

export PATH=$PATH:/usr/jdk1.5.0/bin

export CLASSPATH=$CLASSPATH:/usr/jdk1.5.0/bin

create a shell script like shellscript.sh

#!/bin/bash
clear
echo "Good morning, world."

 

then change the permission for this script.if you execute the shell script you need execute permission.

chmod 777 shellscript.sh

try

./shellscript.sh

From java program

import java.io.IOException;

public class JavaScript {

public static void main(String[] args) {

         try {

                Runtime.getRuntime().exec("/root/shellscript.sh");

                System.out.println("Working Fine");

              } catch (IOException e) {

                   e.printStackTrace();

                                        }

           }

}

0 Comments:

Post a Comment

Bookmark and Share
Hihera.com
Increase Page Rank Google
TopBlogDir.blogspot.com button
Best Indian websites ranking
Tips for New Bloggers
TopOfBlogs
The Link Exchange - Your ultimate resource for link exchange!

About This Blog

TopOfBlogs

FEEDJIT Live Traffic Feed

  © Blogger template Webnolia by Ourblogtemplates.com 2009

Back to TOP