Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need to call Linux command through Java Code

 
Roopesh Rana
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roopesh Rana
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are using Linux machine for encryption of file that why i need to execute encryption command from Linux server.
 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the solution
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic