判断当前java程序是否正在运行2007-05-30
/*** 通过文件锁来判断程序是否正在运行* @return 如果正在运行返回true,否则返回false*/private static boolean isRunning(){boolean rv=false;try {//String os_name=System.getProperty("os.name");//指定文件锁路径String path=null;if(os_name.indexOf("Windows")>-1){//如果是Windows操作系统path=System.getProperty("user.home")+System.getProperty("file.separator");}else{path="/usr/temp/";}File dir=new File(path);if(!dir.exists()){dir.mkdirs();}//程序名称String applicationName="sms";RandomAccessFile fis = new RandomAccessFile(path+applicationName+".lock","rw");FileChannel lockfc = fis.getChannel();FileLock flock = lockfc.tryLock();if(flock == null) {System.out.println("程序正在运行.");rv=true;}} catch (FileNotFoundException e1) {e1.printStackTrace();}catch (IOException e) {e.printStackTrace();}return rv;}