tags:

views:

304

answers:

4

Hi All, I have a small test class that i want to run on a particular jvm that's already up and running (basically it's an web application running on Tomcat) . The reason i want to do this is i want to execute a small test class (with the main method and all) within that jvm so that i get the same environment (loaded and initialized classes) for my test class. Is it possible to indicate that ,say through a jvm parameter, that it should not initialize a new vm to execute my class but instead go and execute on the remote vm and show me the result here, on my console. So the local jvm acts as a kind of thin proxy ? I am not aware in case there are some tools that should make this possible ... also heard somewhere that java 6 jvm comes with an option like this , is that true ?? Please help me.

Thanks,

A: 

You might find JMX useful. Register an MBean in the server process. Invoke it with visualvm (or jconsole). (tutorial) Never tried it myself, mind.

Tom Hawtin - tackline
A: 

RMI would also do the magic.

http://java.sun.com/javase/6/docs/technotes/guides/rmi/index.html

Make your web application start an RMI registry and register your service beans there.

Then in other JVM you can run a program that queries the RMI registry started by your web application for the services you want to verify and you are done.

Reginaldo
A: 

You might want to take a look at btrace. It allows you to run code in an already started JVM provided you don't change the state of the variables inside that JVM. With this kind of tracing, you might be able solve your problem in a different way. Not by running extra code in form of a new class but by adding safe code to and existing class running inside a JVM.

For instance, you might System.out.println the name of the file when there is a call to File.exists.

Marcelo Morales
A: 

I assume "small test class" is basically some debugging code you want to run to monitor your real application, which is deployed remotely on a Tomcat. If this is the case, you should connect your Eclipse debugger remotely to the Tomcat instance, so you can set a breakpoint at interesting locations and then use the Display view of Eclipse to run any arbitrary code you might need to perform advanced debugging code. As java supports Hot Code Replacement using the debug mechanism, you can also change existing code on the remote side with new code at runtime.

mhaller
well let me re-phrase the scenario,say i have a webapp running on tomcat and the new class i want to run is not part of that codebase say some diagnostic code so i want to load this class on the exsisting jvm of the tomcat and run it there, is that possible? debugger will only help if the original class is already loaded in the jvm
redzedi