开发者

Java applet fails to load in Django dev environment (view variables not served to template)

I'm incorporating a test applet onto a new site I'm building using Django. When I load the page with the applet locally, I receive a "class not found" error.

java.lang.ClassNotFoundException: HelloWorld
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: HelloWorld

Applet HTML:

<applet code="HelloWorld" archive="{{ STATIC_URL }}HelloWorld.jar" width=525 height=635>
</applet>

My site root on the development machine is set up as C:/www/django/mysite. My Django settings are:

ABSOLUTE_PATH = lambda x: os.path.join(os.path.abspath(os.path.dirname(__file__)), x)
STATIC_ROOT = ABSOLUTE_PATH('media/')
STATIC_URL = 'media/'

The applet JAR is stored in the /media/ folder. I believe I have Django's settings correct because if I point my browser to http://localhost:8000/media/helloworld.jar it asks me to download the java file. When I load the page with the embedded applet, however, I receive the error. I think the browser is unable to find the actual jar file, but I'm not sure. I have tried a variety of alterations (add ".class", add package prefix, etc.) to no avail. I also opened the JAR file to make sure the class-file is 开发者_如何学JAVApresent - it is.

I've tagged this question with java and django because I'm not sure where the issue lies. Thank you for your time!


Your class seems to be in the root package (no package specified). This will not necessarily work and it's a bad practice. Define a package (e.g. com.company.applets) and place your applet class in this package.


I discovered that the {STATIC_URL} variable wasn't being served to the template. Here are the steps I followed to get my applets working:

First, I Added template context processors to setup.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.contrib.messages.context_processors.messages")

I Added a RequestContext to my views. This supplies the template with my variables:

from django.template import RequestContext
...
return render_to_response(
      'empirical/empirical-outline-tool.html',
      form_data,
      context_instance=RequestContext(request)
      )

Finally, I edited my url patterns to serve static files while in development mode:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
....
urlpatterns += staticfiles_urlpatterns()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜