We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to run exec() in jsp or servlet?

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

Open in new window

Comment
Watch Question

in Java the same can be accomplished by using

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

Author

Commented:
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.

Author

Commented:
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?
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)..

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.