bash 如何在不退出 JVM 的情况下多次运行 Java 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11992198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:03:31  来源:igfitidea点击:

How to run a Java program multiple times without JVM exiting?

javabashjvmjvm-arguments

提问by HymanWM

Suppose I have a Java program Test.class, if I used the below script

假设我有一个 Java 程序Test.class,如果我使用下面的脚本

for i in 1..10
do
    java Test
done

the JVM would exit each time java Testis invoked.

每次java Test调用JVM 时都会退出。

What I want is running java Testmultiple times without exiting the JVM, so that, the optimized methods in prior runs can be used by later runs, and possibly be further optimized.

我想要的是在java Test不退出JVM的情况下多次运行,以便以后运行可以使用先前运行中优化的方法,并可能进一步优化。

回答by Nicolas Mommaerts

Why not write a Java program that repeatedly calls the entry point of the program you want to run?

为什么不写一个重复调用你要运行的程序的入口点的Java程序呢?

回答by mtk

Run this once

运行一次

java Run

The main Program calling your Testclass mulitple times.

主程序Test多次调用您的课程。

class Run {

    public static void main(String[] args) {
        for(int i=0; i<10; i++){
            Test.main(args);
        }
    }
}

回答by CodeBlue

public class RunTest
 {
    public static void main(String[] args)
      {
          for(int i=1; i<=10; i++)
              Test.main(args); 
      }
 }

This should do it. You can call the Test class' main function with the same arguments you pass to your main function. This is the "wrapper" that another poster referred to.

这应该做。您可以使用传递给 main 函数的相同参数调用 Test 类的 main 函数。这是另一张海报提到的“包装器”。

回答by DankMemes

Use code like this:

使用这样的代码:

public static void main2(String args[]){ // change main to main2
  for(int i=0;i<10;i++)main(args);
}
public static void main(String args[]){ // REAL main
   //...
}

回答by Johan Sj?berg

Perhas a ProcessBuilderwould be suitable? E.g., something like

Perhas aProcessBuilder将是合适的?例如,像

String[] cmd = {"path/to/java", "test"};
ProcessBuilder proc = new ProcessBuilder(cmd);
Process process = proc.start();

回答by Ajay George

What you want to do is put the loop inside the Testclass or pass the number of times you want to loop as an argument to the main()inside Test.

您想要做的是将循环放入Test类中或将您想要循环的次数作为参数传递给main()inside Test

NOTE:

笔记:

If you are looking at JIToptimizations it will take more than 10 iterations to kick in.

如果您正在研究JIT优化,则需要 10 多次迭代才能开始。

回答by Diego Torres Milano

Same as previous examples but using command line args

与前面的示例相同,但使用命令行参数

public class Test {

    public static void main(String[] args) {
        if ( args.length > 0) {
            final Integer n = Integer.valueOf(args[0]);
            System.out.println("Running test " + n);
            if ( n > 1 ) {
                main(new String[] { Integer.toString(n-1) });
            }
        }
    }
}

use the args to indicate the number of runs

使用 args 表示运行次数

$ java Test 10