在一个运行时实例中运行Java程序 [英] Running java programs in one runtime instance

查看:72
本文介绍了在一个运行时实例中运行Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这样的事情是否可能: 我有一个Java程序,它带有参数并将输出提供给控制台.我需要多次运行-它(jar文件)运行平稳,但是启动和停止Java运行时的开销却很大.有没有一种方法可以一次实例化Java运行时(或vm,我不确定如何调用它),然后以某种方式几次连接到该运行时并执行jar?

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?

我希望尽管我对Java术语一无所知,但仍然有人能够回答我的问题:D.

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

推荐答案

编写一个包装类,直接调用JAR的Main-class,并重复调用带有适当参数的AppClass.main(),应该很简单:

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);
   }
}

请记住,Java应用程序的main()方法没什么特别的,它只是可以调用的静态方法.如果设计合理,它甚至可以同时由多个线程调用.

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.

这篇关于在一个运行时实例中运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆