我目前正在JAVA中构建一个应用程序,其中只能执行一次。因此,我当前正在使用一个锁定文件,在其中写入当前执行的PID。
因此,只要此应用程序启动,它将打开文件(如果存在)并尝试检测写入文件的PID是否正在实际运行。
这样可以防止我的应用在解锁文件之前崩溃的问题。
我需要它在Windows(XP,7或8)和linux(所有用户都在基于debian的发行版)上工作。
这是一些代码,可以让您更好地了解我想做什么:
//get the PID from the file
int pidValue = new FileReader(file).read();
//get the OS type
String os = System.getProperty("os.name").toLowerCase();
//Check PID depending of OS type
if( os.contains("nux") || os.contains("nix") ){
/*
* Check PID on Linux/Unix
*/
} else if ( os.contains("win") ) {
/*
* Check PID on Windows
*/
}
我试图找到有关该主题的文档,但还没有找到有用的东西。
非常感谢你。