0

Is there a way to find out how a Javafx2 application application was launched?

I.E. was is launched via web start link or was it launched via desktop shortcut?

The reason I need to know whether the app was launched from web start or the desktop icon is that there are some parameters passed via the JNLP which the application does not use when launched from the desktop icon and there is no way other that (that I am aware of) the application would know if those parameters are not passed.

Any pointers would be helpful.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Aspirant
  • 1,934
  • 4
  • 25
  • 44
  • Thanks for editing the question Sergey. It seems much more explanatory now. I will try out the idea you suggested below. Will keep you posted. – Aspirant Jul 09 '13 at 14:43
  • I tried the first two approaches listed below: i.e. reading a property(like: hasRunOnce) from `PersistenceService` and reading the WebContext` and `SecurityManager`, but the problem did not get solved due to some issue or the other. Experts, if you can think of any other approach, please let me know. – Aspirant Jul 12 '13 at 14:04

2 Answers2

0

AFAIK, there is no good way and you need to use side effect of run mode.

You may opt to use next ones:

  1. Applet always has WebContext
  2. Applet/JNLP always has SecurityManager
  3. Desktop run usually doesn't have SecurityManager (not perfectly reliable) and never has WebContext

See next code:

    Pane root = new VBox(5);

    boolean gotSecurityManager = System.getSecurityManager() != null;
    boolean gotWebContext = getHostServices().getWebContext() != null;

    root.getChildren().addAll(
            new Label("desktop = " + (!gotSecurityManager && !gotWebContext)),
            new Label("jnlp = " + (gotSecurityManager && !gotWebContext)),
            new Label("applet = " + gotWebContext)
            );

    Scene scene = new Scene(root, 500, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();

Another option is Can I find out if the java program was launched using java or javaw

Community
  • 1
  • 1
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • I tried the above solution. But I tried launching the app from the shortcut created on the desktop and it still showed as if it was launched via jnlp. Maybe I did word my question incorrectly. Please advise. – Aspirant Jul 09 '13 at 15:02
  • Or possibly the shortcut on desktop just mimics the behavior as if the app was from web because internally it uses the jnlp file to launch the app. – Aspirant Jul 09 '13 at 15:12
  • oh, desktop shortcut, sorry I misunderstood a question – Sergey Grinev Jul 09 '13 at 16:15
  • Any possibility to find out if an application was started by clicking on the desktop shortcut that is created when a javafx app is installed from webstart? – Aspirant Jul 09 '13 at 20:03
0

(Me) ..it will only not be launched from the desktop icon one single (the first) time?

Yes, that is correct.

OK. That is doable.

In the code, check using the PersistenceService of the JNLP API for a key/value (e.g. has-run-once/true). If it does not exist, use the properties defined in the JNLP and set the value.

Here is a small demo. of the PersistenceService.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433