Installing a native library to the Applet client's computer

by Erika Kupkova

To use an external package including native libraries by an applet isn't that easy. It's not enough just to pack it to the applet's jar file - the run time machine would not find it.

You need to copy the native library to the client's JVM.

The InstallApplet does the installation of a java package to the client's JVM (both the native and java libraries). After the installation, the package becomes part of the JVM, so any applet can use it.

To have the right to write to the client's disk, the InstallApplet jar file has to be signed or the client's java security policy has to be modified.

In this particular case, the applet is signed and is installing the javatwain.dll and javatwain.jar libraries from the www.gnome.sk site. You can see this applet working online at http://www.gnome.sk/Twain/remote/www/twain/server/php/remote.html. As well as the applets using the "Morena 6.0 - Image Acquisition Framework for Java(tm) Platform" for remote scanning. Both javatwain.dll and javatwain.jar libraries are part of the Morena Package.

The code of the installation applet is below.

01 /**
02  *  InstallApplet demonstrates the first step of the two-step application
03  *  installation. It downloads the javatwain.dll to the lib/ext/x86 and the
04  *  javatwain.jar to the lib/ext subdirectory of the clients JRE.
05  *  In order to get access to the java.home system property, this applet has 
06  *  to be signed.
07  */
08  
09 import java.applet.*;
10 import java.awt.*;
11 import java.awt.image.*;
12 import java.net.*;
13 import java.io.*;
14  
15 public class InstallApplet extends Applet 
16 
17   String javaHome=System.getProperty("java.home");
18   String osArch=System.getProperty("os.arch");
19   static String status1=null;
20   static String status2=null;
21   static String status3=null;
22   
23   public void init()
24   
25     try
26     
27       super.init();
28       setLayout(null);
29       // install the javatwain.dll and javatwain.jar libraries
30       // to the clients computer
31       // the libraries have to placed in the same folder as the
32       // applet's jar file is
33       URL codeBase=this.getCodeBase();
34       installFile(this,new URL(codeBase, "javatwain.jar"), javaHome+"\\lib\\ext\\javatwain.jar");
35       installFile(this,new URL(codeBase, "javatwain.dll"), javaHome+"\\lib\\ext\\x86\\javatwain.dll");
36     }
37     catch (Exception exception)
38     
39       exception.printStackTrace();
40     }
41   }
42 
43   public void paint(Graphics g)
44   
45     super.paint(g);
46     if (status1!=null)
47     
48       g.drawString(status1,120,140);
49       g.drawString(status2,120,155);
50     }
51     if (status3!=null)
52     
53       g.drawString(status3,120,170);
54     }    
55   }
56   
57   protected static void installFile(Applet applet, URL sourceUrl, String destFileName)
58   
59     File destFile=new File(destFileName);
60     if (!destFile.exists())
61       try
62       
63         System.err.println("installing file " + destFileName)
64         destFile.getParentFile().mkdirs();
65         URLConnection connection=sourceUrl.openConnection();
66         InputStream is=connection.getInputStream();
67         FileOutputStream fos=new FileOutputStream(destFile);
68         byte[] buff = new byte[8192];
69         BufferedInputStream in = new BufferedInputStream(is, buff.length);
70         BufferedOutputStream out = new BufferedOutputStream(fos, buff.length);
71         int i;
72         int count = 0;
73         while ((i = in.read(buff,0,buff.length)) != -1)
74         out.write(buff,0,i);
75           count += i;
76         }  
77         applet.showStatus(count+" bytes copied ...");
78         in.close();
79         out.close();
80         status1="The javatwain library has been downloaded. To finish the" ;
81         status2="installation, you have to close the browser now. Reopen ";
82         status3="the browser to run the scanner application."
83       }
84       catch (Exception exception)
85       
86         exception.printStackTrace();
87         applet.showStatus(exception.toString());
88       }
89     else 
90     
91       System.err.println("file " + destFileName + " already exists")
92       status1="The javatwain library has been on your computer already";
93       status2="No installation is needed";
94     
95   
96 }