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?
# 1
http://tomcat.apache.org/tomcat-5.0-doc/appdev/deployment.html- Saish
# 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..
# 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
# 4
how? how can I execute that ? plsMessage was edited by: jimmyarribasplata@hotmail.com
# 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
# 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?
# 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
# 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);}
# 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
# 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
# 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
# 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
# 13
Assuming it takes no command-line arguments:TimerClock main(new String[0]{});- Saish
# 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
# 15
Glad it worked. Best of luck!- Saish
Saisha at 2007-7-21 16:49:59 >

# 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