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.
Related
I just finished making a Java applet and it works on my localhost server, however, I want to upload it to my blog on wordpress. How should I go about doing that?
I've never done it before, it's my first time, so help is greatly appreciated. Do I upload my .class files to wordpress, or do I upload them somewhere else and link them in my HTML file? If I do upload them to a different server, I'm not sure how to link to them. If anyone knows of a tutorial or is willing to tell me how to go about doing this it would be much appreciated.
I'm not sure you can upload applets to that site, or include applet elements, but if you can run scripts and hot-link to it elsewhere, it should not be a problem. The script to use is the Deployment Toolkit Script.
Do I upload my .class files to wordpress, ..
Don't upload loose class files. Very soon all applet code will need to be digitally signed in order to not be automatically rejected by the Java Virtual Machine. To be digitally signed, code must be in a Jar file.
..or do I upload them somewhere else and link them in my HTML file?
If you need to go with 'somewhere else', it should just be a matter of specifying the codebase to point to the site. Here is the example from the Using Applet Tag Attributes of that document linked above.
<script src="https://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {codebase:'http://sample-url/Java2D',
code:'java2d.Java2DemoApplet',
archive:'Java2Demo.jar',
width:710, height:540} ;
var parameters = {fontSize:16, permissions:'sandbox'} ;
var version = '1.6' ;
deployJava.runApplet(attributes, parameters, version);
</script>
Note that I altered it slightly in the code attribute, because it irritates me to see Oracle itself perpetuating a myth that adding the file extension is a good idea. The code attribute should be the Fully Qualified Name of a class, rather than the file name.
If anyone knows of a tutorial..
I know a lot of good resources about applets, and I included links to them in the Applet tag info. page here at SO.
All the best. The (robust) deployment of a simple applet can be more tricky than the development of it - you have a steep learning curve ahead.
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.
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
Kind of hard to explain in one line but my problem is essentially like this:
I made a java applet that I want to run on a web page that I packaged into a .jar file. I'm able to get the applet working fine using the <applet> tag but the problem is, if the user views the page source, they will see:
<applet archive="directory/program.jar">
Assuming .jar files can be easily opened and all the class files decompiled, all the user would have to do is go to www.url.com/directory/program.jar to download my .jar and they would have all my source code :(
So I'm wondering if there is either a way to protect my code/jar from being decompiled (other than obfuscation) or to use some kind of server-side script to feed the contents of the .jar directly to the browser from a server-side location not publically visible.
Any help is appreciated.
This is fundamentally impossible.
Java applets run the client.
Anything that runs on the client can be disassembled and modified by a sufficiently advanced user.
You should move your sensitive logic to the server and invoke it using HTTP requests ( and remember that the user can use Fiddler).
While you're at it, you should probably replace your applet with HTML and Javascript.
Other than obfuscation or encryption, no--one way or the other, the browser will have access to the jar.
You might be able to create an applet that loads more functionality at runtime.
There is no effective way to block access to the source code of any page; for the page to be readable by browsers and search engines, the source code has to be accessible, and therefore can be viewed and/or copied. That's just how the web works. HTML is sent as a text document and interpreted client-side.
Disabling the right-click is little more than an annoyance, and it works sporadically in alternative browsers. Even if you succeed, the View Source option in the menu is always present. The viewer could also use a download tool such as Wget, or even get the page from the Google cache without visiting your site at all.
Edit: Oops! I misunderstood your question. You should follow #SLaks advice and "move your sensitive logic to the server and invoke ot using HTTP requests ( and remember that the user can use Fiddler)."
While quantum mechanics do rule the universe, they have less of a grip on your code than you might suspect. You cannot both deploy code to the client browser and not deploy code to the client browser. You have the option of doing one or the other.
You can prevent direct browsing to your .jar file by locating it beneath the WEB-INF directory in your WAR file. This will also prevent <applet archive="directory/program.jar"> from working.
Once the jar is beneath the WEB-INF directory you will need something to feed the resource to the client browser; the Spring resources servlet is good for this (If you are using Java and Spring). I feel confident that other such tools exist. With the Sprint resours servlet, your would deploy your applet with something like this: <applet archive="resource/program.jar".
If you write your own resource distributor, you can add security to make it harder to get the jar file; perhaps add a header to your requests like IRGud: <user_id here> and fail any request that does not have that header (or acceptable contents in the header).
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"