We made a .bat file by using the following code
#!/bin/sh
/home/burhan/hbase-0.94.15/bin/start-hbase.sh
/home/burhan/hbase-0.94.15/bin/hbase shell
The above code is run successfully by using the terminal and HBase is started. But when we run the bat file by using java in eclipse its not work. Why?
We write following code in java in eclipse:
Process p;
p=Runtime.getruntime().exec("sh -c /home/burhan/bat.sh");
You are calling exec(String command) which is a convenience method.
That (
exec(String command,String[] envp, File dir)
) in turn behaves in exactly the same way as the invocationexec(String[] cmdarray, String[] envp,File dir)
, where cmdarray is an array of all the tokens in command.The command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.
So your command
"sh -c /home/burhan/bat.sh"
gets a null dir therefore the subprocess inherits the current working directory of the current process.I believe you need to structure your commands somewhere along the lines of:
And then call
exec(commands,String[] envp, File dir)
or "/bin/sh" instead of "sh"> And if @Abishek Manoharan is right you should drop the -c.
Alternatively you can use a ProcessBuilder as exmeplified in this answer.