0

Is there any way to call a certain method in a running java application?

Forexample: I have a java application running, Now I want to call a method uk.co.planetbeyond.chatservice.Stopper.stop() on the same instance of the application. Can I do that?

2
  • Did you try to do that first? Commented Dec 29, 2011 at 12:04
  • Ya I googled it a lot, but found no answers. Commented Dec 29, 2011 at 12:04

2 Answers 2

2

The easiest way to do this is using JMX. You just have to expose methods you want to call to MBean. Then you can use any JMX client you want to call the methods. You can either use JConsole that is a part of your JDK or create your own command line application.

There is also command line JMX client.

Sign up to request clarification or add additional context in comments.

Comments

2

No.

You could however have your java application:

  • listen for input from the command line with new Scanner(System.in) (for example)
  • listen on a port for input using a variety of protocols, http being a common choice
  • watch for appearance of a file (lame, but it would work)

Once running, the JVM will only notice something it's looking for - ie it can only "pull"; you can't "push" anything into it

2 Comments

Why not? Enable debug, and you'll be perfectly able to stop any thread and call a method in it.
Yes you can do lots of things via debug. I was answering for the general situation (where debug is not enabled)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.