Applet不会显示在浏览器中

时间:2011-07-07 21:21:04

标签: java jar applet

我有以下小程序:http://s1.picofile.com/file/6922766596/Game.jar.html

和jar之前的applet:http://s1.picofile.com/file/6923855130/Game_III_Applet.7z.html

我在HTML页面中将其嵌入如下:

<applet code="FrameS.class" archive="Game.jar" width=1000 height=800></applet>

但是,我的MSIE浏览器中没有显示任何内容。这是怎么造成的,我该如何解决?

3 个答案:

答案 0 :(得分:1)

Caused by: java.security.AccessControlException: 
    access denied (java.io.FilePermission back.jpg read)

小程序应使用URL来访问其资源。资源可以位于applet的运行时类路径(即存档属性中提到的)中的Jar文件(例如4.7 meg Game.jar)中,也可以放在与applet相同的服务器上(或者applet的代码库)。

在第一种情况下,可以使用..

形成URL
URL urlToPic = this.getClass().getResource("images/back.jpg");

..图片位于Jar的images路径中。

在第二种情况下,可以相对于applet的代码库或文档库形成URL。 E.G。

URL urlToPic = new URL(this.getDocumentBase(), "images/back.jpg");

..图像位于包含applet的目录的images子目录中。

接受File参数的方法通常也会接受InputStreamURL

答案 1 :(得分:0)

当您只提供.jar

时,我们无法对其进行测试

请提供更多,或通过查看java错误控制台

自行解决

例如我得到了这个例外,说我缺少back.jpg图片资源

java.lang.reflect.InvocationTargetException
at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(DeployAWTUtil.java:116)
at sun.plugin2.applet.Plugin2Manager.runOnEDT(Plugin2Manager.java:3542)
at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3073)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1498)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission back.jpg read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:871)
at sun.awt.SunToolkit.getImage(SunToolkit.java:885)
at apple.awt.CToolkit.getImage(CToolkit.java:934)
at FrameS.<init>(FrameS.java:59)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.plugin2.applet.Plugin2Manager$13.run(Plugin2Manager.java:3061)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:677)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:647)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception: java.lang.reflect.InvocationTargetException

答案 2 :(得分:-1)

在IE中嵌入applet时请使用object标签,这是一个php函数,它根据浏览器输出applet嵌入代码,你会看到不同的浏览器处理方式不同 -

function getApplet($jarFile,$className,$params = array(),$width=1,$height=1,$name='japplet')
{
$retVal = "";

$useApplet = 0;
$user_agent = $_SERVER['HTTP_USER_AGENT'];

if (stristr($user_agent, "konqueror") || stristr($user_agent, "macintosh") || stristr($user_agent, "opera")) {
    $useApplet = 1;
    $retVal = sprintf('<applet name="%s" id="%s" archive="%s" code="%s" width="%s" height="%s" MAYSCRIPT >',$name,$name,$jarFile,$className,$width,$height);
} else {
    if (strstr($user_agent, "MSIE")) {
        $retVal = sprintf('<object  name="%s" id="%s" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" style="border-width:0;" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4_1-windows-i586.cab#version=1,4,1"  width= "%s" height= "%s">',$name,$name,$width,$height);
    } else {
        $retVal = sprintf('<object  name="%s" id="%s" type="application/x-java-applet;version=1.4.1" width= "%s" height= "%s">',$name,$name,$width,$height);
    }

    $params['archive'] = $jarFile;
    $params['code'] = $className;
    $params['mayscript'] = 'true';
    $params['scriptable'] = 'true';
    $params['name'] = $name;

}

foreach ($params as $var=>$val)
{
    $retVal .= sprintf('<param name="%s" value="%s">',$var,$val);
}

$retVal .= 'It appears you do not have Java installed or it is disabled on your system.<br />
                Please download it <a href="http://www.java.com/getjava/" class="link" target="_blank">here</a>';
if ($useApplet == 1) {
    $retVal .= '</applet>';
} else {
    $retVal .= '</object>';
}

return $retVal;
}

在嵌入applet时,您需要使用对象标记。