AmolDesai23
asked on
How to run unix command from JSP
Hello Experts,
Can anybody please guide me on how to run a unix commands from jsp application ported on the unix box?
My requirement is to navigate to a particular Unix directory and run the command like: text2ps -l -m t.1.25, b.5,.5 -r 60 -c 165 -s0.85 | distill > av_report.pdf (this command creates PDF from ASCII/text files)
If you could give a sample code, it will be a great help.
Thanks.
Can anybody please guide me on how to run a unix commands from jsp application ported on the unix box?
My requirement is to navigate to a particular Unix directory and run the command like: text2ps -l -m t.1.25, b.5,.5 -r 60 -c 165 -s0.85 | distill > av_report.pdf (this command creates PDF from ASCII/text files)
If you could give a sample code, it will be a great help.
Thanks.
ASKER
Thank you Owenli.
Do I need to have my JSPs on a same Unix machine to run and try this code? What if I have to run this Unix command on a different Unix machine?
Do I need to have my JSPs on a same Unix machine to run and try this code? What if I have to run this Unix command on a different Unix machine?
Yes, it should be on same machine. It is a separate process and not a thread within JVM.
You can call prcs.waitFor() to wait for the process to finish.
For run this Unix command on a different Unix machine, I am not sure but I guess that you may need telnet API, try this package: de.mud.telnet OR org.apache.commons.net.tel net
You can call prcs.waitFor() to wait for the process to finish.
For run this Unix command on a different Unix machine, I am not sure but I guess that you may need telnet API, try this package: de.mud.telnet OR org.apache.commons.net.tel
ASKER
Thanks again! does anybody have a code to execute above unix comand on a different Unix machine (via a telnet may be... as per Owenli)?
regards.
regards.
MAy this link help u..
http://forum.java.sun.com/thread.jspa?forumID=31&threadID=437231
http://forum.java.sun.com/thread.jspa?forumID=31&threadID=437231
ASKER
Thank you friend. I don't know much abt Unix and couldn't understand the code out there in that link. Can i write something like this in my scriptlet? Will it work? if not...whats the solution?
<%
try
{
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec ("text2ps -l -m t.1.25, b.5,.5 -r 60 -c 165 -s0.85 | distill > av_report.pdf \r\n");
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStream Reader);
String line = null;
while ( (line = bufferedReader.readLine()) != null )
%>
-- <%= line %> <!-- this line is just for a debugging purpose-->
<%
System.out.println(line);
int exitVal = process.waitFor();
System.out.println ("Process exitValue: " + exitVal );
}
catch (Throwable t)
{
t.printStackTrace();
}
%>
<%
try
{
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec ("text2ps -l -m t.1.25, b.5,.5 -r 60 -c 165 -s0.85 | distill > av_report.pdf \r\n");
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader (inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStream
String line = null;
while ( (line = bufferedReader.readLine())
%>
-- <%= line %> <!-- this line is just for a debugging purpose-->
<%
System.out.println(line);
int exitVal = process.waitFor();
System.out.println ("Process exitValue: " + exitVal );
}
catch (Throwable t)
{
t.printStackTrace();
}
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<%@ page language="java" import="java.io.*, java.lang.*"%>
<%
Runtime rt = Runtime.getRuntime();
Process prcs = rt.exec(“/bin/bash”); //or your csh shell location
BufferedWriter cmd = new BufferedWriter(new OutputStreamWriter(prcs.ge
cmd.write(“text2ps -l -m t.1.25, b.5,.5 -r 60 -c 165 -s0.85 | distill > av_report.pdf”);
cmd.flush();
int returnCode = prcs.exitValue();
prcs.destroy();
%>