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.
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.
Related
I'm new to Java programming and it seems to me like they are making this Applet signing more difficult than it needs to be...no website is straightforward on how to do it, and it gives the feeling that it would be time-consuming.
The problem is that I keep getting the "access denied" message when I try to run images on my Applet using the drawImage function.
I was told Class.getResourceAsStream would help, but I am having a hard time trying to find information on how I would implement this precisely for an image. There is also the issue that I am using NetBeans, some of this information that I find is out of my league of understanding or just doesn't fit what I need it for.
This is the kind of program I am trying to run:
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image img;
public void init()
{
img=getImage(getDocumentBase(),"/Image.jpg");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(img,20,20,this);
}
}
And this is the HTML file I am running it from:
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
<applet code="JavaProject.class" width="500" height="600">
</applet>
</body>
</html>
It works with shapes just fine, but I want to implement images like this and this is where I encounter the issue. Basically, I just want to see how Class.getResourceAsStream would fit into that code there, because that code above gives me the "access denied" error.
Also, something to note is that I am running this from the file system, not from HTTP.
If you're using Java 7 you need to sign applets now:
Most significant is a new requirement that all Java applets and Web
Start Applications using the Java plug-in to run in browsers be signed
with a trusted certificate for the best user experience. Java
supports code signing, but until Java SE 7u21 it was an optional
feature. Application code signing provides numerous security benefits
to users.
http://www.oracle.com/technetwork/java/javase/tech/java-code-signing-1915323.html
However, you can change the security settings through the Java Control Panel. (On Windows just Start > Java > Configure Java.) In the Java Control Panel, go to the security tab and set the Security Level to the minimal (medium). I believe this allows you to run an applet after only a security prompt.
I wrote http://pastebin.com/EwShF3YS for school; It's a simple java applet with a GUI pair of eyes that watches the cursor as it moves. It runs well in a compiler (NetBeans 7.4) , but for the life of me I'm having difficulty understanding why it doesn't run in a browser. I'm getting ClassNotFound exceptions. All the reading I've been doing suggests that a .class file isn't required because it's an applet, the browser should generate one at runtime. And if a class is required, how come NetBeans can run it without one?
The HTML file is stored in the same directory as the .java - and it's real simple:
<html>
<head>
<title>WatchMe</title>
</head>
<body>
<applet code="WatchMe.class" width="300" height="200">
</applet>
</body>
</html>
I've tried a variety of different things, different browsers, setting classpath; opening it on different computers. The result is always the same, ClassNotFound exception. The internet research I've been doing yields mostly unproductive answers, such as one person who completely reloaded his PC. I've noticed that if I fully qualify the path to WatchMe.class I get a hang/blank browser page. I'm completely out of ideas, so any suggestions or advice is welcome.
You need to have the compiled WatchMe.class in the same directory as HTML File. Having the .java file is not enough and no the browser will not compile anything for you. It will just run the .class file using the JRE.
All the reading I've been doing suggests that a .class file isn't required because it's an applet, the browser should generate one at runtime.
This is incorrect. You must provide either a ".class" file, or JAR files containing ".class" files for the browser to fetch.
A web browser's Java plugin is not capable of compiling Java code. If you've found a resource that tells you otherwise, it is WRONG. (I'd be interested to see concrete examples of this misinformation! Can you post the URLs?)
It is possible that your confusion arises from reading material on Javascript and thinking that it applies to Java. Don't!! They are very different languages ... and material on one does not apply to the other.
OK so why is your example not working?
It is hard to say, but the most likely reasons are:
you've used the wrong name or path in the "code" attribute,
you need a "codebase" attribute to allow the browser to map the ".class" name to the correct URL for downloading it,
your code depends on other classes (that are not in the browser plugin's class library), or
it is a bytecode version issue; i.e. you have compiled your applet with a later version of Java than is supported by the browser.
Try looking in the browser's Java console for the complete error message and stacktrace for the exception.
Try looking at the server-side HTTP logs to see what files that the client is attempting to fetch ... and what the server's response is.
I am having a problem putting a java applet on a website using Dreamweaver CS5.
This is the page where I am having the problem:
http://www.typingsquad.com/employment.html
This is a picture of what I see in dreamweaver:
http://i895.photobucket.com/albums/ac157/daltonward1/javaAppletProblem.png
Based on what I have read on the internet, I have tried the three following methods, but only one has worked.
First, I tried putting the applet on the webpage by using media-applet. The picture posted shows how I tried to implement that. However, as you can see on the website when viewing the first box, it states that there is an error.
"java.lang.ClassFormatError: Incompatible magic value 1013478509 in class file Calculator/html"
I've done some reading on this error, but I cannot find a solution.
The second thing I tried is using an IFrame using the direct link of the html file on the server. As you can see, all that appears is a blank box.
The third thing I tried using was a hyperlink, which actually did work. No errors, but its not the effect I was going for.
Does anyone see anything wrong with my use of Iframe and/or applet on dreamweaver?
Could it be that my files aren't grouped together right on the server?
I'm really into writing Java applications and applets, but my friend just got into web development and has asked me to write some simple programs for him. Any help will be greatly appreciated and sorry if my questions could have been simply found else where as I am still new to computer programming as well.
Thanks,
James Ward
java.lang.ClassFormatError: Incompatible magic value 1013478509 in class file Calculator/html
This means the JRE is asking for a class but getting an HTML page (probably stating '404, resource/page not found') that starts with <htm. More details on the answer at the OTN.
Now I look at the source and see:
<applet code="Calculator.html" codebase = "Java Applets/Calculator/bin" width="500" height="300">
</applet>
I realize it is because the page is specifying an HTML instead of a class in the code attribute. These things do not work by magic, you need to have some idea of what you're doing if you hope to deploy an applet successfully.
If http://www.typingsquad.com/Calculator.html works as you expect, the fix will be to change the applet element in the original page to..
<applet code="Calculator.class" width="500" height="300">
</applet>
I am guessing the codebase listed in the original HTML is an artifact of your development environment. If not, you will need to add that, but if that is the case, I would strongly recommend using only directories names with:
All lower case letters.
No spaces.
i saw the html from the site.. you are saying
<applet code="Calculator.html" codebase = "Java Applets/Calculator/bin" width="500" height="300">
</applet>
you should be saying
code="Calculator.class"
the issue is that the system is getting a HTML document which is not parsable as a java class file.. just change the applet tag and things will work properly.
I'd recommend you creating a jar file and using the JAR as the codebase.. its simpler and better than having a class file
Is it possible to process JavaScript in a Java application? Possibly utilizing WebKit libraries, or whatever browser libraries use to process JavaScript? A use case would be - how, in Java, can I determine the possible links this web page would go to?
<script>
function goToLink(){
if(1==1){
window.location='www.somesite.com'
} else {
window.location='www.nevergethere.com'
}
}
</script>
<html>
<a href onClick='javascript:goToLink()'>CLICK HERE!!</a>
</html>
Typically, you would just search all of the code for a link regular expression, but you will never actually have the chance of going to: 'www.nevergetthere.com'
I've had some luck in tracing down JavaScript-based page links with HTML Unit. It basically acts like a browser that you have access to inside a Java program, so you can simulate a click on a link, and then figure out where it goes.
You might be looking for Rhino.
If the objective is to look at a website, without knowing beforehand what the javascript or HTML will look like, and try to figure out where you would end up by clicking on various anchor tags, you could use something like WebDriver to actually load up the page in a browser (either real or virtual), click on various DOM elements, and see where you end up.
Web scraping is tricky business, though. There are a hundred little things that could make your code not read the page correctly. A hundred little expectations you may have that the website in question might not abide by.
Related question I am having similar problems to what that guy had in his. My upload server returns a redirect.
Specifically, I am not sure what FormPanel.SubmitCompleteEvent.getResults() returns. Sometimes, I get html of an img:
<img style="cursor: -moz-zoom-in;"
alt="http://<myapp>.appspot.com/servePic?blob-key=abcdef"
src="http://<myapp>.appspot.com/servePic?blob-abcdef"
height="1" width="1">
Sometimes I get the image data in bytes. Behavior varies on I dunno what. I get the first in development, and the second in production.
Does anyone know anything about this?
Here's a pretty good example of how to save/load/resize images with gwt & app engine.
http://code.google.com/p/swagswap/source/browse/#svn/trunk/src/main/java/com/swagswap/dao%3Fstate%3Dclosed
Have a look in the dao implemenations and also the gwt interface packages.
Does the project in this link still exists?
http://code.google.com/p/swagswap/source/browse/#svn/trunk/src/main/java/com/swagswap/dao%3Fstate%3Dclosed
I tried to checked it out through eclipse but looks like it does not exist anymore.