Home : Accessing COM Components from Java Applets in Native Mode
Q30128 - INFO: Accessing COM Components from Java Applets in Native Mode

Accessing COM Components from Java Applets in Native Mode

 

It is possible to use native mode to access COM components from Java Applets (e.g. when embedding an ActiveX control inside the Applet). However, there are a few things you should be aware of. Consider the following (typical) J-Integra/Applet configuration:

Server

  • Hosts the Applet and the jintegra.jar runtime file
  • May host the COM component (the COM component can also reside on a separate Windows machine)

Client

  • Downloads the applet from the server through a web browser

The client must also download some additional files:

  • To run an Applet in native mode, you need to download c:\jintegra\bin\ntvinv.dll and its ResourceDLL (c:\jintegra\bin\international\ntvinvEnglishResourceDll.dll) to the client machine (along with jintegra.jar and the required Java classes).

  • Another limitation to running Applets in native mode is that you cannot specify which user credentials you wish to use to access the COM component. By defualt, native mode uses c:\jintegra\bin\ntvauth.dll to pick up the credentials of the user who is currently logged into the client machine, and uses them to access the COM component. Therefore, you also need to download ntvauth.dll and its ResourceDLL (c:\jintegra\bin\international\ntvinvEnglishResourceDll.dll) as well as ntvocx.dll to the client machine before the Applet will run.

  • When embedding ActiveX controls, it is also necessary to download the ActiveX control itself because the nature of ActiveX control requires that its GUI can only be displayed on local machine.

  • Once the COM component is downloaded to the client machine, it needs to be registered and configured so that the user who is currently logged onto the machine has access to it. This is a highly restrictive limitation, as this will be very difficult to do (if not impossible) at runtime. This makes it pretty much necessary to know the client machines ahead of time so you can register and configure the COM component before the Applet is run.

Policy Files and Security

An Applet is severely restricted in what files it can access on a computer. This restriction protects your computer against security risks. In order to load native DLLs (or write to a log file, etc...), an Applet needs to be granted special access permissions. In order to grant the Applet access permissions to the various files it needs to load (or write to), you will have to configure a policy file for the machine the Applet will run on.

A very simple way of granting unlimited permissions to an Applet is to modify the java.policy file found in [JAVA_HOME]\jre\lib\security. Copy the java.policy file to your working directory and add the following lines to it:

    grant codeBase "file:[WORKING DIRECTORY]/*" { permission java.security.AllPermission; };

This grants all access permissions to the files located in your [WORKING_DIRECTORY] folder. Otherwise you will get this error message:

    java.security.AccessControlException: access denied (java.lang.RuntimePermission loadLibrary.ntvinv)

Loading a Policy File Using the AppletViewer

To specify the policy file to be used when using the Java appletviewer to run an Applet, specify the following Java property:

    appletviewer -J-Djava.security.policy=java.policy Applet.html

Loading a Policy File when Running an Applet in Internet Explorer

When using IE to view an Applet, you should place the java.policy file in the following directory:

    %JAVA_HOME%\jre\lib\security

Then make a copy of java.policy file, rename it as .java.policy (a period sign is added as prefix), and put it in the following directory:

    Windows XP: C:\Documents and Settings\"account name"\.java.policy
    Windows 2000: C:\Documents and Settings\"account name"\.java.policy
    Windows NT: C:\WINNT\Profiles\"account name"\.java.policy
    Windows 98: C:\Windows\.java.policy

Example

The example below demonstrates how to embed Adobe Acrobat in an Swing Applet. Create your own working folder. This example uses a folder named Z:\Testing\PDF.

Copy the following Java code and paste it in a file named AppletPDF.java.

import javax.swing.*;
import java.awt.*;

public class AppletPDF extends javax.swing.JApplet {
  public void init() {
    try{
      com.linar.jintegra.Cleaner.trackObjectsInCurrentThread();

      JTextField textField = new JTextField("Adobe Acrobat Embedded in a Swing Applet");
      textField.setEditable(false);
      textField.setBackground(Color.cyan);

      // Create the PDF Control, set its size, and set its source document
      pdfocx.Pdf pdf = new pdfocx.Pdf();
      pdf.setSize(new Dimension(400,400));
      pdf.setSrc("Z:\\Testing\\PDF\\whitepaper_jintegra.pdf");

      // Add the PDF control to the frame, and display the frame
      getContentPane().add(textField, BorderLayout.NORTH);
      getContentPane().add(pdf, BorderLayout.CENTER);
      validate();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Copy the following HTML code and paste it in a file named Applet.html.

<HTML><HEAD><TITLE>J-Integra Applet Example</TITLE></HEAD>
<BODY>
<applet code="AppletPDF.class" width=400 height=400 archive="jintegra.jar, pdfocx.jar">
</applet>
</BODY>
</HTML>

Copy the following code and paste it in a file named java.policy. You can also copy the first three lines and append them to your own java.policy file. Remember to replace the /Z:/Testing/PDF/* with your actual working directory. You must use forward slash "/" instead of backward slash "\".

grant codeBase "file:/Z:/Testing/PDF/*" {
  permission java.security.AllPermission;
};

grant {
  permission java.lang.RuntimePermission "stopThread";
  permission java.net.SocketPermission "localhost:1024-", "listen";
  permission java.util.PropertyPermission "java.version", "read";
  permission java.util.PropertyPermission "java.vendor", "read";
  permission java.util.PropertyPermission "java.vendor.url", "read";
  permission java.util.PropertyPermission "java.class.version", "read";
  permission java.util.PropertyPermission "os.name", "read";
  permission java.util.PropertyPermission "os.version", "read";
  permission java.util.PropertyPermission "os.arch", "read";
  permission java.util.PropertyPermission "file.separator", "read";
  permission java.util.PropertyPermission "path.separator", "read";
  permission java.util.PropertyPermission "line.separator", "read";
  permission java.util.PropertyPermission "java.specification.version", "read";
  permission java.util.PropertyPermission "java.specification.vendor", "read";
  permission java.util.PropertyPermission "java.specification.name", "read";
  permission java.util.PropertyPermission "java.vm.specification.version", "read";
  permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
  permission java.util.PropertyPermission "java.vm.specification.name", "read";
  permission java.util.PropertyPermission "java.vm.version", "read";
  permission java.util.PropertyPermission "java.vm.vendor", "read";
  permission java.util.PropertyPermission "java.vm.name", "read";
  permission java.net.SocketPermission "192.168.0.78:135-", "connect, accept, listen, resolve";
};

Download a PDF file from http://j-integra.intrinsyc.com/whitepaper/, and put it in your working directory.

Generatea Java proxies for Adobe Acrobat, and then run "jar -cfv pdfocx.jar pdfocx" to create a pdfocx.jar file which contains the generated proxies. Copy jintegra.jar, ntvauth.dll, ntvinv.dll, ntvocx.dll and their resource DLLs to Z:\Testing\PDF. Finally, your working folder should contain all the files as listed below:

Z:\Testing\PDF\
             > international\
                           > ntvinvEnglishResourceDll.dll
                           > ntvauthEnglishResourceDll.dll
            
             > AppletPDF.java
             > Applet.html
             > java.policy
             > whitepaper_jintegra.pdf
             > jintegra.jar
             > pdfocx.jar
             > ntvauth.dll
             > ntvinv.dll
             > ntvocx.dll

Compile the AppletPDF.java. You can now use appletviewer to run the Applet:

    appletviewer -J-Djava.security.policy=java.policy Applet.html

It you would like to run the Applet from Internet Explorer, for example, on Windows 2000, you will need to put it to

    %JAVA_HOME%\jre\lib\security

Then rename it to .java.police (a period sign is added as prefix) and copy it to

    C:\Documents and Settings\"account name"\

Refer to the previous section - Policy Files and Security - for more information. Remember to reboot your machine after moving the policy file. You should then be able to see output similar to the following:

Workaround

One workaround is to use Java Web Start instead of an Applet to deploy the application. The application can then be launched by a single click over the network and run in native mode. See the related article below for more information.

Related Articles
Q40543 - HOWTO: Deploying J-Integra (Native Mode) Applications with Java Web Start (JWS)

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 7/13/2006.
Last Modified by No Author Name Available!.
Article has been viewed 9960 times.
Rated 6 out of 10 based on 214 votes.
Print Article
Email Article