The Artima Developer Community
Sponsored Link

Java Answers Forum
help needed for using Runtime.getRuntime().exec

20 replies on 2 pages. Most recent reply: Jul 1, 2005 5:17 PM by javafan13

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 20 replies on 2 pages [ 1 2 | » ]
nabakumar

Posts: 23
Nickname: nkmeitei
Registered: Jun, 2005

help needed for using Runtime.getRuntime().exec Posted: Jun 17, 2005 5:09 AM
Reply to this message Reply
Advertisement
i have two java programs like this given below .
I am trying to start MyTest.java using a new process from Test.java only .
Its showing runtime error. can anybody help out where is the error.
for simplicity i have removed the try catch statements .

Test.java

public class Test {
private transient static Log log=LogFactory.getLog(Test.class);
public static void main(String str[]){
try {
Process proc = Runtime.getRuntime().exec("java MyTest");
} catch (IOException e) {

e.printStackTrace();
}
while(true){
System.out.println("I am inside main");
Thread.sleep(1000);

}

}
}
**********************
MyTest.java

class MyTest extends Thread{
public void run(){
while(true){
System.out.println("I am inside thread");
Thread.sleep(1000);
}
}

public static void main(String str[]){
MyTest mt = new MyTest();
mt.start();
}
}

thanks ,
kumar


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 17, 2005 6:31 AM
Reply to this message Reply
Okay Check out the API description for Runtime.exec():

Executes the specified string command in a *separate process*

My guess is that it waits for this process (being the
parent process to complete) before running the exec command.
And according to your code with your infinite while loop,
that process never does complete.

Try commenting out the while loop and see what happens.
Something like so:



import java.io.*;

public class Test {
//private transient static Log log=LogFactory.getLog(Test.class);
public static void main(String str[]){
try {
//Process proc = Runtime.getRuntime().exec("java MyTest");
Runtime rt = Runtime.getRuntime();
rt.exec("java MyTest");
System.out.println("***Executed java MyTest***");
} catch (IOException e) {
e.printStackTrace();
}
//while(true){
// System.out.println("I am inside main 2");
// try{
//Thread.sleep(1000);
// }catch(InterruptedException e){
// e.printStackTrace();
// }
//}
}
}


I'm not a 100% on this one so someone might want to correct
me if I'm wrong.

nabakumar

Posts: 23
Nickname: nkmeitei
Registered: Jun, 2005

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 17, 2005 7:07 AM
Reply to this message Reply
even with the necessary changes its not able to create the new process.......help awaited

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 17, 2005 7:09 AM
Reply to this message Reply
Copied and pasted ran perfectly on my machine.
What runtime error message do you receive?
Do you receive a runtime error message?

nabakumar

Posts: 23
Nickname: nkmeitei
Registered: Jun, 2005

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 19, 2005 10:40 PM
Reply to this message Reply
this time i put the line "java MyTest" in run.bat
and call run.bat in
"rt.exec("d:/myprog/run.bat");"

It is not throwing any exception and directly proceeds to the new line without any error.

but in the task manager\processes , one new cmd process comes up but the new cmd window is not
visible in the screen. may be this is the clue to the problem ... help awaited

nabakumar

Posts: 23
Nickname: nkmeitei
Registered: Jun, 2005

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 19, 2005 11:35 PM
Reply to this message Reply
the process was running without a visible console when we put exec("java MyTest").
but when I tried puting the following array in exec() ,the child process started running in a separate visible console.I got this line from some other source . but not sure what are the roles of each of these elements
can anybody tell what are the roles of each of these elements ?

String []cmds={"cmd.exe", "/c" ,"start","run.bat" };

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 20, 2005 2:10 AM
Reply to this message Reply
> the process was running without a visible console when we
> put exec("java MyTest").
> but when I tried puting the following array in exec() ,the
> child process started running in a separate visible
> console.I got this line from some other source . but not
> sure what are the roles of each of these elements
> can anybody tell what are the roles of each of these
> elements ?
>
> String []cmds={"cmd.exe", "/c" ,"start","run.bat" };

"cmd.exe" provides your command environment. Similar
to the Terminal Session in Linux. (where you execute
commands such as "java MyProgram" etc.

"/c" Should be a Dos command, I'm not familiar with it.

"start" I'm not too familiar with either.

run.bat should be the script you wrote, hence it
just runs the instructions in your Script.

java MyTest

As I said, the program copied and pasted worked perfectly
on my Linux machine, maybe you are required to set up
your command environment before executing other programs
in Windows hence the cmd.exe command comming as the
first index in the cmds array.

Sujeevan Veerabomma

Posts: 8
Nickname: saijeevan9
Registered: Jun, 2005

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 20, 2005 10:18 PM
Reply to this message Reply
Hi ,

I got some solutions from ur earlier discussions , Thanks for that.But mytask is bit different . I want to connect to a remote Unix server and execute a shell script in that server.
I used org.apache.commons.net.telnet.* for this.
But our TeamLead wants me to try with Runtime.getRuntime().
The OS on my pc is W2K.and I can run the command "telnet <server name>" from the DOS to connect to that server.

So, Iam trying to connect to taht server from java program
using Runtime.getRuntime().exec("telnet <servername>");
But it is doing nothing.

I made a run.bat file with telnet <servername> command in that and used
Runtime r=Runtime.getRuntime();
String[] cmd={"cmd.exe" , "/c" , "start" ,"d:/run.bat"};
Process P=r.exec(cmd);
in java Program.Iam able to connect to the server now in a different DOS console(c:\winnt\system32\cmd.exe-d:/run.bat).

But how to capture the IOstream of this new window to give my userid,password and execute the commands there.

Waiting for the help ...
Thanks in advance ..
sujeevan

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 20, 2005 11:07 PM
Reply to this message Reply
OK once again, I'm running a Linux machine and all that
stuff seems to run different on my Linux box.

My immediate approach to a task like that from your
machine though would be to collect all thos Strings
before creating the String []cmd array.

i.e.
print: Please enter Server to connect to
String server = EnteredString (collected via buffered Reader)
print: Enter User name
String userName = EnteredString
print: Enter Password
String password

then your cmd will be like so:

String[] cmd={"cmd.exe" , "/c" , "start" ,"d:/run.bat",
"o "+server, userName, password};

If that doesn't work. Try and collect the Names as above
then print them to the file run.bat And simply run
it as you did.

Just some suggestions. I can't try this at the moment
coz though its probably a two minute task, we don't
have Windows Machines at work (cmd.exe is a Windows
command). Will be here for the next 10 hours or so
hopefully I'll remember to test it out when I get
home in the next 9 or 10 hours.

In the mean time let me know the outcome when you run
it like that.

Spike

Sujeevan Veerabomma

Posts: 8
Nickname: saijeevan9
Registered: Jun, 2005

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 21, 2005 1:12 AM
Reply to this message Reply
Hi ,
I checked with the code u have provided.But Again the same thing repeats.It opens a new console for telnet and it asks for the login id.
Is there any way to capture the content of this new screen?Iam pasting my code here .Please have a look at it:-
import java.io.*;

public class Sample2
{
public static void main(String arg[])
{
try
{
Runtime r=Runtime.getRuntime();
String[] cmd={"cmd.exe" , "/c" , "start" ,"d:/run.bat"};
Process P=r.exec(cmd);

BufferedReader br=new BufferedReader(new InputStreamReader(P.getInputStream()));
String str=br.readLine();
System.out.println(str);
while(str!=null)
{
System.out.println(str);
str=br.readLine();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
System.exit(0);
}
}
}

My Run.bat contains only one statement:-
telnet <servername>

-------------------------

to implement the code u have provided , I created 3 variables servername,username,password in thye program and modified the cmd variable as:-
String[] cmd={"cmd.exe" , "/c" , "start" ,"d:/run.bat","o "+servername,username,password};
This also gives the same result.

what does this "o "+servername for ?
Is there ant tutorial like stuff to know what options(parameters) I can use in this exec();function..

Thanks
Sujeevan

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 21, 2005 2:25 AM
Reply to this message Reply
o is a telnet command to connect to a server
via default port 23.

As I said, I don't have access to a Windows
box, so I have no clue what the problem is.

Try to alter your run.bat file to have

telnet someserver.com
username
password

Then run it as ran initially. If that works
you can simply set your program up as you initially
did only that before setting up your cmd String
array you will take in server, username, password
then write all these to the run.bat file



fb.write("telnet "+server);
fb.write("username");
fb.write("password");
// Where fb is a FileWriter taking in
// A File with String arg "run.bat"

cmd = {"cmd.exe", "whatEverTheOtherCmndWas", "c:\run.bat"};
exec(cmd)

Obviously this is semi-pseudo code, but gives you
a rough idea. I'm not sure if it would work but
that's where I'd start.

Hope it works...

Good luck.

Sujeevan Veerabomma

Posts: 8
Nickname: saijeevan9
Registered: Jun, 2005

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 21, 2005 2:55 AM
Reply to this message Reply
I tried this way also . But instead of writing into the run.bat file thru java Program , I manualy hardcoded the username and password in the run.bat file as follows:-

run.bat :-

telnet <servername>
<username>
<password>

It connects to telnet in a new window but again asks for the userId,it wont take from the run.bat file.

I still have time, so u can let me know after u check on ur windows pc.

thanks
Sujeevan

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 21, 2005 3:26 AM
Reply to this message Reply
I guess you learn a new thing everyday.

Apparently Jakarta Commons Net provides a library
for all this Jargon. I was going to suggest
writing a Telnet Client that behaves as the
Telnet Protocol does (I wouldn't even begin
to understand where to start from).

But my suggestion is that you google the library
download it and bingo you're on your way.

Good place to start:

http://www.informit.com/guides/content.asp?g=java&seqNum=40&rl=1

Good luck again.

Sujeevan Veerabomma

Posts: 8
Nickname: saijeevan9
Registered: Jun, 2005

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 21, 2005 4:01 AM
Reply to this message Reply
Hi ,

I already tried with org.apache.commons.net.telnet.*(the Apache API u r speaking) and it is working fine. But my TeamLead wants me to do with the Runtime, he doesnt want to use any external API.

Thanks
Sujeevan

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: help needed for using Runtime.getRuntime().exec Posted: Jun 21, 2005 5:18 AM
Reply to this message Reply
You have:

Process P=r.exec(cmd);

Somewhere in your code, try include this (after eliminating
the username and password from your run.bat file) and
see what your get.


BufferedWriter buffOut = new BufferedWriter(new OutputStreamWriter(P.getOutputStream()));

BufferedReader buffIn = new BufferedReader(new InputStreamReader(P.getInputStream()));

String s;
s = buffIn.readLine();
System.out.println("s successfully retrieved");
System.out.println("s: "+s);

I have no idea how the back & forth client server
interaction works so perhaps this might help
figure that out.

Flat View: This topic has 20 replies on 2 pages [ 1  2 | » ]
Topic: embedded jetty Previous Topic   Next Topic Topic: Applets - Phased out in the near future?

Sponsored Links



Google
  Web Artima.com   

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