简体   繁体   English

无法关闭 java.exe,JNI 调用

[英]Cannot close java.exe, JNI calls

I am using a proprietary dll using the jni, anyway i need to free resources manually on shutdown.我正在使用使用 jni 的专有 dll,无论如何我需要在关机时手动释放资源。 I tried using shutdownhooks but if i dont call my method for freeing these resources which happens on killing java through netbeans or per taskmanager, java.exe is staying alive.我尝试使用shutdownhooks,但如果我不调用我的方法来释放这些资源,这发生在通过netbeans或每个任务管理器杀死java时,java.exe就会保持活动状态。 I cant even kill it with the taskmanager (access denied).我什至不能用任务管理器杀死它(访问被拒绝)。 Is there another safe method for making sure that a free resource method is called after processkill or other unsafe exiting?是否有另一种安全的方法来确保在 processkill 或其他不安全退出后调用空闲资源方法?

Edit:编辑:

Thanks for the first answer but it does not solve my problem.感谢您的第一个答案,但它并没有解决我的问题。 If i kill the program during the sleep call, it wont shutdown properly.如果我在睡眠调用期间终止程序,它就不会正常关闭。 But if i wait this 5 seconds, it is properly shutting down.但是,如果我等待这 5 秒钟,它就会正确关闭。

I think the call "AdsCallDllFunction.adsPortOpen()" is registering something for cleanup and if the process is killed, java.exe stays for about 5 - 10 minutes alive until the jvm dies.我认为调用“AdsCallDllFunction.adsPortOpen()”正在注册一些东西以进行清理,如果进程被终止,java.exe 会保持大约 5 - 10 分钟的活动状态,直到 jvm 死亡。

    import de.beckhoff.jni.tcads.AdsCallDllFunction;
    import de.beckhoff.jni.tcads.AdsVersion;

    /**
     *
     * @author Christian
     */
    public class KillTest {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            long port = 0;

            if (port == 0) {
                AdsVersion lAdsVersion = null;
                port = AdsCallDllFunction.adsPortOpen();

            }
            System.out.println("runs through this");
            /**
             * If i call the following function, it can exit.
             **/
            //AdsCallDllFunction.adsPortClose();
            try {
             /**
             * If i kill the program during this sleep call, it wont exit for a timespan of 5-10 minutes
             */
                Thread.sleep(5000);
            } catch (InterruptedException ex) {

            }



            System.exit(0);
        }

    }

Edit 2:编辑2:

http://i.stack.imgur.com/4Vh71.png http://i.stack.imgur.com/4Vh71.png

For own research you can download twincat 3 on http://www.beckhoff.com/ .对于自己的研究,您可以在http://www.beckhoff.com/上下载 twincat 3。 The DLL and jar file is provided in C:\\TwinCAT\\AdsApi\\AdsToJava. DLL 和 jar 文件在 C:\\TwinCAT\\AdsApi\\AdsToJava 中提供。

I think that the dll tries to communicate with the twincat 3 driver which gets installed on twincat 3 setup.我认为 dll 尝试与安装在 twincat 3 安装程序上的 twincat 3 驱动程序进行通信。 However if u try to kill java during the 5 seconds sleeptime through the taskmanager, the program gets stuck.但是,如果您尝试在 5 秒睡眠时间内通过任务管理器杀死 java,则程序会卡住。

As of Java 7, you can use try-with-resources statement, for ensuring that each used (opened) resource that needs to finally be closed, is closed at the end of the statement.从 Java 7 开始,您可以使用 try-with-resources 语句,以确保需要最终关闭的每个已使用(打开)资源在语句末尾关闭。 For more information, look here: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html有关更多信息,请查看此处: http : //docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

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

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