Reputation: 31
We have an use case to transfer > 4GB data file from a sftp site to server box. We have decided to create a java service which will invoke a shell script that interns enter into the sftp site and path and get the file to the target destination.
We have wrote the following code to invoke shell script from Java file.
Process proc = null;
String command = "/mnt/hmdata/loadTest.sh";
System.out.println("passing command::" + command);
try {
Runtime rt = Runtime.getRuntime();
proc = rt.exec(command.trim());
boolean status = proc.waitFor `enter code here` (45, TimeUnit.SECONDS);
LOGGER.log(Level.INFO, "Shell Called successfully");
if (status) {
msg = "Shell Called successfully";
} else {
msg = "Error while calling Shell";
}
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
proc.destroy();
}
The shell script is like that
sshpass -p '<password>' scp -r <userid>@<host>:/finint/inbound/financial/tenv/edw_osccl/* .
Once I am calling my java service, our service unable to start the shell. On contrary same shell is working fine from server command prompt. We are running our program from Unix.
Can you anyone please suggest the solution.
Upvotes: 0
Views: 197
Reputation: 31
Resolved. No Change in Java Code.
Changed Shell Script as
echo "Hello" >> /mnt/hmdata/output.txt
chmod 777 /mnt/hmdata/output.txt
export PASSWD="password"
sshpass -f /mnt/hmdata/password scp -v -r <user><host>:<path>/<file>
Upvotes: 1
Reputation: 169
I think the command should be sh /mnt/hmdata/loadTest.sh
instead of /mnt/hmdata/loadTest.sh
for you to be able to use it from your java process.
And it can be represented as String[]
, like so:
String[] cmd = { "sh", "/mnt/hmdata/loadTest.sh"};
public Process exec(String command)
is a convenience method for public Process exec(String command, String[] envp, File dir)
witch is in turn a convenience method for public Process exec(String[] cmdarray, String[] envp, File dir)
, according to Javadoc.
It would be nice if you could share the catch
block also just so we have the full try-catch
block.
Upvotes: 0