Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to run exec() in jsp or servlet?

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();
chdir('search');
exec('java Show_UI ' . $random . ' allList');
$list = file('tmp/' . $random, FILE_IGNORE_NEW_LINES);

Open in new window

Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

in Java the same can be accomplished by using

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

ASKER

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.

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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
process.waitFor();

Open in new window