Digital Music

12/23/2009

Executing Shell Script From Java Programming



Hey guys, in this time, i want to share something about Java Programming. Shell script is a script written for the shell, or command line interpreter, of an operating system.
Here is an example to show 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();
}
}
}

Happy Trying ..

0 komentar:

Post a Comment