• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:SingleInstanceServiceを使って Web Start アプリケーションの重複起動を禁止
#navi(../)
RIGHT:Posted by [[aterai]] at 2008-02-25
*SingleInstanceServiceを使って Web Start アプリケーションの重複起動を禁止 [#md640f9c]
SingleInstanceServiceを使って、Web Start アプリケーションの重複起動を禁止したり、引数の取得を行います。
---
category: swing
folder: SingleInstanceService
title: SingleInstanceServiceを使って Web Start アプリケーションの重複起動を禁止
tags: [SingleInstanceService, ServiceManager, SingleInstance]
author: aterai
pubdate: 2008-02-25T02:06:56+09:00
description: SingleInstanceServiceを使って、Web Startアプリケーションの重複起動を禁止したり、引数の取得を行います。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTTIU5TktI/AAAAAAAAAj4/muKNMFrhEcE/s800/SingleInstanceService.png
---
* 概要 [#summary]
`SingleInstanceService`を使って、`Web Start`アプリケーションの重複起動を禁止したり、引数の取得を行います。

-&jnlp;
-&jar;
-&zip;
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTTIU5TktI/AAAAAAAAAj4/muKNMFrhEcE/s800/SingleInstanceService.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTTIU5TktI/AAAAAAAAAj4/muKNMFrhEcE/s800/SingleInstanceService.png)

**サンプルコード [#zf8b6235]
#code{{
final JFrame frame = new JFrame("@title@");
* サンプルコード [#sourcecode]
#code(link){{
JFrame frame = new JFrame("@title@");
try {
  SingleInstanceService sis =
      (SingleInstanceService)ServiceManager.lookup("javax.jnlp.SingleInstanceService");
      (SingleInstanceService) ServiceManager.lookup("javax.jnlp.SingleInstanceService");
  sis.addSingleInstanceListener(new SingleInstanceListener() {
    private int count = 0;
    public void newActivation(String[] args) {
      //System.out.println(EventQueue.isDispatchThread());
    @Override public void newActivation(String[] args) {
      // System.out.println(EventQueue.isDispatchThread());
      EventQueue.invokeLater(new Runnable() {
        public void run() {
        @Override public void run() {
          JOptionPane.showMessageDialog(frame, "");
          frame.setTitle("title:"+count);
          frame.setTitle("title:" + count);
          count++;
        }
      });
    }
  });
} catch(UnavailableServiceException use) {
} catch (UnavailableServiceException use) {
  use.printStackTrace();
  return;
}
}}

**解説 [#kf8f5501]
Web Start アプリケーションの場合、javax.jnlp.SingleInstanceService に、SingleInstanceListener を追加することで、新しい次のインスタンスの起動やその時の引数を取得することが簡単に出来ます。
* 解説 [#explanation]
`Web Start`アプリケーションの場合、`javax.jnlp.SingleInstanceService`に`SingleInstanceListener`を追加することで、新しい次のインスタンスの起動やその時の引数取得などが可能になります。

%%ただし、Java 1.6.0_03 では、うまく動作しないようです。%% 1.6.0_10 で修正済み。

----
コンパイルに、javaws.jar が必要なので、以下のようなクラスパスを設定します。
// - %%メモ: `JDK 1.6.0_03`では`SingleInstanceService`は正常に動作しない%% `JDK 1.6.0_10`で修正済み
- コンパイルに`javaws.jar`が必要なので以下のようなクラスパスを設定
 set CLASSPATH=%JAVA_HOME%/jre/lib/javaws.jar

または、build.xmlなどに記入してください。
- または`build.xml`などに記入
#code{{
<path id="project.class.path">
  <pathelement location="${build.dest}" />
  <pathelement location="${java.home}/lib/javaws.jar" />
  <pathelement path="${java.class.path}" />
</path>
}}
-[http://javahowto.blogspot.com/2006/05/javahome-vs-javahome.html Java How To ...: JAVA_HOME vs java.home]

**参考リンク [#aa4383a9]
-[http://java.sun.com/javase/ja/6/docs/ja/jre/api/javaws/jnlp/javax/jnlp/SingleInstanceService.html SingleInstanceService (JNLP API Reference 1.5)]
-[http://bugs.sun.com/view_bug.do?bug_id=6631056 Bug ID: 6631056 SingleInstanceService does not work on JRE 1.6.0_03]
--via:[http://forums.sun.com/thread.jspa?threadID=5246786 Java Web Start & JNLP - How to use singleinstance service with a JWS application]
-[https://appframework.dev.java.net/servlets/ReadMsg?listName=users&msgNo=396 Java Web Start SingleInstanceService - appframework(JSR-296)]
-[[ServerSocketを使ってアプリケーションの複数起動を禁止>Swing/SingleInstanceApplication]]
-- [http://javahowto.blogspot.com/2006/05/javahome-vs-javahome.html Java How To ...: JAVA_HOME vs java.home]

**コメント [#rd58f835]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/jre/api/javaws/jnlp/javax/jnlp/SingleInstanceService.html SingleInstanceService (JNLP API Reference 1.8.0_71)]
- [https://bugs.openjdk.org/browse/JDK-6631056 Bug ID: 6631056 SingleInstanceService does not work on JRE 1.6.0_03]
-- via: [https://community.oracle.com/thread/1307009 Java Web Start & JNLP - How to use singleinstance service with a JWS application]
- [https://appframework.dev.java.net/servlets/ReadMsg?listName=users&msgNo=396 Java Web Start SingleInstanceService - appframework(JSR-296)]
- [[ServerSocketを使ってアプリケーションの複数起動を禁止>Swing/SingleInstanceApplication]]

* コメント [#comment]
#comment
#comment