dominoGuru.com

Your Development & Design Resource

XPages, Java Runtimes, and Windows OS-level interaction via native cmd.exe Calls

While responding to questions received via email, through Facebook and Twitter, I came across a question that I thought would make a good article.

The basic gist of the question is this: is there a way to call a Windows OS application via a Lotus Notes Client XPage?

The developer looking for a solution was attempting to use JavaScript to call an ActiveX Control to execute a local method call... but there's another way.

The Plan: use a Java method to call a Windows OS-level xcopy.exe.

(As Nathan points out, the Java File methods are the more desired approach for performing xcopy-style operations as it's not only more flexible but multi-OS compatible... but not only is this an illustrative example showing that Windows OS native cmd.exe calls are possible, but I plan on publishing another article and app using the Java File methods soon.)

First, we'll create our simple Java file (xwinos.java):

package com.dominoguru.xwinos;

public class xwinos {
       
    public static void xcopy() {
        try {
            String cmd = "xcopy.exe c:\\source c:\\target";
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(cmd);
        }
        catch(Exception e) {
            e.printStackTrace();
        }  
    }      
}

You can use any Java-friendly authoring tool (Eclipse, Aptana, et al) or even Notepad to create the above xwinos.java, which you can save on your desktop (any directory, really).

Once you have your xwinos.java, you'll want to import it into the NSF and include it in the XPage Build Path. In an attempt to not repeat myself, follow the steps outlined in this article for adding a Java Script Library to the Project Build Path.

Once included in the Build Path, your Java Script Library will be available for import/use in an SSJS Script Libary:

importPackage(com.dominoguru.xwinos);

function interact() {
    xwinos.xcopy();
}

Now, you're ready for the XPage...

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.resources>
        <xp:script src="/functions.jss" clientSide="false"></xp:script>
    </xp:this.resources>
    <p style="margin: 25px;">
    <xp:button value="Interact!" id="button1">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="complete">
            <xp:this.action>
                <xp:executeScript script="#{javascript:interact();}">
                </xp:executeScript>

            </xp:this.action>
        </xp:eventHandler>
    </xp:button>
    </p>
</xp:view>

This XPage Souce XML will produce a very simple but functional XPage:

XWinOS 
Example XPage

Once the Interact! button is clicked, the xwinos.xcopy method is called and -- as written -- all contents from the C:\source directory will be copied to the C:\target directory.

Example/Download

XWinOS Example NotesDatabase Download

XWinOS, the above example NotesDatabase, includes the Java, SSJS, and XPage designs discussed in the above article.


About the author: Chris Toohey

Thought Leadership, Web & Mobile Application Development, Solutions Integration, Technical Writing & Mentoring

A published developer and webmaster of dominoGuru.com, Chris Toohey specializes in platform application development, solutions integration, and evangelism of platform capabilities and best practices.



More from dominoGuru.com


dominoGuru.com is powered by IBM Notes Domino XPages & hosted by Prominic.NET

Contact Us

Use our Contact / Feedback form or one of these email addresses:

Creative Commons License

Except where otherwise noted, dominoGuru.com by Chris Toohey is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.