The Artima Developer Community
Sponsored Link

Java Answers Forum
executing a unix shell command using jsp

3 replies on 1 page. Most recent reply: Mar 31, 2002 11:02 AM by Jay Kandy

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

Posts: 23
Nickname: patrick
Registered: Mar, 2002

executing a unix shell command using jsp Posted: Mar 29, 2002 7:46 AM
Reply to this message Reply
Advertisement
hi
i want by a click of a button execute a shell command called whois -h server query plus passing the query as argument t this command from the jsp
hwo can do that
thank you


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: executing a unix shell command using jsp Posted: Mar 29, 2002 12:50 PM
Reply to this message Reply
Patrick,

Firstly, this is not a universal solution. I mean you can not run any *NIX command with this class. In a perfect world, you might want to check for ErrorStream, wait appropriately for a command to finish, etc...For simplicity's sake I ignored all those cases. This class runs commands that dont expect an input from command line during the process of execution. (For example 'passwd')

import java.io.InputStreamReader;
import java.io.BufferedReader;
 
public class RunCommand
{
    public static String execute(String[] cmd)
    {
		String output = null;
 
        try
        {
			String line;
			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;
    }
 
	/**
	 *	Usage:
	 *
	 *	public static void main(String s[])
	 *	{
	 *		String cmd[] = new String[1];
	 *		cmd[0] = s[0];
	 *
	 *		System.out.println( RunCommand.execute( cmd ) );
 	 *	}
	 */
}
 


Deploy the class in WEB-INF/classes directory and within the JSP, use
<jsp:useBean id="rc" scope="session" class="RunCommand"/>

<%
String cmd[] = String[1];
cmd[0] = "dir"
%>
<% RunCommand.execute( cmd ); %>

Also note: I did not test with whois command. (My *NIX machine is not on LAN )

Sincerely,
Jay

patrick

Posts: 23
Nickname: patrick
Registered: Mar, 2002

Re: question for Mr jay Kandy Posted: Mar 30, 2002 5:10 AM
Reply to this message Reply
Mr kandy,
you code that you gave me about executing a shell command from jsp worked wonderfully
but i tried it whith my command (whois)
it gave me a null message on the screen
i must pass whois -h server query to the shell
with -h ,server ,query as argument to the command whois and print the result on the jsp page
ps: the code must wait for the whois complete its mission
thank you

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: question for Mr jay Kandy Posted: Mar 31, 2002 11:02 AM
Reply to this message Reply
Patrick,

You can do that yourself with help from this article:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?

I can be of help if you are stuck.

Sincerely,
Jay.

Flat View: This topic has 3 replies on 1 page
Topic: ascii in java Previous Topic   Next Topic Topic: accessing database through jsp

Sponsored Links



Google
  Web Artima.com   

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