Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I execute a SYSTEM COMMAND??

Status
Not open for further replies.

rcsen

Programmer
Jan 11, 2001
51
US
Hi,
I would like to execute a SYSTEM COMMAND inside the java code.
Example:

class syscomm{
public static void main(String args[])
{
------ "java code1";
}
}

I tried with java.lang.Runtime "exec"
but it is not working.
Can you suggest me how to proceed with?

Thanks for your help in advance.
 
Do a "Keyword Search" in this forum with "/cmd".

Otto
 
Hi Otto,
I tried that. But I couldn't find the details for JAVA.

Thanks.
RCSEN
 
U can use this function :

/**
*
* execute ksh Program (or any command that U can run from the prompt).
*
* @param comman the command that need to be executed
* in the syntax : "prog.ksh parameter1 p2 ...pi"
*
* @return the output of the program that was execute.
*
*/

public String executeProgram(String comman )
{
String results = null;
Process proc = null;

try
{
proc = Runtime.getRuntime().exec(comman);
//Get the ouput of the Command.
InputStream is = proc.getInputStream();
StringBuffer bfstr = new StringBuffer();
int ch=0;

while ((ch=is.read()) != -1)
bfstr.append((char)ch);

results = new String(bfstr.toString());
is.close();

proc.destroy();
}
catch(Exception e)
{
Amc_DaemonRef.writeLog("Error in function executeProgram : " +e.getMessage());
am with the command : "+comman+".");

}

return results;
}//End of executeProgram.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top