executing a jar from a WEB APP

Hello everybody, How can I execute a jar file from a web application, I want to run that jar file that contains an application and to be able to send some parameters as well and get results, I am using tomcat 5,any help?
[234 byte] By [jimmyarribasplata@hotmail.coma] at [2007-11-26 16:45:11]
# 1
http://tomcat.apache.org/tomcat-5.0-doc/appdev/deployment.html- Saish
Saisha at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

what I am looking for is not executing a jar that isgoing to work in my web app but a jar that is a single application by itself, so I try to run it from my web app, I found this but it doesnt seem to work...

Process myProcess= Runtime.getRuntime().exec("java -jar tc.jar");

any ideas pls..

jimmyarribasplata@hotmail.co at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
You lost me there. Some class in that JAR that is presumably in your web application's classpath has a main() method. Invoke it directly.- Saish
Saisha at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
how? how can I execute that ? plsMessage was edited by: jimmyarribasplata@hotmail.com
jimmyarribasplata@hotmail.co at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I have some JAR named foo.jar. In there is a class called Bar with a main() method in it. So, simply call Bar.main(new String[0] {}). The JAR needs to be on your classpath and you have to figure out which class has the main() method you want to invoke. It's like any other method, nothing special about it. Or, you can use Runtime#exec().

- Saish

Saisha at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I am using runtime and it doesn't work

Runtime.getRuntime().exec("java -jar "+_request.getRealPath(_request.getContextPath())+"\tc.jar");

nothing there , I tried the following and nothing either

try {

TimerClock.main(new String[0]);

System.out.println("before any error");

} catch(Exception ex) {

System.out.println("the exception is " +ex);

}

Is there anything wrong?

jimmyarribasplata@hotmail.co at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Runtime#exec() returns a Process object. That has a waitFor() method. Have you tried that? Also, are you not getting what I am saying about simply calling main() on that object within Java?- Saish
Saisha at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
TimerClock is my main so ....Is this alright? or what is missing to launch my jar?try { TimerClock myobj = new TimerClock();System.out.println("before any error");} catch(Exception ex) {System.out.println("the exception is " +ex);}
jimmyarribasplata@hotmail.co at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
Ok. What we have here is a failure to communicate. :^)Your class "TimerClock" is the one that you want to have invoke a standalone application in another JAR, right? What is the name of the other class (in the JAR) that has the main() method?- Saish
Saisha at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

But if you did want to use Runtime#exec(), it would look like:

Process process = Runtime.exec("java -jar foo.jar");

int response = process.waitFor();

if (response != 0) {

throw new RuntimeException("Error executing foo.jar");

}

// Continue

- Saish

Saisha at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
TimerClock has the main class it is in tc.jar It launches a clock. I am calling TimerClock from a servlet:oMessage was edited by: jimmyarribasplata@hotmail.com
jimmyarribasplata@hotmail.co at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
Process process = Runtime.exec("java -jar foo.jar");int response = process.waitFor();if (response != 0) { throw new RuntimeException("Error executing foo.jar");}// ContinueUnfortunately that doesnt work, response = 1
jimmyarribasplata@hotmail.co at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
Assuming it takes no command-line arguments:TimerClock main(new String[0]{});- Saish
Saisha at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
Finally thanks to you , I finally got to it, But there was one more step I didnt know .To make that window appear I had to open services >Apache tomcat > LOG ON tab and click on ALLOW SERVICE TO INTERACT With DESKTOPThank you Saish
jimmyarribasplata@hotmail.co at 2007-7-8 23:12:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15
Glad it worked. Best of luck!- Saish
Saisha at 2007-7-21 16:49:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16

mmm One more issue

when I run this not in java but in windows run

java -jar tc.jar

the equivalents from java is :

TimerClock.main(new String[]{}); ... where TimeClock has the main in tc.jar

What would be the equivalent in java for these two cases

1java mymainclass other.jar(in this case i have to mention the class that has the main : mymainclass )

2 java mymainclass othe.jar -userid="Peter" (in this case it gets a parameter)

Thank you

jimmyarribasplata@hotmail.co at 2007-7-21 16:49:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...