The Artima Developer Community
Sponsored Link

Java Answers Forum
i need help with this code please

1 reply on 1 page. Most recent reply: May 25, 2002 8:15 AM by Thomas SMETS

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 1 reply on 1 page
patrick

Posts: 23
Nickname: patrick
Registered: Mar, 2002

i need help with this code please Posted: May 23, 2002 10:43 PM
Reply to this message Reply
Advertisement
i have a class that take a shell command as an argument and try to execute it
but i'm having a IOException java.lang.UNIXProcess.ForkAndExec....
when i try to execute the command cd
(i want to cd /etc/mail than execute make)
this the code:

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;
import java.io.*;
import java.lang.*;

public class User
{

public static String adduser(String[] cmd)
{ String line;
String output =null;
try
{

StringBuffer buffer=new StringBuffer();


Runtime rt=Runtime.getRuntime();
Process process=rt.exec(cmd);
InputStreamReader isr=new InputStreamReader(process.getInputStream());
BufferedReader br=new BufferedReader(isr);
while ((line=br.readLine())!=null)
{
buffer.append(line);


}



int exitValue=process.waitFor();
System.out.println("ExitValue:"+exitValue);
output =buffer.toString();
}
catch(Throwable t)
{
t.printStackTrace();
}
return output;
}

public static void main(String[] argv)
{
User user=new User();
String[] cmd=new String[2];
cmd[0]="cd /etc/mail";
cmd[1]="make";
//cmd[2]="make";
user.adduser(cmd);
}

}

can anyone have an answer to this problem please


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: i need help with this code please Posted: May 25, 2002 8:15 AM
Reply to this message Reply
Patrick,
Should you look at this instead
<snip>
Process exec(String[] cmdarray, String[] envp, File dir) 
Executes the specified command and arguments in a separate process with the specified environment and working directory. </snip> ?

The only trouble with this solution is that it is available from the JDK 1.3 onwards, while you may be working with the JDK 1.2 ?

Thomas SMETS,
SCJP2 - Brussels

Flat View: This topic has 1 reply on 1 page
Topic: Retrieve the list of loaded/used classes in a java applet Previous Topic   Next Topic Topic: Printer Defaults

Sponsored Links



Google
  Web Artima.com   

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