Learn how to a build a cloud-first strategyRegister Now


How to run exec() in jsp or servlet?

Posted on 2009-02-17
Medium Priority
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
  • 3
  • 3
LVL 19

Expert Comment

ID: 23661607
in Java the same can be accomplished by using

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

Author Comment

ID: 23689761
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 java_Show_UI.java? under WebRoot or src folders?
Currently, I put it under src dir without package.

LVL 19

Expert Comment

ID: 23690980
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 23712142
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

Kuldeepchaturvedi earned 150 total points
ID: 23712478
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

ID: 23824513
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 Gen_Show_Pic.java

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


Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 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