aspose file tools*
The moose likes Java in General and the fly likes need to call Linux command through Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "need to call Linux command through Java Code" Watch "need to call Linux command through Java Code" New topic
Author

need to call Linux command through Java Code

Roopesh Rana
Greenhorn

Joined: Sep 03, 2012
Posts: 19
I am Trying this but this not working fine

if any one have any solution please help me /* test.java */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test
{
public static void main(String[] args)
{
String lsString = null;
Process process = null;
try
{
process = Runtime.getRuntime().exec(“ls”);
BufferedReader bufferedReader = new
BufferedReader(new InputStreamReader(process.getInputStream()));
while ((lsString = bufferedReader.readLine()) != null)
{
System.out.println(lsString);
}
try
{
process.waitFor();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
} // end test.java


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8202
    
  23

Roopesh Rana wrote:I am Trying this but this not working fine

Roopesh,

1. ItDoesntWorkIsUseless (←click) - What's not working?
2. Please read the UseCodeTags (←click) page.

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.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Roopesh Rana
Greenhorn

Joined: Sep 03, 2012
Posts: 19
We are using Linux machine for encryption of file that why i need to execute encryption command from Linux server.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8202
    
  23

Roopesh Rana wrote:We are using Linux machine for encryption of file that why i need to execute encryption command from Linux server.

So: why not just run the command? Or write a script to run it for you? Not everything needs a Java program - indeed it sounds to me like "noise code".

Winston
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1083
    
  10

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.

Using Runtime.exec() should a last resort but if you have to use it then you need to do it right. You are falling for at least 3 of the traps covered in http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and you need to implement ALL the recommendations.
Roopesh Rana
Greenhorn

Joined: Sep 03, 2012
Posts: 19
I got the solution
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1083
    
  10

Roopesh Rana wrote: I got the solution ]


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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: need to call Linux command through Java Code