我相信IDE通常会使用appletviewer来启动applet,但是会使用不受限制的安全策略(从命令行启动时appletviewer会被沙盒化)。
要从命令行启动小程序查看器,请尝试以下操作:
prompt> appletviewer TheApplet.java
特别要注意的是,小应用程序查看器现在不直接将HTML作为小程序查看器的参数来提供参数,而是将解析小程序元素直接从源中解析出来。
/* <!-- Defines the applet element used by the appletviewer. -->
<applet code='HelloWorld' width='200' height='100'></applet> */
import javax.swing.*;
/** An 'Hello World' Swing based applet.
To compile and launch:
prompt> javac HelloWorld.java
prompt> appletviewer HelloWorld.java */
public class HelloWorld extends JApplet {
public void init() {
// Swing operations need to be performed on the EDT.
// The Runnable/invokelater() ensures that happens.
Runnable r = new Runnable() {
public void run() {
// the crux of this simple applet
getContentPane().add( new JLabel("Hello World!") );
}
};
SwingUtilities.invokelater(r);
}
}
当从命令行启动时,小应用程序查看器将具有比应用于浏览器中部署的小应用程序更严格的沙箱。
我强烈推荐Appleteer,它是appletviewer的替代方法,因为它比appletviewer更好(我写过它;)。