如果在java中判断只允许一个当前进程运行? 平台是linux,有没有比较好的实现,我想到的一些办法都有不太好的缺点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1. 什么叫做“判断只允许一个当前进程运行”呢?程序是你写的,你不去创建新的进程不就可以了吗?2. 既然是 Java 程序,那么 Linux 与否有什么关系? 另一个思路,用单例模式作个类,并设置个标记.然后在main函数里做判断 hehe, 看了楼上 yanqlv(maomao) 兄的发言,我也明白楼主的意思了。抱歉我上面提出的外行的问题 :(要做到“单一进程实例”,关键是要让第二次运行的程序能够知道前面已经有一个在跑着了。(好像是废话,hehe)作为 Java 程序,一般而言,两次运行是在两个 JVM 里,所以基本上不能通过 JVM 内部的手段来解决。yanqlv(maomao) 兄说得很对,就是要建立一个“临界区”,用“文件”是一个好办法。另外“占用一个网络端口”也是一个歪招 :) 窗体是JBUILDER声称的,如果你觉得事件那部分不好你可以改,不过思路还是LS的思路import javax.swing.JFrame;import java.awt.event.*;import java.io.File;import java.io.BufferedReader;import java.io.FileReader;import javax.swing.JOptionPane;import java.io.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2007</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class only extends JFrame { public only() { try { jbInit(); this.setSize(400,300); this.show(); boolean ff=this.ifopen(); if (ff==false) { this.setopen(); } } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { only only1 = new only(); } private void jbInit() throws Exception { this.addWindowListener(new only_this_windowAdapter(this)); } void this_windowClosing(WindowEvent e) { this.setclose(); System.exit(0); } //设置标实为N public void setclose(){ try { File f1 = new File(""); String path = f1.getAbsolutePath(); System.out.println(path); FileWriter ff = new FileWriter(path + "\\classes\\check.ini"); ff.write("N"); ff.close(); } catch (Exception ex) { System.out.println(ex); } } //设置标实为Y public void setopen(){ try { File f1 = new File(""); String path = f1.getAbsolutePath(); System.out.println(path); FileWriter ff = new FileWriter(path + "\\classes\\check.ini"); ff.write("Y"); ff.close(); } catch (Exception ex) { System.out.println(ex); } } //判断是否存在文件且信息标实为Y public boolean ifopen(){ boolean f=false; try{ File f1=new File(""); String path=f1.getAbsolutePath(); System.out.println(path); FileReader ff=new FileReader(path+"\\classes\\check.ini"); BufferedReader in=new BufferedReader(ff); String s=null; while ((s=in.readLine())!=null) { if (s.equals("Y")) { JOptionPane.showMessageDialog(this, "您已经打开一个窗口了!", "信息", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } }catch(Exception e){ System.out.println(e); } return f; }}class only_this_windowAdapter extends java.awt.event.WindowAdapter { only adaptee; only_this_windowAdapter(only adaptee) { this.adaptee = adaptee; } public void windowClosing(WindowEvent e) { adaptee.this_windowClosing(e); }} 用文件的方式不太好,如果进程异常终止的时候,那个文件还在,下一次启动程序的时候就会启动不了,但java里异常终止无法捕捉到,如 kill 9的时候。 如你用了Java6.命令行直接运行 jps -l 就可打印出机器上所运行的Java进程ID号及运行主类.___________________ www.NetJava.cn 领先的.Net&Java技术推广者.___ 文件锁 eclipse 就是这么做的 把一个文件锁起来 不让别的程序读写启动的时候去读这个文件 如果失败 则有一个存在 java 交流群:36468871 高手请进! 如 insiku(tmd 结帖啊!!!) 所说,用文件锁是一个比较好的做法,进程异常中止也能释放锁File file = new File(path);RandomAccessFile raf = new RandomAccessFile(file,"rw");FileChannel fc = raf.getChannel();FileLock lock = fc.tryLock(0, 1, false);if (lock == null) {System.out.println("已有一个实例在运行");System.exit(0);}... CrazyGou(阿狗)(...) ( ) 信誉:100 Blog 加为好友 2007-04-28 09:16:51 得分: 0 ===========感谢捧场 NND jboss5.0.0 BETA2 在jdk1.6下不能启动 在jdk1.5一下 jms有问题靠 beta的东西 就是不行 两个list中,某字段相等处理,求助 hibernate 数据库问题 时间日期加减法 JTree添加节点 此题非圣贤不会也 关于java内存泄露的一点小问题 初次提问,向大家求教: 获取日期和星期方法!! 请教各位高手我想判断字符串是数字. 我的电脑好象感染病毒了,多了很多名字是folder.htt的文件,请问怎么办? 我装了一个JSWDK1.0.1可不知如何运用自已的类,系统老说找不到 java泛型转型是的unchecked警告 jsp 本地数据库 ajax 要用哪些技术实现呀? 请教关于下拉列表框的问题
然后在main函数里做判断
import javax.swing.JFrame;
import java.awt.event.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JOptionPane;
import java.io.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class only extends JFrame {
public only() {
try {
jbInit();
this.setSize(400,300);
this.show();
boolean ff=this.ifopen(); if (ff==false)
{
this.setopen();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
only only1 = new only();
}
private void jbInit() throws Exception {
this.addWindowListener(new only_this_windowAdapter(this));
} void this_windowClosing(WindowEvent e) {
this.setclose();
System.exit(0);
} //设置标实为N
public void setclose(){
try {
File f1 = new File("");
String path = f1.getAbsolutePath();
System.out.println(path); FileWriter ff = new FileWriter(path + "\\classes\\check.ini");
ff.write("N");
ff.close();
}
catch (Exception ex) {
System.out.println(ex);
}
} //设置标实为Y
public void setopen(){
try {
File f1 = new File("");
String path = f1.getAbsolutePath();
System.out.println(path); FileWriter ff = new FileWriter(path + "\\classes\\check.ini");
ff.write("Y");
ff.close();
}
catch (Exception ex) {
System.out.println(ex);
}
} //判断是否存在文件且信息标实为Y
public boolean ifopen(){
boolean f=false; try{
File f1=new File("");
String path=f1.getAbsolutePath();
System.out.println(path);
FileReader ff=new FileReader(path+"\\classes\\check.ini");
BufferedReader in=new BufferedReader(ff);
String s=null; while ((s=in.readLine())!=null)
{
if (s.equals("Y"))
{
JOptionPane.showMessageDialog(this, "您已经打开一个窗口了!", "信息",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}catch(Exception e){
System.out.println(e);
}
return f;
}}class only_this_windowAdapter extends java.awt.event.WindowAdapter {
only adaptee; only_this_windowAdapter(only adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}
jps -l
就可打印出机器上所运行的Java进程ID号及运行主类.___________________ www.NetJava.cn 领先的.Net&Java技术推广者.___
启动的时候去读这个文件 如果失败 则有一个存在
File file = new File(path);
RandomAccessFile raf = new RandomAccessFile(file,"rw");
FileChannel fc = raf.getChannel();
FileLock lock = fc.tryLock(0, 1, false);
if (lock == null) {
System.out.println("已有一个实例在运行");
System.exit(0);
}
...
NND jboss5.0.0 BETA2 在jdk1.6下不能启动 在jdk1.5一下 jms有问题
靠 beta的东西 就是不行