gpt4 book ai didi

java - 无法关闭 java.exe、JNI 调用

转载 作者:太空宇宙 更新时间:2023-11-04 15:07:47 26 4
gpt4 key购买 nike

我正在使用 jni 的专有 dll,无论如何我需要在关闭时手动释放资源。我尝试使用 shutdownhooks 但如果我不调用我的方法来释放这些资源(通过 netbeans 或每个任务管理器杀死 java 时发生),java.exe 将保持 Activity 状态。我什至无法用任务管理器杀死它(访问被拒绝)。是否有另一种安全方法来确保在 processkill 或其他不安全退出后调用免费资源方法?

编辑:

感谢您的第一个回答,但这并没有解决我的问题。如果我在 sleep 通话期间终止该程序,它将无法正常关闭。但如果我等这 5 秒,它就会正确关闭。

我认为调用“AdsCallDllFunction.adsPortOpen()”正在注册某些内容以进行清理,如果该进程被终止,java.exe 会保持大约 5 - 10 分钟的 Activity 状态,直到 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);
}

}

编辑2:

/image/4Vh71.png

为了进行自己的研究,您可以在 http://www.beckhoff.com/ 下载 twincat 3 。 DLL 和 jar 文件位于 C:\TwinCAT\AdsApi\AdsToJava 中。

我认为该 dll 尝试与安装在 twincat 3 安装程序上的 twincat 3 驱动程序进行通信。但是,如果您尝试通过任务管理器在 5 秒 sleep 时间内终止 java,程序就会卡住。

最佳答案

从 Java 7 开始,您可以使用 try-with-resources 语句,以确保需要最终关闭的每个已使用(打开)的资源在语句末尾关闭。欲了解更多信息,请查看此处: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

关于java - 无法关闭 java.exe、JNI 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21758498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com