• Post Reply Bookmark Topic Watch Topic
  • New Topic

invoking jvm from other jvm  RSS feed

 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application, for the maintenance of which, I have created a jsp page with tomcat, so that application can be started/stopped from browser. Now to start the application, what I need to do is, get the Runtime object, and call the exec method of runtime, passing as argument "java -classpath myclasspath myApp 1>/var/log/app.log 2>&1 &". This application remains on continuously, thats why I need to put it in background and redirect the output/error logs. Now, when I execute this command using exec method, it doesn't return from the exec call, neither does any output/error goes into the specified log file, though the processes of the application are started. Whats I am trying to do here is that I am trying to invoke my application in its own jvm, from the jvm of tomcat. Why is it not returning from this call? What could be the solution to this?
Waiting for replies,
Ankit.
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't give you any direct replys since I've never done what you are trying to do. But Ant does it. Maybe looking at the code for the Ant Java task would point you in the right direction.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ankit,
first things first,
"you can't put a process in background using exec()".
why?? because the API says exec() argument has following format,
"command arg1 arg2 ..." for the method you are using, right?
so if you have something like,
exec("myprog &") then imagine what will happen??
& will go as the argument-1 to the "myprog". here is the code i wrote to test,

the code in run.sh is like,

the output was,
------------------
Starting script..
Arguments: &
Key sent.
<<after sometime>>
Script executed with return code 0
-------------------
here as we can see Arguments to the run script is "&".
here the exec() call doesn't return because run scirpt was never put in the background and the shell never returned.
but if u put "./run &" in another script say "./dorun" and you run dorun using exec() then the exec() returns immidiately returning code of the ./dorun process as successful if the shell was able to fork the process ./run successfully and put it in the background. here, we won't be able to get return code for ./run process that we ran from the ./dorun script though.
i wrote a dorun.sh script that has the following code,
#!/bin/sh
./run &
and did exec("./dorun.sh") and got the following output,
-----------------
Starting script..
Arguments:
Key sent.
Script executed with return code 0
-------------------
here i got the "Script executed with return code 0" line immidiately and the 'run' script was put in the background.
so i guess this solves your problem. you can create another script and from that script you can run "java -classpath <<whatever you have>>"
btw, i forgot why i put p.getOutputStream().write((byte)13) in my code
anyways,
hope this was helpful..
regards
maulin
 
raimondas zemaitis
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done similar things (launched separate jvm's from one jvm and those jvm's are controlled by launching jvm) using JPDA. If this is of some interest to you give a note.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!