1

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.

Greg Witczak
  • 1,634
  • 4
  • 27
  • 56
  • Check out Javascript http://stackoverflow.com/questions/2716284/how-can-i-run-a-local-windows-application-and-have-the-output-be-piped-into-the it might be able to do something like this – TheBat Aug 29 '14 at 18:22
  • Are you talking about running programs that the user would have installed already on their computer or running applications that would be hosted from your web page? – Jason Sperske Aug 29 '14 at 18:23
  • Do you mean run your own web app or applet, or are you literally talking about running the Windows Calculator on the user's hard disc when they click a button in a browser? – markspace Aug 29 '14 at 18:25
  • @JasonSperske @markspace - yes, that's what I'm talking about. Executing, for example, `C:\Windows\System32\calc.exe`. I'm familiar with web servers like Glassfish and Tomcat and deploying `war` web applications there. And, as far as I know, I can't trigger execution of user local program using Java on a server side. – Greg Witczak Aug 29 '14 at 18:37
  • There really isn't a method to accomplish this. Access to system resources (like applications that run locally) is the attack vector that a virus would use. Possibly by running an applet where you request system access from the user but here the browser would present a stern warning to not grant this access – Jason Sperske Aug 29 '14 at 18:58
  • @JasonSperske yes, I'm aware of that. But, for example, I can start Battlefield 3 from web browser - I just have to install Chrome plugin. As another example, when I log to corporate VPN networks, it launches desktop application, when I agree to start Java in browser. – Greg Witczak Aug 29 '14 at 19:11

2 Answers2

1

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 ;)

Greg Witczak
  • 1,634
  • 4
  • 27
  • 56
-2

May be you would Like to use an Applet?

You may attach an applet to a web-page, it gets downloaded to the client site.They you will be able to preform operations.

Community
  • 1
  • 1
joey rohan
  • 3,505
  • 5
  • 33
  • 70