How to run exec() in jsp or servlet?

Posted on 2009-02-17
Last Modified: 2013-11-24
The following is a php code. It exec a java command and run the java class file.

How can I do it the same in jsp?

$random = rand();


exec('java Show_UI ' . $random . ' allList');

$list = file('tmp/' . $random, FILE_IGNORE_NEW_LINES);

Open in new window

Question by:techques
    LVL 19

    Expert Comment

    in Java the same can be accomplished by using

    System.getRuntime.exec("java Show_UI "+ random " allList");

    Author Comment

    Dear Sir

    There is a problem. Do you remember you taught me
    <a href="/project/genpicture?id=<%=rs.getInt("id")%>">click me</a>

    Then, I build a servlet doGet() and I put the code insides it:
    System.getRuntime.exec("java Show_UI " + random + " allList");

    Which java class should I import to it?

    Moreover, where should I put the under WebRoot or src folders?
    Currently, I put it under src dir without package.

    LVL 19

    Expert Comment

    you do not need to import any classes..

    move your java class ( I am assuming the name of your java class is Show_UI and not java_show_UI)...

    to a folder on C: lets say C:\myCode.

    change your code to

    System.getRuntime.exec("java c:\\myCode\\Show_UI " + random + " allList");

    Also note that this code will run your java class on Server not on Client...
    if you are trying to so show something on the client side, you are doing it wrong.

    Author Comment

    I do not understand what did you mean running on server but not client.
    I host it in localhost tomcat.

    Can I run it?

    When I open a console and type java c:\mycode\Show_UI, it said NoClassDefFoundError

    I need to enter into the dir c:\mycode>java Show_UI and it can run. Is it classpath problem?
    LVL 19

    Accepted Solution

    Yes it is the classpath problem..
    you shd be able to fix it by setting your classpath..

    By running on server I meant, it will run on the same machine where your server is installed ( i.e. tomcat is installed)..
    if you are doing localhost, yes you can see it..
    but if you are accessing your jsp from different machine's browser.. the exec() will open your java program on the server only ( i.e the same machine where your tomcat is installed)..

    Author Comment

    sorry, the code still not work. I need to run a java file which located in local directory, but not under webapp

    The local directory is C:/search, the java class file is Gen_Show_Pic.class and

    I added C:/search to classpath and path in environmental variables

    The servlet code is below:

    The program hanged after running it.

    If I run the command in cmd mode, it will generate a file with file name '10' under C:/search/test folder.

    String id = 10;
    Process process = Runtime.getRuntime().exec("java Gen_Show_Pic " + id + " imageFile " + id);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now