Java applet using Dreamweaver - java

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

Related

How to compile Java code online?

Making a site, comparing the performance of lambda expressions in Java 8 vs. Non-lambda expressions, using html and javascript. Got most of the site done, right now the example code gets displayed by using iframes that display the .java files, then I embedded java applets that run the .class files. What I'm not sure how to do is, how can I display the code in pre tags for example and then compile the code online? That way the user can modify source code to test different stuff such as stream() vs parallelStream().
What sort of code are you trying to put into a webpage? Usually when people try to embed java applets they use the applet tag for html.
<applet code="YourMainClass.class" archive="YourApplet.jar" width="800" height="600">
</applet>
Please correct me if I'm getting the wrong idea of what you are trying to achieve, but are you just trying to embed a java applet into a website? Or are you literally trying to compile code online?

ClassNotFoundException running simple applets

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.

Java Embedding Into HTML

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.

Java/JSP - How do I track down application-generated code?

I'm very new to Java and JSP.
I am working with a purchased Java web application. When I access the application in my browser, there is a file "mysite.com/app/servlet/com.sample.weblet.server.ClientReader?..." that contains a line of Javascript is erroring out in some browsers. I would like to find the source of that Javascript code so that I can modify it so it will be cross-browser compatible.
I've searched all of the JSP and JS files (which are all in a separate folder, and not packaged into JAR files), but could not find the faulty function.
I dug through the JAR files extensively. I only found class declarations, variable declarations, and empty methods. I have not been able to find any logic whatsoever, and definitely have not come across anything resembling javascript auto-generation. There are no WAR files.
I found com.sample.weblet.server.ClientReader in a jar file.. and it just contained a couple of empty methods, yet again.
I am assuming that this faulty JS code is auto-generated. Does that sound correct?
Is there like one main JAR file that has all of the logic? Would it have this JS code hard-coded into it? What am I missing?
Can anyone direct me, or give me any tips?
My suggestion is you should use firebug to detect the javascript error. If any error occurs, you'll see it under "console" tab in firebug and usually there's a link beside it, click on the link will bring you where the bad code resides.
Also, you can navigate javascript files the page has loaded by clicking the list button under "Script" tab in firebug.
Hope it helps.

Applet jar not being searched for files

For example purposes, let's say I have a series of Locations on a website and the urls are of the form /location/#/ where # is the id of the location I want to view. Since I'm using Django with Apache, all of my static content is in /media. Each Location page is trying to load a Java applet that allows for file uploads.
<applet
codebase="/media/java/"
code="com.elementit.JavaPowUpload.Manager"
archive="JavaPowUpload.jar, commons-logging-1.1.jar, commons-httpclient-3.1-rc1.jar, commons-codec-1.3.jar"
width="200"
height="100"
name="java-uploader"
id="id-java-uploader"
mayscript="true"
alt="JavaPowUpload by www.element-it.com"></applet>
All of the listed jar files are in /media/java/ and are found by the web server. The applet appears to load on the page without a problem but when looking at the network traffic during page load I see there are several errors. Basically the applet seems to be looking for files that are within the jar, say com.elementit.JavaPowUpload.Messages_en.class, but is asking the web server for them, which amounts to requesting /media/java/com/elementit/JavaPowUpload/Messages_en.class, which of course does not exist. Note that if I get rid of codebase and give the full path to each jar, I still have a similar problem where the request is then /location/#/com/elementit/JavaPowUpload/Messages_en.class. How do I set things up so that the jar file is searched rather than the filesystem?
See the codebase_lookup applet attribute.
Despite taking this code from another page on a different server, it appears there is a slight error in the applet's code attribute. Adding .class to the string fixed my problem, but I'm unsure why it works without it on the other host and page.
code="com.elementit.JavaPowUpload.Manager.class"

Categories

Resources