Java Embedding Into HTML

2019-04-21 13:44发布

I'm sure this question has been asked a million times, but no matter how many Google searches I do I cannot get this working. I'm basically trying to get a project with multiple packages in it to be embedded in a webpage. I made a test program which just made some balls bounce around the screen and was able to get that running. I put the main class in one package and the ball class in another just to test it and it seems to be running fine. But the program that I actually need in a web page (just called FinalProject) refuses to do it.

The best thing I can get it to do is give me a blank screen, without giving an error but just white. If I try clicking where it should be nothing happens, I think because the applet is there but is just showing white so I can't see it. I did use the applet tag, which from my understanding is now depreciated but I need to turn this project in on a webpage just so the teacher can see it. We've already tested that other people's projects (which used the applet tag) work, so I was trying to stick with that for now and worry about getting it working on every browser afterwards. Though that could very well be the problem. Maybe it would work on his browser but not mine here. I've tried running my program on Google Chrome, Mozilla Firefox, and Internet Explorer with no luck.

Here is the HTML code:

<html>
<head>  

</head>  
<body>  
    <applet code = "main.FinalProject.class" width = "700px" height = "500px"></applet>  
</body>  
</html>

The HTML file this is written in is in [Eclipse Workspace]/FinalProject/bin/test.htm. The FinalProject.class file referenced in the HTML exists in [Eclipse Workspace]/FinalProject/bin/ main/FinalProject.class. The FinalProject.class file acts as the main class, so I'm pretty sure that's the one I need to run. It's the one with the init(), actionPerformed(), paint() methods and all that good stuff.

Currently I'm trying to run this offline on my computer, so there shouldn't be any net URL's I would think. I used Eclipse to write the Java code, dunno if that makes any difference. Unfortunately, the Java code is rather large, too much to reproduce here, if there's something specific you think is the problem I can look and post that small section.

A few of my friends managed to get theirs working, however they said they had to remove all their .png files (annoying but doable for my project). They also said had to remove all their mouse movement code. My program is kind of dependent on that, I need that for it to work at all. I know there MUST be a way to use all the MouseListener and MouseMoveListener code online, maybe it's a little different though. I dunno if that has something to do with this, but I figured I'd point it out just to be safe.

Any help here would be greatly appreciated.

1条回答
Luminary・发光体
2楼-- · 2019-04-21 14:38

Basically you're asking something like: How to deploy a java applet for today's browsers (applet, embed, object)?

Based on that, I think what you want is:

<object 
  classid="clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA"
  style="height: 500px; width: 700px;">
  <param name="code" value="FinalProject.class">
    <comment>
      <embed code="FinalProject.class"
        type="application/x-java-applet"
        height="500" width="700">
        <noembed>
          This browser appears to lack support for Java Applets.
        </noembed>
      </embed>
    </comment>
  </object>

Now, you have a filename of main.FinalProject.class in your code. It seems like FinalProject.class would be more likely. But yours could be right. In any case, this html file needs to be in the same folder as main.FinalProject.class or FinalProject.class and whatever classes may also be required.

Now, you may also need to make sure your browsers can actually run an applet. See: How do I enable Java in my web browser?


Update

Based on feedback from Andrew Thompson, the preferred solution is to use JavaScript from Oracle, like this:

<script src="http://www.java.com/js/deployJava.js"></script>
<script>
    var attributes = {
        code:'FinalProject.class',
    width:700, height:500} ;
    var parameters = {}; // does the Applet take parameters?
    var version = '1.6' ; // does the Applet require a minimum version of Java
    deployJava.runApplet(attributes, parameters, version);
</script>

This requires the ability to load arbitrary JavaScript, but you could also capture that deployJava.js and have that be local as well. Might be worth a look.

查看更多
登录 后发表回答