Also: Any question that starts out with "need to call a Linux command in a Java program" is likely to prompt the question: WHY?
Java is supposed to be platform-independent, so it stands to reason that things that are platform-dependent are unlikely to be suitable for Java. Now maybe you have a perfectly valid reason for doing this, but it might help us to know what it is.
Isn't it funny how there's always time and money enough to do it WRONG?
Joined: Sep 03, 2012
We are using Linux machine for encryption of file that why i need to execute encryption command from Linux server.
Roopesh Rana wrote:We are using Linux machine for encryption of file that why i need to execute encryption command from Linux server.
Java has the JCE which will allow you to encrypt and decrypt using a large range of algorithms using pure Java. If the in-build provider does not have the algorithms you need then I'm pretty sure the Bouncy Castle provider will have.
I don't see how that the use of SSH helps with your original problem so can you explain it to me?
There is an implication that you were trying to use Runtime.exec() on a client computer to invoke an encryption process on a server which obviously won't work! If so then your JSch Java code is insecure since it does not do "StrictHostKeyChecking" and it uses the server uesrname and password to connect to the server. You must do "StrictHostKeyChecking" and the server should use private key client authentication rather than username and password based authentication. Also, several of the 'traps' outlined in the Runtime.exec() article apply to JSch (and other SSH libraries I have used). You do not seem to be processing both 'stdout' and 'stderr' in separate threads or to be handling the return code of the remote process.