Frequently Asked Questions About the SDK for Java
ID: Q168942
|
The information in this article applies to:
-
Microsoft Win32 Virtual Machine
-
Microsoft SDK for Java, versions 2.0, 2.01, 2.02, 3.0, 3.2
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
- 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.
- 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."
- 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());
}
}
}
- 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:
- Failing to have the Java class in the CLASSPATH.
- Failing to declare your Java method as 'public.'
- Failing to register the class file (using javareg.exe).
- Using an old version of the Microsoft VM for Java in some cases
(for example, if you are attempting to use CreateObject). Install
the latest Microsoft VM for Java from http://www.microsoft.com/java.
- Missing required steps in the process. Follow the steps in the
Visual J++ sample named COMCALLINGJAVA.
- 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:
- 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.)
public int getCurrentImage() {
return m_nCurrImage;
}
- Create a Web page with an applet inside your form.
- Create a hidden field in the <FORM>, where your applet will
submit its data.
- Write a script function, like processApplet() in the example
below, to populate the hidden field by calling public functions of
your applet.
- Set your submit button's onClick function to call the script
function you wrote in step 4.
Here is the finished product:
<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>
- 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:
- Create a package to implement the protocol, for example:
.\sun\net\www\protocol\ftp
- Implement the Handler class in the
sun.net.www.protocol.{protocol-name} package. For example:
.\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);
}
}
- Implement any functions your Handler class uses:
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().");
}
}
- Create the URL and call the showDocument function
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;
}
}
- 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:
- Create your Java class. For example, here is a class in a file
called simple.java:
class Simple
{
int SimpleFn ( int x )
{
return x * 2;
}
}
- From the command line or from MSDEV, compile the .JAVA file into
a .CLASS file such as:
jvc simple.java
- Copy the resulting .CLASS file into your java\trustlib directory,
such as:
copy simple.class \winnt\java\trustlib
- Register your Java class as a COM object using JavaReg, such as:
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.
- 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:
<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.
- 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.
- 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.
- 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.
- 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).
- 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).
- 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);
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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