Frequently Asked Questions About the SDK for Java

ID: Q168942


The information in this article applies to:


SUMMARY

This article covers some of the Frequently Asked Questions (FAQ) about the SDK for Java. For other Java related FAQs search the Web at:

http://support.microsoft.com/support for 'JAVA' and 'FAQ'.


MORE INFORMATION

  1. Q. How does the Msdev.exe, in Visual J++, relax the security in Internet Explorer so that my applet can run outside the Java sandbox, despite the fact that it is not in a signed .cab file?

    A. In the Microsoft VM for Java, that is included with Internet Explorer 3.x, only trusted class files can access resources outside the Java sandbox. Class files from digitally signed .cab files are trusted. If the HTML file is run from Microsoft Developer Studio (MSDEV), the class files are also trusted. This can be very helpful during applet development. However, to deliver your applet to other users, you must place them in a signed CAB file.

    When executing or debugging a Java Applet from MSDEV, the security restrictions in Internet Explorer are relaxed for quick and easy testing. MSDEV prepends the project path to the CLASSPATH environment variable that is used by Internet Explorer. This gives the classes trusted status.


  2. Q. Why does InetAddress.getLocalHost() return localhost/127.0.0.1 in my Applet?

    A. An untrusted applet isn't allowed to get the real localhost IP address because it opens various types of network attacks. If the Java libs detect an untrusted applet, then it will return the loopback address (127.0.0.1).

    JDK 1.02's/1.1's appletviewer will do the same thing unless you've changed the "network access" property in the appletviewer to "unrestricted."


  3. Q. Is it possible for an applet to read a file from my Web server? If so, how?

    A. The class below shows how to read a text file named poem.txt from the server. NOTE: Place the .htm and .class files in the same directory on the Web server along with a text file named poem.txt with at least one line of text:
    
    import java.io.*;
    import java.net.*;
    import java.applet.*;
    import java.awt.*;
    
    class ReadFile extends Applet {
       public void start () {
          try {
             URL u2 = new URL(getCodeBase(),"poem.txt");
             DataInputStream d = new DataInputStream(u2.openStream());
             String s5 = d.readLine();
             add(new Label(s5));
          }
          catch (Exception e) {
             System.out.println("Error: " + e.toString());
          }
       }
    } 


  4. Q. What are common causes of the "Failed to create object" message when trying to instantiate a COM object written in Java?

    A. Here are some common causes:



  5. Q. When I have an applet in a <FORM> it doesn't submit any information to the server. How can I get the applet to submit its information?

    A. The applet doesn't submit any information because the information a user would want to submit is specific to the applet. For example, an applet that shows images may want to submit the currently displayed image number, while an applet that displays a clock may want to submit the GMT time. It is up to the programmer to write the methods to submit the applet. The five steps below show one way you might implement submitting an applet:

    1. Create an applet with a public function that returns the information you want to submit: (For the first example above, you would return the image number.)


    2. 
      public int getCurrentImage() {
         return m_nCurrImage;
      } 
    3. Create a Web page with an applet inside your form.


    4. Create a hidden field in the <FORM>, where your applet will submit its data.


    5. Write a script function, like processApplet() in the example below, to populate the hidden field by calling public functions of your applet.


    6. Set your submit button's onClick function to call the script function you wrote in step 4.

      Here is the finished product:


    7. 
            <form action="/scripts/info.dll" method=GET name="myForm" >
            <applet code=AppletInForm.class id=myapplet
             width=320 height=240 >
            </applet>
            <input type=hidden name="appletImage">
            <script language=JScript>
              function processApplet() {
      parent.myForm.appletImage.value=document.myForm.myapplet.getCurrentImage();
              alert("Sending form: image="+parent.myForm.appletImage.value+"
      user="+parent.myForm.Username.value);
              }
            </script>
            <br>
            Username:<input type=text name="Username"><br>
            <input type=submit value="Please send me the above picture."
             language="JScript" onClick="processApplet();">
            </form> 


  6. Q. How can I get showDocument() to display a URL, when the URL protocol isn't supported by Java (for example FTP)?

    A. The URL class verifies Java protocol support by calling the protocol's Handler class. The four steps below show how to add a protocol and demonstrates how to make the call to showDocument:

    1. Create a package to implement the protocol, for example:


    2. 
      .\sun\net\www\protocol\ftp 
    3. Implement the Handler class in the sun.net.www.protocol.{protocol-name} package. For example:


    4. 
      .\sun\net\www\protocol\ftp\Handler.java.
      
      package sun.net.www.protocol.FTP;
      import java.net.URL;
      import java.net.URLConnection;
      import java.net.URLStreamHandler;
      public class Handler extends URLStreamHandler {
         public synchronized URLConnection openConnection(URL u) {
       return new FTPURLConnection(u);
         }
      } 
    5. Implement any functions your Handler class uses:


    6. 
      package sun.net.www.protocol.FTP;
      import java.io.IOException;
      public class FTPURLConnection extends sun.net.www.URLConnection {
         FTPURLConnection(java.net.URL u) {
            super(u);
         }
         public void connect() throws IOException {
            throw new IOException("FTP doesn't support connect().");
         }
      } 
    7. Create the URL and call the showDocument function


    8. 
      import java.net.*;
      public class FTPTest extends java.applet.Applet {
         public void init() {
            add(new java.awt.Button("Click to open FTP session."));
         }
         public boolean action(java.awt.Event  evt, Object  what) {
            try {
               URL u=new URL("ftp://ftp.microsoft.com");
               getAppletContext().showDocument(u,"FTPFrame");
            } catch (Exception e) {
               showStatus(e.toString());
               e.printStackTrace();
            }
       return true;
         }
      } 


  7. Q. What steps must be followed to use a Java class from an Active Server Page?

    A. First, you must install build 1257 or later of the Microsoft VM for Java or else install the SDK for Java 1.5. Then follow these steps:

    1. Create your Java class. For example, here is a class in a file called simple.java:


    2. 
      class Simple
      {
         int SimpleFn ( int x )
         {
            return x * 2;
         }
      } 
    3. From the command line or from MSDEV, compile the .JAVA file into a .CLASS file such as:


    4. 
      jvc simple.java 
    5. Copy the resulting .CLASS file into your java\trustlib directory, such as:


    6. 
      copy simple.class \winnt\java\trustlib 
    7. Register your Java class as a COM object using JavaReg, such as:


    8. 
      javareg /register /class:simple /progid:Simple 
      /class: argument is the name of the .class file in your trustlib directory.

      /progid: is the name that you will use to create your COM object.

      Now your Java class is registered as a COM object and ready to use from ASP.

    9. Create a .ASP file in a HTTP shared directory on your machine. Be sure that the directory the file is in has EXECUTE access. You can check this in the "Microsoft Internet Service Manager." A simple ASP file would look like:


    10. 
      <html>
      <body>
      <h1>Simple Test</h1>
      The result from SIMPLE is:
      <% Set SimpleObj = Server.CreateObject("Simple") %>
      <% = SimpleObj.SimpleFn(5) %>
      <hr>
      </body>
      </html> 
      That's it! Open your Web browser and point it at YOUR_MACHINE/simple.asp.


  8. Q. How do I redirect output from jview or wjview?

    A. Unfortunately, there is not an option or registry setting for jview or wjview that enables Java logging. However, you can redirect the output when invoking an application like the following example:
    
    jview main > javalog.txt 
    This works for both jview and wjview. Or, you could programmatically direct the output to a file or pipe by reassigning System.out. The following code, which works for jview/wjview and Internet Explorer, shows how this can be done:
    
       if
    ("true".equalsIgnoreCase(System.getProperty("com.ms.applet.enable.logging")
    )) {
       try {
          String logdir = System.getProperty("java.home");
          PrintStream ps = new PrintStream(new BufferedOutputStream(new
                               FileOutputStream(new File(logdir,
                                                "javalog.txt"))), true);
          System.out = ps;           // comment line if using SDK 2.0x
    //    System.setOut(ps);         // uncomment line if using SDK 2.0x
          System.err = ps;           // comment line if using SDK 2.0x
    //    System.setErr(ps);         // uncomment line if using SDK 2.0x
       } catch (Exception e) {
          }
       } 
    The ability to alter the value of System.out and other system streams may be removed or changed in the future.


  9. Q. When I create an ActiveX control in Java and then call its methods I get a java.lang.UnsatisfiedLinkError exception. Why can't I call the methods?

    A. For the Microsoft VM for Java build 1518:
    The problem is that Applets aren't ActiveX containers and therefore cannot host ActiveX controls. The call to "new myActiveXControl();" doesn't make sense because there is no container to host the control. You should use an <OBJECT> tag in your HTML file, and then pass this object reference to your Applet. For an example of how this is done, see the "OLEControls" sample in the Microsoft Visual J++ samples documentation.
    For the Microsoft VM for Java build 2057 (SDK 2.0x):
    You need to use the AXComponent class, located in the com.ms.activeX package, to host your ActiveX control.


  10. Q. How do I get the SampleSelect JDBC-ODBC sample to work with the Northwind database that comes with Access?

    A. An updated jdbcrel.htm has been placed in the sample pack that details the steps necessary to connect to the Northwind database. The sample pack can be obtained at http://www.microsoft.com/java.


  11. Q. When I call show() on a modal dialog, the Microsoft VM for Java continues executing the code after the show() call even though I haven't dismissed the dialog yet. Is this a known bug?

    A. This is a bug and has been fixed in the Microsoft VM for Java that is provided in the SDK 2.0x (build 2057 of the Microsoft VM for Java).


  12. Q. Why does System.getProperty("user.name") return null?

    A. This is a bug and has been fixed in the Microsoft VM for Java that is provided in the SDK 2.0x (build 2057 of the Microsoft VM for Java).


  13. Q. How do I tell from my Java applet or application what version of the Microsoft VM for Java is installed on the system?

    A. Use the SystemVersionManager.getVMVersion() static method in com.ms.util package to find the version number of the installed Microsoft VM for Java. The BuildIncrement property will indicate the version the Microsoft VM for Java:
    
    String build;
    build=SystemVersionManager.getVMVersion().getProperty("BuildIncrement");
    System.out.println("Using build "+build); 


  14. Q. I am getting the error message "Security Exception: Couldn't connect to with origin from file ***" in my javalog.txt file.

    A. This could be caused by changes to the AppletSecurity class, some of these restrictions have been loosened in build 1517 of the VM. You can obtain build 1517 of the VM by navigating to http://www.microsoft.com/java and click Downloads.


  15. Q. How can I execute JVIEW.EXE without displaying a DOS BOX?

    A. Use WJVIEW.EXE instead of JVIEW.EXE. WJVIEW can be found in the SDK for Java.


  16. Q. How can I combine all of my Java application's classes into one executable file?

    A. Use JEXEGEN.EXE to convert your Java classes into a Win32 application. The Win32 application will still require the Microsoft Win32 Virtual Machine for Java to be installed on the local machine.


  17. Q. Has the support for SafeArray been updated in the VM?

    A. Yes, see the documentation for the SDK for Java 2.0x at http://www.microsoft.com/java/pre-sdk and follow the link from Packages and Classes to the com.ms.com package then to Class Variant or Class SafeArray.


  18. Q. How do I install the developer version of classes.zip for the SDK 2.0x?

    A. Run the ClassD.exe from the bin directory of the SDK 2.0x, then run javasrc.exe (installed by Visual J++ in the %windir%\java\classes directory) to extract the Java files. If you do not have Visual J++, use a program that can unzip the *.JAVA files from the classes.zip file. Note: You may have to delete your old classes.zip first.


  19. Q. How can I see messages that are printed using System.out.println. In Netscape Navigator I can see these in the Java Console?

    A. For the answer to this question, please see the following article in the Microsoft Knowledge Base:
    Q177177 HOWTO: Enabling Messages Printed Using System.out.printIn


  20. Q. How can I enable the Java JIT compiler for my application?

    A. If you have enabled JIT in Internet Explorer, your applications will also run in JIT compiler mode. To enable JIT in Internet Explorer choose View, Options, Advanced, Enable Java JIT compiler.

    In the registry JIT can be enabled by setting the following to 0x00000001:
    
         HKEY_Current_User\Software\Microsoft\Java VM\EnableJIT 



REFERENCES

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:

http://support.microsoft.com/support/visualj/

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Rafael M. Munoz, Microsoft Corporation

Additional query words:


Keywords          : kberrmsg kbCommandLine kbJavaVM kbJIT kbSDKJava200 kbSDKJava201 kbSDKJava300 kbSDKJava310 kbSDKJava202 JCOM JVM UtilPkg JDB kbSDKJava320 
Version           : WINDOWS:2.0,2.01,2.02,3.0,3.2
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: July 23, 1999