Running Java programs in a single run-time instance

I am wondering if this is possible: I have a Java program that takes arguments and prints to the console. I need to run it several times - it (the jar file) works smoothly, but the overhead of starting and stopping Java time is a big way. Is there a way to create an instance of java runtime (or vm, I'm not sure how to call it) once, and then somehow connect to this runtime environment 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.

+2
java jvm
source share
4 answers

It's hard to write a wrapper class that calls the main JAR class and calls AppClass.main () with the appropriate arguments repeatedly:

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

Remember that the main () method of a Java application is not something special, it is just a static method that you can call yourself. It can even be called by multiple threads at the same time if it is properly designed.

+11
source share

It might be better to create a design to create the main shell that executes your code several times. Think about it in these terms. Create a class file and call the method as many times as you need.

0
source share

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

0
source share

Note that in the obvious approach there is no reloading of classes between moments. If you change static fields, the fields are NOT retrieved!

If this could be a problem, consider rewriting to handle this. Another approach can be considered in an OSGi-based container, since they do not allow individual segments to see each other, so each call has classes loaded on their own.

0
source share

All Articles