Hi all! I've come very close to finishing this applet of mine but I'm stuck on one last part.

The main purpose of this applet is just to fetch some data from a URL (read in 1 line, parse it, and display the data.) After starting this project, I found that applets couldn't access URLs unless they were signed applets. So after completing the applet the way it's meant to function, I signed the applet and it's still giving me errors. When I try loading the applet on a basic HTML page, it gives the Java symbol with the circling loading symbol around it but then it freezes.

I assume it has something to do with my URL function. Any help would be much appreciated, it's been giving me a huge headache ><

The problem lies within the Start() function. So you can skip down right to the problem. I figured I'd paste the whole program if anyone wanted to read through all the code. I know this function works outside of an applet so is there a way to fix this?

import java.util.*;
import javax.swing.JApplet;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.Desktop;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Ranks extends JApplet{
	
	//Declare varibles
	private String worldRank, realmRank;
	private JLabel worldRankLabel, realmRankLabel,tierNormalLabel, tierHeroicLabel, guildLogo;
	private JComboBox tierNormal, tierHeroic;
	private BufferedImage logo;

	
public void init(){
		Container contents = getContentPane();
		contents.setLayout(new FlowLayout());
		
		String[] bossesNormal = {"Morchok", "Yor'sahj", "Zon'ozz", "Hagara", "Ultraxion", "Blackhorn", "Spine", "Madness"};
		String[] bossesHeroic = {"Morchok", "Yor'sahj"};
		
		realmRank = new String();
		worldRank = new String();
		
		tierNormal = new JComboBox(bossesNormal);
		tierHeroic = new JComboBox(bossesHeroic);
		
		//Set logo to guild image
		try{
			logo = ImageIO.read(this.getClass().getResource("FD_Logo.png"));
			guildLogo = new JLabel(new ImageIcon(logo));
		}
		
		catch(IOException io){
			guildLogo = new JLabel("Error Loading Image");
		}	
				
		realmRankLabel = new JLabel("Realm Rank:      " + realmRank);
		worldRankLabel = new JLabel("World Rank:     " + worldRank);
		tierNormalLabel = new JLabel("Normal kills");
		tierHeroicLabel = new JLabel("Heroic kills");
	
		
		contents.add(guildLogo);
      contents.add(realmRankLabel);
		contents.add(worldRankLabel);
		contents.add(tierNormalLabel);
		contents.add(tierNormal);
		contents.add(tierHeroicLabel);
		contents.add(tierHeroic);
		
		
		
		getContentPane().setBackground(Color.white);
		setSize(185, 200);
		Start();
	}
	
public void Start(){
	
	try{
	System.out.println(worldRank);
	  URL url = new URL("http://www.wowprogress.com/guild/us/elune/From+Dust/json_rank");
 	  BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

     String inputLine;
	  String data = null;
		// Set data = to the only line in the input stream from the URL
		data = (inputLine = in.readLine());
		
		worldRank = data.substring(28,32);
		realmRank = data.substring(62,63);
	}
	
	catch(IOException io){}
	
	
}



}

Thanks in advance!

Are there any error messages in the browser's java console?

Unfortunately there aren't. It only ever gets to the loading symbols and then the loading symbols freeze.

However, I do get these error messages from the compiler at runtime using my IDE: java.security.AccessControlException: access denied ("java.net.SocketPermission" "www.wowprogress.com:80" "connect,resolve")

After looking into these issues about a week ago, I found that the JAR file needed to be signed and it would allow my method to execute, but it doesn't ever run.

Oh, and here is my HTML code if you would like to give it a shot for yourself.

<html>
<head>
<p> this works </P
</head>

<body>
<p>This also works</p>

<applet code = "Ranks.class"
	codebase = "http://djmcsi252.net76.net"
	archive = "GuildRanks.jar"
	width = 185 
	height = 200
>
</applet>

</body>

</html>

get these error messages from the compiler at runtime

Those messages are NOT from the compiler. IDEs seem to confuse the difference between compile and execution.

The error message is because an applet is not allowed to connect to other sites. If you have signed the jar file with the applet code, I don't know why you get the error.

You should get that error message in the browser's java console.

I have a signed applet at: http://norms.freeiz.com/AppletReader.html
It prompts for permission. It will read and display the contents of this file:
C:\\AppletTestFile.txt

I get this in the java console:

Java Plug-in 1.6.0_29
Using JRE version 1.6.0_29-b11 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Norm

----------------------------------------------------
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>
----------------------------------------------------

going to read C:\\AppletTestFile.txt

Oh I'm sorry I didn't have the Java console enabled earlier to see these errors.

Here's what I get

Java Plug-in 1.6.0_30
Using JRE version 1.6.0_30-b12 Java HotSpot(TM) Client VM
User home directory = C:\Users\Dave

----------------------------------------------------
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 www.wowprogress.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.Applet2SecurityManager.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 Ranks.run(Ranks.java:70)
at Ranks.init(Ranks.java:63)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.net.SocketPermission www.wowprogress.com:80 connect,resolve)

Oh, and the signing of the applet was successful (prior to uploading to my webhost) since the console told me it was good for 6 months so I'm confused as to why this isn't working. Thanks for your help btw :)

The error message says that your applet does not have permission to connect to that site.
Do you get the prompt from the JVM about security when you open the HTML page with the applet?
Did you try my applet to see what prompt you would get from the JVM about security?

Did you look in the jar file to see what it contains?

Do you get the prompt from the JVM about security when you open the HTML page with the applet?

No I do not.

Did you try my applet to see what prompt you would get from the JVM about security?

Yes

Did you look in the jar file to see what it contains?

The JAR file contains 3 items inside the folder META-INF
The files are: alias.DSA, alias.SF, and MANIFEST.MF

Where is the class file??? It needs to be in the jar file!!!

That's really strange I don't know why it's not in the class file. I'll show you the commands I used to sign it. I'm sure the commands is where I messed up. It was my first time with this so it wouldn't shock me if I missed a step lol

Ranks.java is my only file here

Steps:
1. command prompt
2. set directory to where Ranks.java file located
3. compiled Ranks.java file (to compile in version 6 and not 7)
4. command: jar cfm Ranks.jar manifest.txt
-Where manifest.txt just has the contents:

Main-Class: Ranks

Signing commands used:
4. command: keytool -genkey -alias mykey
5. command: jarsigner -signedjar GuildRanks.jar Ranks.jar mykey

Where do you put the .class file in the jar?

I don't think applets use the Main-Class: record. The name of the starting class is in the HTML.

Oh I must have created the jar file wrong then. I googled how to create a jar file with command prompt and followed that way since it was my first time.

I'll search for another command to create the JAR and try this all over again.

I'll post here whether or not I've been successful or not. But in the mean time, thank you for all your help I would have never guessed to check the JAR file!

When there are problems, check EVERYTHING. Make NO assumptions that this or that is good.

Okay so I created a new JAR file, signed it, uploaded it to my webhost again to test and I'm still getting the same java console error:

Java Plug-in 1.6.0_30
Using JRE version 1.6.0_30-b12 Java HotSpot(TM) Client VM
User home directory = C:\Users\Dave

----------------------------------------------------
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 www.wowprogress.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.Applet2SecurityManager.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 Ranks.run(Ranks.java:70)
at Ranks.init(Ranks.java:63)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.net.SocketPermission www.wowprogress.com:80 connect,resolve)

The signed JAR file has the same files as before but this time includes the Ranks.class file outside of the META-INF folder

You're sure that there isn't a Rank.class file left over from before?
The only Rank.class file available is in the jar file.

Okay never mind I fixed it sorry >< I just forgot to update the HTML :)

Thanks again for all your help though I really appreciate it!

Problem solved!

Glad you figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.