?
Solved

How to run unix command from JSP

Posted on 2006-07-13
7
Medium Priority
?
1,850 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:AmolDesai23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:owenli27
ID: 17102146
How about run shell from JSP script?  not sure it is best answer...

<%@ 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.getOutputStream()));
  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();

%>
0
 

Author Comment

by:AmolDesai23
ID: 17102259
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?
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17102896
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.telnet



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AmolDesai23
ID: 17103049
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.
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17105960
0
 

Author Comment

by:AmolDesai23
ID: 17107604
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(inputStreamReader);
          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();
          }
    %>
0
 
LVL 9

Accepted Solution

by:
owenli27 earned 1500 total points
ID: 17108133
AmolDesai,

The code will work fine.  You may need use whereis/which text2ps to find its absolute location path. such as:
runTime.exec ("/local/bin/text2ps  -l  -m  t.1.25, b.5,.5  -r 60  -c 165 ....
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question