How to start HBase master using Java code in ubuntu

135 views Asked by At

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");
2

There are 2 answers

2
Laurentiu L. On

You are calling exec(String command) which is a convenience method.

An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null). from the Runtime documentation

That (exec(String command,String[] envp, File dir)) in turn behaves in exactly the same way as the invocation exec(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:

String[] commands = new String[] { "sh", "-c", "/home/burhan/bat.sh" }; 

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.

2
Codebender On

When you use -c option in sh, it means you are passing commands as a string instead of a shell file. Hence the statement sh -c /home/burhan/bat.sh means that /home/burhan/bat.sh is the command you want to execute and not the file you want to execute.

So to run the file remove the -c argument,

p=Runtime.getruntime().exec("sh /home/burhan/bat.sh");