• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to connect to an UNIX server from Java application

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to execute a shell script from my Java application. I hope this can be done by using Runtime.exec(). But the problem here is my java application resides in windows NT.. and i have no idea on how to connect an UNIX server from my Java application.

Kindly provide me a solution for this.

- Karthik
 
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not related to J2MED. I will move it to Java Beginner Forum.
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree, it's not J2MED related but the question is not for beginners.

One way to achive this is with rsh (or remsh) on Windows, but you'll run into other issues such as security and what to put in the .rhosts file which you may want to address in the Unix/Linux forum.

Assuming you have rsh on Windows either natively (in C:\WINNT\system32) or from cygwin you first should test if you can run it bare bones from the cmd line.

Test if this works from a DOS window.


Then you simply put that line to your Process.exec...
[ December 01, 2004: Message edited by: Leslie Chaim ]
 
Karthikeyan Thyagarajan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leslie,

I had tried executing rsh from cmd prompt and its working fine. And i had applied the same in the following Java code. When i tried to execute "rsh <<hostname>> <<loginname>> simple" from the cmd prompt its returning the correct value. Here "simple" is a shell script in which im jus printing "Success". And this gets displayed when i execute the above mentioned command. But im not able to capture that output in the java code. I had tried capturing in 3 diff ways but none is returning the value.

Now how should i capture the return value ?

public void usingRSH()
{
try{
Runtime run = Runtime.getRuntime();
Process pro = run.exec("rsh <<hostname>> <<loginname>> simple");
pro.waitFor();

System.out.println("B4");

DataInputStream dataIn = new DataInputStream(pro.getInputStream());
while(dataIn.available() != 0){
System.out.println("DIS : " + dataIn.readLine());
}

BufferedReader in = new BufferedReader(
new InputStreamReader(pro.getInputStream()));

String line = null;
while ((line = in.readLine()) != null) {
System.out.println("BR : " + line);
}

BufferedReader out = new BufferedReader(new InputStreamReader(pro.getInputStream()));
while(out.read() != -1)
{
System.out.println("BReader : " + out.readLine());
}

System.out.println("After");
}catch(Exception expObj){
System.err.println("Error in usingRSH : " + expObj);
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic