Java Embedding Into HTML
Asked Answered
T

1

13

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.

Thurnau answered 9/6, 2012 at 21:24 Comment(15)
I assume that you declared the class FinalProject to be in package main inside the java code? You could try to move it to the default package to check whether the path is the problem.Absolutism
FinalProject is in the main class. I did not physically move it to that location, I created a new class in Eclipse and it put it under bin/main/ which I assume is the right location. Moving it into default package and changing the HTML code I had originally as well as artlung's to the new location doesn't seem to have any affect.Thurnau
Oh whoops, I said FinalProject is IN the main CLASS. That doesn't really make sense. I meant FinalProject IS the Main class, and is also IN the main package. That was intended to be 2 sentences, somehow I combined them.Thurnau
I'd suggest using javascript instead of Java. It will be much easier and a lot faster.Otology
What problematic results are you receiving? If you're getting a NoClassFoundException, you might want to try specifying the archive attribute. Furthermore, the applet tag is deprecated and isn't officially supported in Chrome or Opera (without extra plugins and extensions installed) according W3's applet tag documentation. w3schools.com/tags/tag_applet.aspIncorrigible
Is all your code in a single class file? Your description makes it sound liker it's not, and you don't have the "archive" attribute set on your <applet> tag, so it may be that it's just not sure where to find your code.Scythe
"I'm basically trying to get a project with multiple packages in it to be embedded in a webpage." Why? It would be more common these days to launch a frame from a ink using JWS.Tater
@Vulcan - I do not know what error I am getting, since I'm only getting a blank screen I can't look up the Java console (unless there's another way I don't know).Thurnau
@Thurnau You could log the console to something on the server such as a database or file, but that might be more work than necessary. Did you try specifying the archive attribute of your applet tags?Incorrigible
@Ian McLaird - There are multiple classes in multiple packages. I have tried setting the archive attribute to a FinalProject.jar file I exported from Eclipse, but that doesn't seem to change anything, and I don't really know what to do with it. Andrew Thompson - This is a project for a class I'm taking. I'm required to embed it in a web page, I don't really much to say about how I turn it in, that's up to the teacher.Thurnau
@Vulcan - I did play with the Archive attribute, putting it to a FinalProject.jar file I exported from Eclipse, but I admit I don't really know what I'm doing there. It didn't change anything, though I don't know if I exported it right or using it correctly to begin with. I did notice in the .jar file, all the code was compressed in there, but none of the images existed in it. Is it supposed to be like that, or did I screw up somewhere?Thurnau
@Thurnau If you export it correctly, all images within your project will be within the jar. There might be an option in eclipse to enable/disable non-java file compressing, but I don't know for sure; look into that.Incorrigible
@Vulcan - I did another check in Eclipse and I found the option to export all files into the .jar and that worked fine. Didn't really change anything though, still getting blank screens. I did notice that in the Java icon in the taskbar, when you right-click it you can show the console, even if it doesn't report an error. I did it and it said that Java did not have permission to read one of the images (first one loaded). I commented that one out to test, and it threw the same error on the second image loaded. Is there some special kind of permission setting it needs to have to display images?Thurnau
I'm not sure about what permissions you would need to set, but how are you loading the images? Images in applets should be loaded using Applet#getImage(URL, String), where the URL is your applet's code base and the String is your image's resource path.Incorrigible
@Vulcan - That's how I loaded them. The syntax for each was imgSomeImage = getImage(getDocumentBase(), "images/imageName.gif"); However I did use ImageIO.read() so I could get the dimensions of the images, which I needed in the program, that might have something to do with it. I don't recall ever seeing anything about permission with that syntax though. Would that mean the browser is blocking it from running, or is it something in Eclipse that I would need to do?Thurnau
S
7

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.

Sherasherar answered 9/6, 2012 at 21:32 Comment(15)
Thanks for the quick response. However Chrome and IE show no change, it's still just a blank screen on both. Firefox was a bit weird, it said I needed a different plugin (though other projects tested did and still do work fine) and when I told it to check it couldn't find anything. I Googled for the latest version for Firefox, downloaded and installed it, but it still says I need another plugin. Not sure what to do from there. Note: None of the browsers displayed the "This browser appears to lack support for Java Applets." message.Thurnau
My Chrome and Safari have the code in this answering working on MacOSX. Firefox initially failed, but I updated the type in the embed tag and now it works. You might also look at: java.com/en/download/help/enable_browser.xmlSherasherar
It says it is enabled, and I've never had any trouble with any other Java applets online. Admittedly, it's pretty rare I use one, but still it's always worked fine. All the browsers keep popping up with the "Are you really sure you want to run this? It could explode your computer!" whenever I try to load it, so I know it's not disabled.Thurnau
I think you want to make sure your class is named FinalProject.class -- so in your code you should have had: public class FinalProject extends Applet I believe. Are you sure your applet will actually run?Sherasherar
The program runs fine in Eclipse, and the file is named FinalProject.class. The entire declaration line is public class FinalProject extends JApplet implements ActionListener, KeyListener, MouseListener, MouseMotionListener {. I dunno if the JApplet has anything to do with it.Thurnau
You might also look for a log on your Java console. I made a quick HelloWorld applet and gist.github.com/2903608 is what the output to my Java console is when it runs in Firefox.Sherasherar
I would post the log, but unfortunately it only displays a blank screen. It doesn't throw an error on neither your code, nor mine. I know the applet is there however, it does ask me to allow it and I see the gray box asking permission to run before I say "Yes". Also when it is blank, if I right click on it, no context menu comes up, which I assume is because Java controls the right clicks, and it's not working so nothing happens. Since it doesn't show an error, I can't get the log, unless there's another way besides clicking on the error message.Thurnau
The log typically is not in the browser, for me it gets spawned when I run an applet. I'm not sure. Maybe try JApplet should not be a problem, I don't think. Try looking over: docs.oracle.com/javase/tutorial/uiswing/components/…Sherasherar
Their code didn't fare any better, still a blank screen throughout. Though I don't know what to set most of their code to. I don't have a separate .jar with the images. And actually there are no images in the .jar I compiled, but I haven't linked to it in any of the HTML I've been doing so I don't think that's the problem. The Java code directly links to all the images, so I wouldn't think I would need to set that in the HTML, perhaps I'm wrong on that assumption though.Thurnau
Sorry could not be more helpful.Sherasherar
Thanks anyways for your help, you did a lot more than I would've had I been in your situation. At least I know it's not me being a complete idiot.Thurnau
"How to .. for today's .." The problem with such advice is that it could be wrong 'tomorrow' - & that thread is from 2009. The preferred way to launch applets in this day & age is using the deployJava.js (briefly mentioned in that thread) linked from the applet info. page (yes, it has improved since 2009).Tater
@AndrewThompson - so, this page: docs.oracle.com/javase/6/docs/technotes/guides/jweb/… ?Sherasherar
Yes, that page describes the script.Tater
@AndrewThompson thanks, updated my answer to include that option.Sherasherar

© 2022 - 2024 — McMap. All rights reserved.