send command line parameters to existing instance of java application

hi,

how to send command line parameters to existing instance of java application?

if i've one instance of application running, and from console i invoke my application again by sending some peramaters to it, how my application existing instance can get that data, or is there any other way to do this?

[321 byte] By [naeemga] at [2007-11-26 18:54:35]
# 1

/*

* SingleInstance.java

*

* Created on February 19, 2007, 9:43 AM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package evaluation.jws;

import javax.jnlp.ServiceManager;

import javax.jnlp.SingleInstanceListener;

import javax.jnlp.SingleInstanceService;

import javax.jnlp.UnavailableServiceException;

/**

*

* @author javious

*/

public class SingleInstance {

SingleInstanceService sis;

SISListener sisL = new SISListener();

// for proof that only once instance is running.

private int counter;

private boolean keepRunning = true;

/** Creates a new instance of SingleInstance */

public SingleInstance() {

try {

sis =

(SingleInstanceService)ServiceManager.lookup("javax.jnlp.SingleInstanceService");

} catch (UnavailableServiceException e) { sis=null; }

sis.addSingleInstanceListener(sisL);

}

public static void main(String args[])

{

final SingleInstance instance = new SingleInstance();

new Thread() {

public void run()

{

while(instance.keepRunning)

{

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

instance.shutdown();

}

}.start();

System.out.println("SingleInstance running");

}

class SISListener implements SingleInstanceListener {

public void newActivation(String[] params) {

System.out.println("counter["+(counter++)+"]");

System.out.println("new arguments["+params[0]+","+params[1]+"]");

if(params[1].equals("stop"))

{

keepRunning = false;

}

}

}

public void shutdown()

{

sis.removeSingleInstanceListener(sisL);

System.exit(0);

}

}

SingleInstance.jnlp -

<?xml version="1.0" encoding="utf-8"?>

<!-- JNLP File for Single Instance Application -->

<jnlp

spec="1.5+"

codebase="file:///C:/Evaluation/dist/"

href="SingleInstance.jnlp">

<information>

<title>Single Instance Application</title>

<vendor>Javious, Inc.</vendor>

<homepage href="docs/help.html"/>

<description>Single Instance Application</description>

<description kind="short">A demo of the capabilities

of the JWS Single Instance Service.</description>

<offline-allowed/>

<association mime-type="application-x/singleinstance-file" extensions="sif"/>

<shortcut online="false">

<desktop/>

<menu submenu="My Corporation Apps"/>

</shortcut>

</information>

<resources>

<j2se version="1.5+" java-vm-args="-esa -Xnoclassgc"/>

<jar href="Evaluation.jar"/>

</resources>

<application-desc main-class="evaluation.jws.SingleInstance"/>

</jnlp>

Not sure how to pass multiple arguments, but you can run this program with the following different commands:

C:\Evaluation\dist>%java_home%\bin\javaws -open "C:/Evaluation/dist/alternate_args1.sif" "file:///C:/Evaluation/dist/SingleInstance.jnlp"

C:\Evaluation\dist>%java_home%\bin\javaws -open "C:/Evaluation/dist/alternate_args2.sif" "file:///C:/Evaluation/dist/SingleInstance.jnlp"

C:\Evaluation\dist>%java_home%\bin\javaws -open "C:/Evaluation/dist/alternate_args3.sif" "file:///C:/Evaluation/dist/SingleInstance.jnlp"

Before initial launch you could accept the request to register the file extension "sif" (SingleInstanceFile) to your program and then write various files with that extension to contain different launch parameters. You would access this filename as param[1] and read from it with java.io.*. Also when your done creating those files and registering the extension, you can simply double-click on them.

javiousa at 2007-7-9 20:31:18 > top of Java-index,Java Essentials,Java Programming...
# 2

import javax.jnlp.SingleInstanceListener;

import javax.jnlp.SingleInstanceService;

i didn't found above libraries in my jdk, i use jdk1.4.3

and how to automatically associate that relevant file with my app? i couldn't totally understand your logic but i guess it will work to solve this problem.

naeemga at 2007-7-9 20:31:18 > top of Java-index,Java Essentials,Java Programming...
# 3

I can't remember where it was in 1.4, but it currently resides in my 1.5 at:

jdk1.5.0_09\jre\lib\javaws.jar

You don't need to do anything special to associate the file with your app other than accept the prompts when you first see your application run and it asks you if you wish to associate the file.

You don't really need to associate the file for it to work though. In fact, I'm inclined to take back that part of my answer. The launch commands should suffice from the command prompt.

javiousa at 2007-7-9 20:31:18 > top of Java-index,Java Essentials,Java Programming...