i created a game in applet and i want to put it on a site. the problem is that my games has alot of classes. how can i use applet tag to up it online?

java.php

 <html>
   ...
       <applet code = "Main.class" width = 900 height = 400> </applet>    
   ...
   </html>

my folder tree

-java_game
    -java.php
    -main.class
    -item.class
    -background.clas
    -enemy.class
    -...
    -music
         -...
    -image
        -...

Dani AI

Generated

Short summary: applets are effectively obsolete in modern web browsers (NPAPI was phased out) and Oracle has deprecated the browser plugin, so long‑term hosting of a classic Applet is fragile; still, immediate fixes are possible to get an existing game running from a web server. (blog.chromium.org)

As noted, keep the same folder/layout and use relative paths. For deployment, bundle all .class files plus images/sounds into one (or a few) JARs and use the applet archive attribute so the browser downloads a single archive instead of many .class files. Example HTML wrapper (archive and code must match package/class layout):

<applet code="com.mygame.MainApplet" archive="game.jar" width="900" height="400"></applet>

The applet caching/archive semantics are documented in the Java plug‑in guide. (docs.oracle.com)

Why the background shows but sprites/animation do not — focused checklist (common causes seen in migration from an app to an applet):

  • Resource loading: images/sounds loaded via local file paths or incorrect classpath will fail silently. Prefer loading embedded resources with getClass().getResource(...) or getResourceAsStream(...), or getImage(getCodeBase(), "images/player.png"). Initialize images inside init() (not as field initializers). (flylib.com)

  • Lifecycle/threading: a game loop started from main() won’t run inside an Applet. Start the animation thread in start() (and stop in stop()). Minimal pattern:

    public void start() {
    if (animThread == null) {
      animThread = new Thread(() -> { while(running){ update(); repaint(); Thread.sleep(16); }});
      animThread.start();
    }
    }
  • Permissions / security exceptions: unsigned applets can be blocked from threads/resources. Enable the Java Console and re-run to catch stack traces / security errors; browser dev tools can show 404s for missing assets. (java.com)

Quick checks: inspect the JAR with jar tvf game.jar to verify paths match package names, confirm server is serving the files (check 404s and case sensitivity), test a tiny “moving rectangle” applet to isolate rendering vs resource problems, and consider packaging/signing the JAR if privileged operations are required.

Recommended Answers

All 2 Replies

Please read oracle tag embeded. In your website, you need to structure (folders & file locations) similar to whatever in your development. If your main class is in the same location as your page, you need copy exactly the same folder structure and file location on to your web server location. One thing, you must NOT use absolute path in any of your code or your web page. You must use relative path only or you will have a hard time porting it from development environment to anywhere else.

dc531e72750853ec65370adb2eaafb55
here is my folder where there are all my java classes, images, and sounds. they all in one folder. when i run in web i get this error:

it is kind of working now. erros are gone and i can see the background of my applet. but the problem is that its not moving and i cant see my player, enemy or items. any idea why this is?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.