Tuesday, June 8, 2010

Java Web Start - JNLP Applet reading COM Port

  1. Java Web Start version 1.5.0 is included in the Java 2 Platform Standard Edition 5.0.
  2. With Java Web Start, you launch applications simply by clicking on a Web page link. If the application is not present on your computer, Java Web Start automatically downloads all necessary files. It then caches the files on your computer so the application is always ready to be relaunched anytime you want—either from an icon on your desktop or from the browser link. And no matter which method you use to launch the application, the most current version of the application is always presented to you.
  3. The technology underlying Java Web Start is the Java TM Network Launching Protocol & API (JNLP). This technology was developed via the Java Community Process (JCP). Java Web Start is the reference implementation (RI) for the JNLP specification. The JNLP technology defines, among other things, a standard file format that describes how to launch an application called a JNLP file.

I have developed some small application to read local system com1 port from server using Applet and JNLP usage. I used JNLPAppletLauncher to get the native lib from server to client system, for me native libraries use nativelib tag does not worked , i dont know whts the reason. Manually i dropped the dll inside java-home/bin.

1. Applet code must be always outside WEB_INF, as this code will be used by browser.[ If u r using saperate jar for applet code] , server never allow to get the files inside WEB-IN, so applet code must be available to browser , so place the applet jar outside WEB-INF.

2.All jars must be signed by RCA certification, ordinary signing will not allow you in jdk1.6 plugins.

we have to create certificate first to sign the jars.

keytool -genkey -keystore weight.keystore -alias signkey -keypass password -storepass password -keyalg RSA -validity 365 -dname "CN=vamsi, OU=Development, O=ACME"

I used like this to create the Certificate

And i used the following to sign the jar

jarsigner -keystore weight.keystore -storepass password -keypass password applet-launcher.jar signkey

signKey is the certificate name , all jars must be signed with same time near by , else browser will say invalid certificate.

3. To allow the applet to use the local resources we can change the policy file of the client system.But this is not a good practice. for testing purpose in our local pc we can try, but in projects we can't go to each client system and change the java security policy file.

4.sun java api is not suitable to read com ports for windows. So i used rxtx jar and dll. [http://rxtx.qbang.org/wiki/index.php/Download]. Another jars can be used to read com ports[http://www.serialio.com/]. this is better than rxtx, as serilio is having some more advantes over rxtx like multiple readers of com port is supported in serilio.

5. Use previliged code to allow the applet to access the resources. snap of the code as shown below:

private void loadCom1() {
sPort = (SerialPort) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
SerialPort port = null;
try {
//loadLibraryInternal("rxtxSerial");
ports = CommPortIdentifier.getPortIdentifiers();

while (ports.hasMoreElements()) {
pID = (CommPortIdentifier) ports.nextElement();
if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (pID.getName().equals("COM1")) {
try {
port = (SerialPort) pID.open("PortReader", 2000);
} catch (PortInUseException e) {
System.err.println("Port already in use: " + e);
}
try {
port.setSerialPortParams(4800, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
}
catch (UnsupportedCommOperationException ucox) {
ucox.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return port;
}
});

try {

inStream = sPort.getInputStream();
this.add(field1);
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
System.out.println("COM1 found");

} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File Settings File Templates.
} catch (TooManyListenersException e) {
e.printStackTrace(); //To change body of catch statement use File Settings File Templates.
}
}

The bold styled code is important to access the com port .

6. Html code will be useful so placing here,




<applet height="100" archive="applet-launcher.jar,RXTXcomm.jar" width="100" code="org.jdesktop.applet.util.JNLPAppletLauncher" name="myApplet"><param name="_cx" value="2646"><param name="_cy" value="2646"></p></p> </p> </p>http://localhost:8080/calweight/launch.jnlp</a>">

</applet>



generally for JNLPAppletLauncher ,sub applet param value will be our own applet, but here i changed the JNLPAppletCode and used the same .

7. JNLP code is like this :


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

<!-- JNLP File for My Application -->

<jnlp spec="1.5+" codebase="http://localhost:8888/test/weightcal/">

<information>

<title>My Application</title>

<vendor>My Company</vendor>

<description>My Application</description>

<description kind="short">MyApp</description>

</information>

<security>

<all-permissions/>

</security>

<resources OS = "Windows">

<j2se version="1.5+ 1.6+"/>

<lib href="RXTXcomm.jar" download="eager"/>

<lib href="applet-launcher.jar" download="eager"/>

<nativelib href="rx-dll.jar" />

</resources>

<applet-desc

name="Com1Reader"

main-class="PortReader.class"

width="300"

height="300">

</applet-desc>

<update check="background"/>

</jnlp>



Hope this will be useful some other people like me .

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. hello. thanks for the post. I am trying to do something similar form quite some time, but it is driving me crazy.
    I have been trying to use webstart in netbeans.

    I have a RXTXcomm.jar which provides me the gnu classes. Additionally the platform specific dll -> rxtxwin32.dll.
    I add RXTXcomm.jar in the libraries tab for my project and get a coorresponding entry in my jnlp resources tag like this:

    jar href="new.jar" main="true"
    jar href="lib/RXTXcomm.jar"

    where do I keep my rxtxwin32.dll ?
    Could you please help me with it.

    Regards
    Naman

    ReplyDelete
  3. additionally i tried this:
    I have made a jar of my native dll.
    and edited my jnlp file with nativelib tag:

    Code:
    jar href="new.jar" main="true"
    jar href="lib/RXTXcomm.jar"
    nativelib href="lib/rxtxSerial.dll.jar"


    Now I run it but it shows a error:
    Use of native libraries require unrestricted access to the system.

    with stacktrace:
    JNLPException[category: Security Error : Exception: null : LaunchDesc: null ]
    at com.sun.javaws.LaunchDownload.checkJNLPSecurity(Unknown Source)
    at com.sun.javaws.Launcher.downloadResources(Unknown Source)
    at com.sun.javaws.Launcher.prepareLaunchFile(Unknown Source)
    at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
    at com.sun.javaws.Launcher.launch(Unknown Source)
    at com.sun.javaws.Main.launchApp(Unknown Source)
    at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
    at com.sun.javaws.Main$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


    Do you have any ideas about it ?

    ReplyDelete
  4. my friend, this is my bat which will compile and create the war file

    @echo off
    cd/d E:\ComPor\src\org\jdesktop\applet\util
    javac -nowarn -d . -cp "E:\ComPor\src\RXTXcomm.jar;" JNLPAppletLauncher.java
    jar cvf applet-launcher.jar org
    copy applet-launcher.jar D:\weightCal
    cd/d D:\weightCal
    jarsigner -keystore weight.keystore -storepass password -keypass password applet-launcher.jar signkey
    jarsigner -keystore weight.keystore -storepass password -keypass password rx-dll.jar signkey
    jarsigner -keystore weight.keystore -storepass password -keypass password RXTXcomm.jar signkey
    jar cvf calweight.war .
    copy *.jar E:\oc4j101330\j2ee\home\applications\amsatms\amsatms\weightcal
    start iexplore http://localhost:8888/amsatms
    cd/d c:

    ReplyDelete