Search code examples
javanpapibrowser-pluginhtml5-filesystem

Running application from web browser


I would like to run application, for example Windows Calculator from a website. I consider using browser plugin, Java Web Start, something like agent or any other solution that would work on windows/linux, without much work to do.

Plugin way: I saw that it could be done with NPAPI but it seems to be dead. I looked also at FileSystemAPI but it also seems to be dead. Is there any good API to do this?

Agent way: I thought about Java agent that user will install and website would communicate with system through it.


Solution

  • I've managed to do that by Java Web Start:

    1) I've created Swing application, with source code as below. Based on http://java.dzone.com/articles/java-web-start-jnlp-hello tutorial.

    Code below:

    package com.gogowitczak;
    
    import javax.jnlp.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class Main {
        static BasicService basicService = null;
    
        public static void main(String args[]) {
            JFrame frame = new JFrame("Mkyong Jnlp UnOfficial Guide");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel label = new JLabel();
            Container content = frame.getContentPane();
            content.add(label, BorderLayout.CENTER);
            String message = "Jnln Hello Word";
    
            label.setText(message);
    
            try {
                basicService = (BasicService)
                        ServiceManager.lookup("javax.jnlp.BasicService");
            } catch (UnavailableServiceException e) {
                System.err.println("Lookup failed: " + e);
            }
    
            JButton button = new JButton("http://www.mkyong.com");
    
            ActionListener listener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    try {
                        Runtime.getRuntime().exec("C:\\Windows\\System32\\calc.exe");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
    
            button.addActionListener(listener);
    
            content.add(button, BorderLayout.SOUTH);
            frame.pack();
            frame.show();
        }
    }
    

    2) Besides of that, I created MANIFEST.MF file.

    File is in src\META-INF\MANIFEST.MF path:

    Manifest-Version: 1.0
    Main-Class: com.gogowitczak.Main
    Permissions: all-permissions
    

    3) And, of course, .jnlp file, for Java Web Start.

    File is in src\JNLP-INF\APPLICATION.JNLP path:

    <?xml version="1.0" encoding="utf-8"?>
    <jnlp spec="1.0+" codebase="http://localhost:8080/" href="Test.jnlp">
        <information>
            <title>Jnlp Test</title>
            <vendor>MyVendor</vendor>
            <homepage href="http://localhost:8080/" />
            <description>Testing Testing</description>
        </information>
        <security>
            <all-permissions/>
        </security>
        <resources>
            <!-- <j2se version="1.7+" /> -->
            <j2se version="1.7*" java-vm-args="-Xmx32m" max-heap-size="32m" href="http://java.sun.com/products/autodl/j2se"/>
            <jar href="jws.jar" />
        </resources>
        <application-desc main-class="com.gogowitczak.Main" />
    </jnlp>
    

    4) It should be possible to compile and run this project right from IDE you're using (IntelliJ is mine). Clicking on button should open new Windows Calculator window.

    5) Create .jar from this project. In IntelliJ it's easy: File -> Project structure -> Artifacts -> '+' -> jar -> From modules with dependencies. Pick com.gogowitczak.Main as Main Class. Make sure that checkbox Build on make is selected.

    6) Build project again. In <project_path>\out\artifacts\<project_name>_jar you should find <project_name>.jar file. For me it's jws.jar.

    7) Now you need to create your own own certificate. It's required to sign .jar file. You can do it by running keytool -genkey -keystore testKeys -alias jdc. keytool.exe can be found in JDK installation directory, in bin folder. For me it's path C:\Program Files\Java\jdk1.8.0_05\bin\keytool.exe. Remember password you set, other things are irrelevant right now. This method is based on Oracle website

    8) Sign your .jar by executing jarsigner jsw.jar jdc. Most probably it will complain about lack of .keystore file in home directory. Move out testKeys file there, and change it's name to .keystore (If Windows would resufe to set a filename with dot at the beginning, you can always execute move testKeys .keystore for renaming it.

    9) Now you have to put it on web server. Easiest way of doing that is setting up Tomcat. Download it from here, put our signed .jar file, together with copy of APPLICATION.JNLP, into <tomcat_directory>\webapps\ROOT\ directory. Rename APPLICATION.JNLP to Test.jnlp

    10) Now you can give it a try. Go to gttp://localhost:8080/Test.jnlp and see what happens. Every time browser wars you about danger, just keep agreeing to execute it anyway. If it fails, it's most probably caused by rejecting self-signed certificate. Open "Configure Java" menu and add http://localhost:8080 entry to "Exceptions site list".

    Configure Java window

    11) Now go again to web browser and refresh page. This time executing java app should suceed, and after clicking a button, it should open Windows Calculator.

    Don't hesitate to write a comment if I'm doing something wrong or if you have any questions. Hope it would help somebody. And remember about up-voting ;)