• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

need to call Linux command through Java Code

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the solution
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic