dahpgjgamgan
dahpgjgamgan

Reputation: 3007

Running java programs in one runtime instance

I am wondering if such thing is possible: I have a java program that takes arguments and gives output to the console. What i need is to run it multiple times - it (jar file) runs smoothly but the overhead for starting and stoping java runtime is way to big. Is there a way to instantiate java runtime (or vm, I'm not sure how to call it) once, and then somehow connect to that runtime several times and execute the jar?

I hope that despite my serious ignorance of java terminology, someone will be able to answer my question :D.

Upvotes: 2

Views: 1051

Answers (4)

Please note that the obvious approach listed does NOT reload classes between instantations. If you modify static fields the fields do NOT get reinitialized!

If that may be a problem, consider rewriting to deal with this. Another approach may be looking into a OSGi container based solution, as they do not allow separate segments to see one another, so each invocation have classes loaded on its own.

Upvotes: 0

andersoj
andersoj

Reputation: 22934

It should be straightforward to write a wrapper class that calls into the JAR's Main-class, and calls AppClass.main() with the appropriate arguments repetitively:

// wraps class MyWrapped
class MyWrapper {

public static void main(String[] args) {
   for (each set of command-line args) {
       MyWrapped.main(arguments);
   }
}

Remember, a Java app's main() method is nothing special, it's just a static method you can call yourself. It could even be invoked by multiple threads simultaneously, if properly designed.

Upvotes: 11

Ben Hardy
Ben Hardy

Reputation: 1759

Any reason you can't just have this run in loop?

Upvotes: 0

Mike Pone
Mike Pone

Reputation: 19330

Might be a better to design to create a main wrapper that executes your code multiple times. Think about it in those terms. Instantiate a class file and call a method as many times as you need.

Upvotes: 0

Related Questions