您可以使用ManagementFactory对象.从
here:
import sun.management.ConnectorAddressLink;
import sun.jvmstat.monitor.HostIdentifier;
import sun.jvmstat.monitor.Monitor;
import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.MonitoredVmUtil;
import sun.jvmstat.monitor.MonitorException;
import sun.jvmstat.monitor.VmIdentifier;
public static void main(String args[]) {
/* The method ManagementFactory.getRuntimeMXBean() returns an identifier with applcation PID
in the Sun JVM, but each jvm may have you own implementation.
So in anothers jvm, other than Sun, this code may not work., :(
*/
RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
final int runtimePid = Integer.parseInt(rt.getName().substring(0,rt.getName().indexOf("@")));
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// If exists another instance, show message and terminates the current instance.
// Otherwise starts application.
if (getMonitoredVMs(runtimePid))
{
new MainFrame().setVisible(true);
} else
JOptionPane.showMessageDialog(null,"There is another instance of this application running.");
}
});
}
The getMonitoredVMs(int processPid) method receives as paramter the
current application PID, and catch the application name that is called
from command line, for example, the application was started from
c:\java\app\test.jar path, then the value variable is
“c:\java\app\teste.jar”. This way, we will catch just application name
on the line 17 of the code below. After that, we search JVM for
antoher process with the same name, if we found it and the application
PID is different, it means that is the second application instance.
private static boolean getMonitoredVMs(int processPid) {
MonitoredHost host;
Set vms;
try {
host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
vms = host.activeVms();
} catch (java.net.URISyntaxException sx) {
throw new InternalError(sx.getMessage());
} catch (MonitorException mx) {
throw new InternalError(mx.getMessage());
}
MonitoredVm mvm = null;
String processName = null;
try{
mvm = host.getMonitoredVm(new VmIdentifier(String.valueOf(processPid)));
processName = MonitoredVmUtil.commandLine(mvm);
processName = processName.substring(processName.lastIndexOf("\\") + 1,processName.length());
mvm.detach();
} catch (Exception ex) {
}
// This line is just to verify the process name. It can be removed.
JOptionPane.showMessageDialog(null,processName);
for (Object vmid: vms) {
if (vmid instanceof Integer) {
int pid = ((Integer) vmid).intValue();
String name = vmid.toString(); // default to pid if name not available
try {
mvm = host.getMonitoredVm(new VmIdentifier(name));
// use the command line as the display name
name = MonitoredVmUtil.commandLine(mvm);
name = name.substring(name.lastIndexOf("\\")+1,name.length());
mvm.detach();
if ((name.equalsIgnoreCase(processName)) && (processPid != pid))
return false;
} catch (Exception x) {
// ignore
}
}
}
return true;
}
The javax.jnlp.SingleInstanceService provides a set of methods for applications to register themselves as singletons, and to register listener(s) for handling arguments passed in from different instances of applications.