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:
� | 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). For more information about Microsoft VM for Java, visit the following Microsoft Web site: | � | Missing required steps in the process. Follow the steps in the
Visual J++ sample named COMCALLINGJAVA. |
|
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:
a. | 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;
}
| b. | Create a Web page with an applet inside your form. | c. | Create a hidden field in the <FORM>, where your applet will submit its data. | d. | Write a script function, like processApplet() in the example below, to populate the hidden field by calling public functions of your applet. | e. | 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>
|
|
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:
a. | Create a package to implement the protocol, for example:
.\sun\net\www\protocol\ftp
| b. | 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);
}
}
| c. | 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().");
}
}
| d. | 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;
}
}
|
|
7. | 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. |
8. | 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.
|
9. | 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. For more information about the sample pack, visit the following Microsoft Web site: |
10. | 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).
|
11. | 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).
|
12. | 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);
|
13. | 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. For more information about the latest build for Microsoft VM, visit the following Microsoft Web site: |
14. | 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.
|
15. | 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.
|
16. | Q. Has the support for SafeArray been updated in the VM?
A. Yes. For more information, see the documentation for the SDK for Java 2.0x, and follow the link from Packages and
Classes to the com.ms.com package then to Class Variant or Class SafeArray.
|
17. | 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.
|
18. | 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
|