*
The moose likes Applets and the fly likes Applet works in Eclipse but not browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet works in Eclipse but not browser" Watch "Applet works in Eclipse but not browser" New topic
Author

Applet works in Eclipse but not browser

Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 131

I have an applet, very simple, on the click of a button it reads a URL (provided by another class), and then loads that URL.
It works just fine in the Eclipse applet player and reads the URL from the other class. But, when I run the same applet in a browser, it can't find the URL and returns null.

What's going on? Do I need to do something special with the html file to allow it to locate the other class?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Is it perhaps throwing a security exception because you asked the applet to do something beyond the sandboxed operations it can do by default? Like trying to connect to a host other than the one the applet was downloaded, for example?

Or perhaps it really isn't able to download this mysterious "other class" and you're getting an exception for that reason?

So the first thing you need to do is to open the applet console and see what you've got there when the problem arises. Chances are you'll see a stack trace there. That's the place to start from.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

Perhaps you missed out the relevant part of my reply to your other question here
Maneesh Godbole wrote:One possible way to do it would be have a JEditorPane embedded in your applet which you load with the yahoo query page.
Another would be to explore if yahoo exposes a search API which returns JSON or XML or any other format which your applet can consume. Then parse the data and display in whatever way you wish. Of course you will have to overcome the sandbox restriction, because "an applet can only dial home"

Check out this FAQ to understand whats happening and how to resolve it.

But before anything else, copy paste the error message on your console like Paul recommended


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 131

I couldn't get the Java console to work in firefox. Here is the code I am using, first the URLReader class. It takes a URL (line 8) which is a Yahoo search for
reviews of the said game. It then loads any hits which match the Regex expression (any hit containing the word 'organic' - line 17), into String [] hits.



The next class is for the applet itself. It takes the URL variable 'url' and on the click of a button, opens that url.



When I set the URL manually, with for example 'URL url= new URL("http://www.metacritic.com/game/pc/organic-panic");' (see line 25) it works in the browser.
But when I use a URL from the String array I mentioned above (see line 24), it doesn't work, and returns null. Why is this?
As I mentioned before, both of these patterns (line 24 and 25) work in eclispe, but only line 25 works in the browser.

Is it to do with sandbox restrictions? Not sure, I added a '<param name="permissions" value="all-permissions" />' to my html file, to hopefully overcome this.

This is my first applet, it's a volunteer job for a game's website. The developers are good friends of mine, I asked them if there was anything
I could do to contribute. It's pretty much non-critical for them, just something for my resume. I appreciate your help.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

The console you need to check is not attached to any browser, but is a part of your JDK installation.
http://www.java.com/en/download/help/javaconsole.xml
Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 131

Actually, I'm using Linux. I checked the corresponding Linux page, but the procedure didn't work for me. I've been struggling with it all afternoon (I'm in Japan). I may just boot up into Windows and try it from there.
Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 131

OK, I got it.............

Java Plug-in 10.7.0.10
Using JRE version 1.7.0_07-b10 Java HotSpot(TM) 64-Bit Server VM
User home directory = /home/billy
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.security.AccessControlException: access denied ("java.net.SocketPermission" "search.yahoo.com:80" "connect,resolve")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at URLReader.go(URLReader.java:9)
at URLApplet.actionPerformed(URLApplet.java:20)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

This is the sandboxing issue. Please refer to the FAQ link I provided in my earlier post for the solution.
Essentially you need to ask your end user permission to override the built in security features implemented by applets (which include, among others, restrictions on connecting to sources other than where the applet originated)
Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 131

Ok, thanks! I'll repost when I've solved it!
Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 131

I followed the steps on this website to sign my applet:

https://eyeasme.com/Shayne/SECURITY/appletSecurity.html

I also put the troublesome code (line 20 URLReader's go() method) inside an AccessController.doPrivileged method.

All classes were added to a jar file (and then the jar was signed). I then altered my html file to point to the URLApplet class inside the jar.
When I ran the applet in the browser, I had that exact same issue as last time. I seem to be getting the
"java.security.AccessControlException" even though the applet is signed and the troublesome code tucked
inside an AccessController.doPrivileged method.

The only other option left on the FAQ link you gave me is to use a policy file, but thats not really what I want.

Here is the console output:

java.security.AccessControlException: access denied ("java.net.SocketPermission" "search.yahoo.com:80" "connect,resolve")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.SecurityManagerHelper.checkConnectHelper(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at URLReader.go(URLReader.java:9)
at URLApplet$1.run(URLApplet.java:23)
at java.security.AccessController.doPrivileged(Native Method)
at URLApplet.actionPerformed(URLApplet.java:22)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Any ideas what is going on?

By the way I am running the applet from a file on my local HDD.
Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 131

I figured out what was wrong. I made a typo in the HTML file and misspelt the name of the jar file. I was actually running an older jar that was created before I implemented all of your advice.

Running smoothly now! Thanks for your help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I'm glad you got it running. Good work -- quite often things like the browser caching an old version of the applet tie people's brains in knots.

However I noticed in your other thread that the example you were working from was on a web page dated 1998. That's 15 years ago. Back in those days applets were a big deal and they would show up near the beginning of books which claimed to teach you Java. But these days applets are a dying technology. People just aren't using them for real-life work except in very specialized situations. So I wouldn't recommend studying applets as a way to learn Java these days.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet works in Eclipse but not browser