The Artima Developer Community
Sponsored Link

Java Answers Forum
Runtime.getRuntime().exec(cmd) issues

13 replies on 1 page. Most recent reply: Jul 6, 2007 1:14 AM by Anne van Rossum

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Runtime.getRuntime().exec(cmd) issues Posted: Jun 20, 2007 1:24 PM
Reply to this message Reply
Advertisement
I want to use java to telnet into a server and do stuff there, I have the following code below:
the "telnet server" part doesn't seem to do anything.

try {
p= Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("telnet server\n");
writer.flush();
writer.close();
while ((line = reader.readLine()) != null) {
System.out.println(line); //reading from process
}
} catch (IOException e) {
e.printStackTrace();
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 21, 2007 6:59 AM
Reply to this message Reply
Well, I don't know what the content of cmd is, but when I used "cmd" instead, "telnet server" war written on the standard output writer.

Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 21, 2007 7:00 AM
Reply to this message Reply
Sorry I meant
try {
p= Runtime.getRuntime().exec("cmd");
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("telnet server\n");
writer.flush();
writer.close();
while ((line = reader.readLine()) != null) {
System.out.println(line); //reading from process
}
} catch (IOException e) {
e.printStackTrace();
}
}

Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 21, 2007 7:01 AM
Reply to this message Reply
Quote:" Well, I don't know what the content of cmd is, but when I used "cmd" instead, "telnet server" war written on the standard output writer."

"telnet server" is written on the standard output writer, but it doesn't do anything.

Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 22, 2007 1:14 AM
Reply to this message Reply
Try writing "telnet server\r" instead of "telnet server\n" ?

Dave Lorde

Posts: 8
Nickname: dlorde
Registered: Aug, 2005

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 25, 2007 6:22 AM
Reply to this message Reply
Normally, to run a task inside a command prompt window using Runtime.exec(..), you append the task name and its arguments to the command string rather than trying to use the output stream.

See http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html and http://java.sun.com/developer/JDCTechTips/2003/tt0304.html#1

Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 26, 2007 1:26 PM
Reply to this message Reply
For example, if I have username :testUser and password:testPassword.

What would be the code that can telnet me into server?

Thanks

Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 27, 2007 6:38 AM
Reply to this message Reply
The code I have above works for all commands like
ls, cmd, and even perl perlFile.pl
just doesn't work for telnet.
I am very puzzled.

Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 27, 2007 6:50 AM
Reply to this message Reply
What, exactly, do you expect to happen?

What actually happens? You say that it doesn’t work, but you don’t say what it is doing wrong.

You are running on a unix/linux system? What happens when you try commands like which telnet or echo $PATH ?

Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 27, 2007 8:42 AM
Reply to this message Reply
If I use
writer.write("pwd\n");
it works, I can see the directory I am and stuff.
However
if I use
writer.write("telnet server\n");
Nothing happens, writer.write("telnet server \t"); doesn't do anything either.
So, it doesn't do anything, I get no output and I am still in the same directory.

What I am trying to do is.
Make a java program that runs in windows.

I am trying to run commands on this unix machine. So, I have to telnet into it first.

Thanks for the help guys.

Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 27, 2007 9:22 AM
Reply to this message Reply
When you try running telnet manually from the command line, does that work?

If you run echo %PATH% from the command line, does that give the same output as if you run it from inside java?

Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jun 27, 2007 9:46 AM
Reply to this message Reply
If I manually type in telnet server in the command promp, it works. It just does work when I try to use java to run the command.
Are there some sort of permission thing I have get by in order for telnet to work?

Chenen Liang

Posts: 8
Nickname: chenen
Registered: Jun, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jul 4, 2007 6:58 AM
Reply to this message Reply
hello? anybody?

Anne van Rossum

Posts: 1
Nickname: mrquincle
Registered: Jul, 2007

Re: Runtime.getRuntime().exec(cmd) issues Posted: Jul 6, 2007 1:14 AM
Reply to this message Reply
Like already said, place "telnet ..." directly in cmd. Do not just write to stdout.

Flat View: This topic has 13 replies on 1 page
Topic: Runtime.getRuntime().exec(cmd) issues Previous Topic   Next Topic Topic: Sydney Java Opportunities

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use