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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling PERL script from JSP 1

Status
Not open for further replies.

mdazam

Programmer
Apr 1, 2005
28
US
hi how do I call Perl SCript from JSP
I tried this but didnt worked...

String a = "/opt/bin/symlink\r";
String b= "crypto/readme.txt";
String c= "\rEND\r";
String[] cmd = new String[] {a,b,c};
if(cmd != null){
Process p = Runtime.getRuntime().exec(a);
....

When I type string a, b and c on commandline it works. why not in jSP ?
 
errors ? Post the stack trace ...

Have you tried :

Process p = Runtime.getRuntime().exec("/opt/bin/symlink crypto/readme.txt END");

--------------------------------------------------
Free Database Connection Pooling Software
 
The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.io.IOException: CreateProcess: /opt/bin/symlink crypto/readme.txt END error=3
java.lang.Win32Process.create(Native Method)
java.lang.Win32Process.<init>(Win32Process.java:66)
java.lang.Runtime.execInternal(Native Method)
java.lang.Runtime.exec(Runtime.java:566)
java.lang.Runtime.exec(Runtime.java:428)
java.lang.Runtime.exec(Runtime.java:364)
java.lang.Runtime.exec(Runtime.java:326)
org.apache.jsp.perltest1_jsp._jspService(perltest1_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

 
1) You are running this on Windows - perl is not standard on Windows - have you installed it specially ?

2) Are you sure that /opt/bin/symlink exists ?

3)On Windows you should execute shell commands like this :

Process p = Runtime.getRuntime().exec("cmd /c mycommand");

rather than :

Process p = Runtime.getRuntime().exec("mycommand");

--------------------------------------------------
Free Database Connection Pooling Software
 
Yes /opt/bin/symlink does exists on my Unix box.

When I run the commandline of my unix box, it gives output
$ /opt/bin/symlink
crypto/readme.txt
END

Also I tried you above "cmd ... I got emtpy page. I tried to look source code its empty too
 
It may well exist on your UNIX machine, but this stack trace :

java.io.IOException: CreateProcess: /opt/bin/symlink crypto/readme.txt END error=3
java.lang.Win32Process.create(Native Method)

shows us that you are trying to run this on a Windows machine ... so whats going on ? Why are you talking about a UNIX machine when its clearly running on a Windows machine ?

--------------------------------------------------
Free Database Connection Pooling Software
 
I got the answer.

I was trying to run a perl script sitting on Unix box from my Jsp application sitting on same unix box through the browse URL. I got the answer when I used PrintWriter.

But can you help me to filter further. My out looks like this.
BEGINmyserver/download/fgh62gn3wwNYe/readme.txtEND

I have to remove "BEGIN" and "END" from the output. How ??

 
String s = "BEGINmyserver/download/fgh62gn3wwNYe/readme.txtEND"
s = s.replaceAll("BEGIN", "");
s = s.replaceAll("END", "");

--------------------------------------------------
Free Database Connection Pooling Software
 
Yes I got it thanks. Just a additional Question.

Right now I'm submiting one paramater and getting the result as you seen above.

But I dont know in real how many parameter I need to send that perl script. I get this parameter from DB, it can be 1 or 2 or 3 or 4 etc., How do I pass one by one parameter and get the result.

PS: Perl Script accepts one parameter at a time and give output.
 
How are you getting the parameters from the DB - does it come as one String or do you build it up from a result set or something ?

--------------------------------------------------
Free Database Connection Pooling Software
 
Something like this :

StringBuffer sb = new StringBuffer();
while(resultSet.next()) {
sb.append(resultSet.getString(1));
}

Process p = Runtime.getRuntime().exec(sb.toString());



--------------------------------------------------
Free Database Connection Pooling Software
 
But my output didnt came using your code:
Process p = Runtime.getRuntime().exec("/opt/bin/symlink crypto/readme.txt END");

It was good for this code:
Process p = Runtime.getRuntime().exec("/opt/bin/symlink");
PrintWriter pw = new PrintWriter(p.getOutputStream());
pw.println("crypto/readme.txt");
pw.println("END");
pw.close();


Sorry not to mention earlier. So now tell me how to use for resultset ??
 
Then maybe something more like :

Process p = Runtime.getRuntime().exec("/opt/bin/symlink");
PrintWriter pw = new PrintWriter(p.getOutputStream());
while(resultSet.next()) {
pw.println(resultSet.getString(1));
}
pw.close();

Just have a play around and see what works !


--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top