简体   繁体   English

为什么重新启动的Java程序会失去键盘焦点?

[英]Why does my restarted Java program lose keyboard focus?

I attempted to create a simplified failsafe Java application that would restart itself, whenever it was forcibly shutdown (in Windows, using CMD command of CTRL+C ). 我试图创建一个简化的故障安全Java应用程序,只要它被强制关闭(在Windows中,使用CTRL + C的CMD命令),该应用程序就会自行重启。

The batch code looks like this : 批处理代码如下所示:

@echo off
setlocal

start /wait java BatchWakeMeUpSomehow

if errorlevel 1 goto retry
echo Finished successfully
exit

:retry
echo retrying...
start /wait java BatchWakeMeUpSomehow

And the java code is this one : Java代码是这样的:

public class WakeMeUpSomehow {
    static class Message extends Thread {

        public void run() {
            try {

                while(true)
                {
                    System.out.println("Hello World from run");

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            Runtime.getRuntime().addShutdownHook(new Message());
            while(true)
            {
                System.out.println("Hello World");
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I wanted to clarify my understanding . 我想澄清一下我的理解。 I think that because Java runs within a JVM environment, then it has some limitations in regards to restarting a JVM which was forcibly closed down (if we restart a forcibly-shutdown JVM via a batch file, then it loses the cursor-focus and it just runs in a new CMD window that cannot have keyboard focus. 我认为,因为Java在JVM环境中运行,所以在重新启动被强制关闭的JVM方面存在一些限制(如果我们通过批处理文件重新启动被强制关闭的JVM,那么它将失去光标焦点​​,只能在没有键盘焦点的新CMD窗口中运行。

My goal was to have this app keep running in a loop (a la "digital wackamole" ), but I can only ever re-run it once (where it's running in a non-responsive CMD window ) 我的目标是使该应用程序保持循环运行(例如“数字怪胎”),但我只能将其重新运行一次(在无响应的CMD窗口中运行)

Someone had suggested that I look at Apache Daemon tool, but I'm not sure how this helps. 有人建议我看一下Apache Daemon工具,但是我不确定这有什么帮助。

To make an endless loop in case of errorlevel 1 and above add a label before the start line: 要在错误级别1和更高级别的情况下进行无限循环,请在start行之前添加标签:

:run
start /wait java BatchWakeMeUpSomehow
if errorlevel 1 echo retrying... & goto run
echo Finished successfully

However this will also open a new window, so if you want to restart the java process in the already started window you may use typeperf to check its CPU usage (so that you can forcibly close the process and restart it) or simply start java anew once the execution is returned into the batch file. 但是,这也会打开一个新窗口,因此,如果要在已经启动的窗口中重新启动java进程,则可以使用typeperf来检查其CPU使用率(以便您可以强制关闭该进程并重新启动它)或简单地重新启动java一旦执行返回到批处理文件中。

  • In case the java process exits (doesn't hang) we can detect it with tasklist : 万一java进程退出(不挂起),我们可以使用tasklist来检测到它:

     :run start java BatchWakeMeUpSomehow :check timeout 1 >nul tasklist /fi "imagename eq java.exe" | find "java.exe" >nul if errorlevel 1 echo retrying... & goto run goto check 
  • In case the java process hangs and the CPU usage is 0% for 1 minute: 如果java进程挂起,并且CPU使用率为0%1分钟:

     if not "%~1"=="childprocess" goto main :loop java BatchWakeMeUpSomehow goto loop :main start "" "%~dpnx0" childprocess :check for /f "skip=2 delims=, tokens=2" %%a in (' typeperf "\\process(java)\\%% processor time" -sc 1 -si 60 ') do if %%a=="0.000000" taskkill /f /im java.exe goto check 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM