954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Applet Won't Load In Browser

Hey guys for some reason my code displays in the applet viewer in jgrasp and in eclipse, but for some reason it wont display via an html file in any browser. Any Ideas? Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class FamilyTreeTabbedPane extends JApplet implements ActionListener, MouseListener{
	//Make buttons and labels local to the class so that they can be accessed by all methods, and the action performed method
	JButton gButton, gTwoButton, gThreeButton,pButton, pTwoButton, pThreeButton,sButton, sTwoButton, sThreeButton,gPlay,gStop,pPlay,pStop,sPlay,sStop;
	JLabel grandText, parentText, siblingText;
	//create audio clips
	AudioClip gac,pac,sac;
	JTabbedPane tabPane;
	public void init(){
		//initialize my jtabbedpane
	    tabPane = new JTabbedPane();
		//set up tabpane mouse listener to listen for when clicked event
		tabPane.addMouseListener(this);
		//set size of the applet
		setSize(1400, 610);
		//create a new tab with an icon and the appropriate returned panel
		tabPane.addTab("Grandparent",new ImageIcon("grandIcon.jpg"), grandparentsTab());
		tabPane.addTab("Parents",new ImageIcon("parentIcon.jpg"),parentTab());
		tabPane.addTab("Siblings",new ImageIcon("siblingIcon.jpg"),siblingTab());
		//set the color of the tabbed panels
		tabPane.setBackgroundAt(0, Color.CYAN);
		tabPane.setBackgroundAt(1, Color.GREEN);
		tabPane.setBackgroundAt(2, Color.magenta);
		//add tabPane to the applet
		add(tabPane);
	}
//I normally would not have created three different method names, It adds alot of tedious work that can easily be circumvented by overloading the same one
//but in the instructions it said create three seperate methods for each tab, and I wasn't sure if method overloading was allowed, so I played it safe.	
	public JPanel grandparentsTab(){
		//create jpanel and Button
		JPanel grand, borderFrame, audioPanel;
		//setup audio clip
		gac = getAudioClip( getCodeBase(),"make my dreams come true.wav");
		//create jButtons
		gButton = new JButton(new ImageIcon("Grandparent.jpg"));
		gTwoButton = new JButton(new ImageIcon("Grandparent2.jpg"));
		gThreeButton = new JButton(new ImageIcon("Grandparent3.jpg"));
		gPlay = new JButton("Play");
		gStop = new JButton("Stop");
		//create jLabel
		grandText = new JLabel("               ",JLabel.CENTER);
		//create jpanels
		grand = new JPanel();
		audioPanel=new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		//add action listeners
		gButton.addActionListener(this);
		gTwoButton.addActionListener(this);
		gThreeButton.addActionListener(this);
		gPlay.addActionListener(this);
		gStop.addActionListener(this);
		//add to panel
		grand.add(gButton);
		grand.add(gTwoButton);
		grand.add(gThreeButton);
		audioPanel.add(gPlay);
		audioPanel.add(gStop);
		//add items to borderFrame
		borderFrame.add(grand,BorderLayout.CENTER);
		borderFrame.add(grandText,BorderLayout.NORTH);
		borderFrame.add(audioPanel,BorderLayout.SOUTH);
		//return to panel
		return borderFrame;
	}
	//refer to grandTab
	public JPanel parentTab(){
		JPanel parent, borderFrame, audioPanel;
		pac = getAudioClip( getCodeBase(),"sweet disposition.wav");
		pButton = new JButton(new ImageIcon("Parents.jpg"));
		pTwoButton = new JButton(new ImageIcon("Parents2.jpg"));
		pThreeButton = new JButton(new ImageIcon("Parents3.jpg"));
		pPlay = new JButton("Play");
		pStop = new JButton("Stop");
		parentText = new JLabel("                    ",JLabel.CENTER);
		audioPanel=new JPanel();
		parent = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		pButton.addActionListener(this);
		pTwoButton.addActionListener(this);
		pThreeButton.addActionListener(this);
		pPlay.addActionListener(this);
		pStop.addActionListener(this);
		parent.add(pButton);
		parent.add(pTwoButton);
		parent.add(pThreeButton);
		audioPanel.add(pPlay);
		audioPanel.add(pStop);
		borderFrame.add(parent,BorderLayout.CENTER);
		borderFrame.add(parentText,BorderLayout.NORTH);
		borderFrame.add(audioPanel,BorderLayout.SOUTH);
		return borderFrame;
	}
	//refer to grandTab
	public JPanel siblingTab(){
		JPanel sibling, borderFrame, audioPanel;    
		sac = getAudioClip( getCodeBase(),"shes got you high.wav");
		sButton = new JButton(new ImageIcon("siblings.jpg"));
		sTwoButton = new JButton(new ImageIcon("siblings2.jpg"));
		sThreeButton = new JButton(new ImageIcon("siblings3.jpg"));
		sPlay = new JButton("Play");
		sStop = new JButton("Stop");
		siblingText = new JLabel("               ",JLabel.CENTER);
		audioPanel=new JPanel();
		sibling = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		sButton.addActionListener(this);
		sTwoButton.addActionListener(this);
		sThreeButton.addActionListener(this);
		sPlay.addActionListener(this);
		sStop.addActionListener(this);
		sibling.add(sButton);
		sibling.add(sTwoButton);
		sibling.add(sThreeButton);
		audioPanel.add(sPlay);
		audioPanel.add(sStop);
		borderFrame.add(sibling,BorderLayout.CENTER);
		borderFrame.add(siblingText,BorderLayout.NORTH);
		borderFrame.add(audioPanel,BorderLayout.SOUTH);
		return borderFrame;
	}
	public void actionPerformed(ActionEvent event) {
		Object src = event.getSource();
		
		if(src == gButton)
			grandText.setText("This is my grandma!");
		else if(src == gTwoButton)
			grandText.setText("This is my grandma and my Family!");
		else if(src == gThreeButton)
			grandText.setText("This is my grandma and my mother!");
		else if(src == pButton)
			parentText.setText("My mom, dad, and my nephew!");
		else if(src == pTwoButton)
			parentText.setText("My mom and dad dancing!");
		else if(src == pThreeButton)
			parentText.setText("My mom and dad 80's style!");
		else if(src == sButton)
			siblingText.setText("Me my brother and my sister!");
		else if(src == sTwoButton)
			siblingText.setText("Wow was I young here!");
		else if(src == sThreeButton)
			siblingText.setText("Me and my sister!");
		else if(src == gPlay){
			stopMusic(sac);
			stopMusic(pac);
			gac.play();
		}else if(src == gStop)
			stopMusic(gac);
		else if(src == pPlay){
			stopMusic(gac);
			stopMusic(sac);
			pac.play();
		}else if(src == pStop)
			stopMusic(pac);
		else if(src == sPlay){
			stopMusic(gac);
			stopMusic(pac);
			sac.play();
		}else if(src == sStop)
			stopMusic(sac);
		
	}
	private void stopMusic(AudioClip clip){
		//check if clip is playing if it is stop it
		if(clip != null)
			clip.stop();
	}
	public void mouseClicked(MouseEvent me)
	{
	
		int tabindex = tabPane.getSelectedIndex();
		
		if (tabindex == 0)
		{
			gac.play();
			pac.stop();
			sac.stop();
		}
		else if (tabindex == 1)
		{
			pac.play();
			gac.stop();
			sac.stop();
		}
		else if (tabindex == 2)
		{
			sac.play();
			gac.stop();
			pac.stop();
		}
	}
	
	public void mouseEntered(MouseEvent me){}
	public void mouseExited(MouseEvent me){}
	public void mousePressed(MouseEvent me){}
	public void mouseReleased(MouseEvent me){}

}
<html>
<head> <title>Thomas Stephen Brown</title></head>
<body><applet code="FamilyTreeTabbedPane.class" width="1400" height = "610"></applet></body>
</html>
spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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

BTW The APPLET tag's code= attribute refers to a class not a file.
Try removing the .class

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

yea here is what im getting sorry didnt know you could check.


Java Plug-in 1.6.0_24
Using JRE version 1.6.0_24-b07 Java HotSpot(TM) Client VM
User home directory = C:\Users\Thomas
----------------------------------------------------
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
----------------------------------------------------
java.security.AccessControlException: access denied (java.io.FilePermission grandIcon.jpg read)
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.checkRead(Unknown Source)
at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
at sun.awt.SunToolkit.getImage(Unknown Source)
at javax.swing.ImageIcon.(Unknown Source)
at javax.swing.ImageIcon.(Unknown Source)
at FamilyTreeTabbedPane.init(FamilyTreeTabbedPane.java:25)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.io.FilePermission grandIcon.jpg read)

spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

if you run an applet once via a browser, the browser will remember your applet, and will output what it has in its memory instead of the new version of the applet. therefore, you need to clean your browser's memory to run the new version of your applet if you've changed it. otherwise, everything seems to be fine. one more thought: are you sure you've saved your html file with an html suffix?

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 
access denied (java.io.FilePermission grandIcon.jpg read)


This error says that the applet was denied permission to read a file from the local system.
Unsigned applets loaded from a server are NOT allowed to read files from the local system.

You should put ALL the files the applet needs into a jar file on the server, reference that jar file in the

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I compiled the code and ran it. I got a frame with three tabs, no pictures... I missed the FilePermission error posted here, no error when I compiled/ran the code. my bad!

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 

This is so frustrating hahah. Yea I'm running it from my home computer, and I've tested it on others, and it does roughly the same thing. Any other ideas, NormR1 my professor is very strict when it comes to using methods that are not in the book so unfortunately I can't do that. I think it has something to do with the policy info on my computer but I tried changing it and I don't think it helped.

spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

How are you executing the applet?
Where are all the files? Are they all local, no server?
How do you load the html into the browser?

Your code works for me for the few images I changed from yours to mine:
Java Plug-in 1.6.0_20
Using JRE version 1.6.0_25-b06 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Owner

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

Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/make my dreams come true.wav
Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/sweet disposition.wav
Loaded audio clip: file:/D:/JavaDevelopment/Testing/ForumQuestions5/shes got you high.wav

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

When i debug the applet using eclipse or jgrasp it automatically creates a html file for me, I click on this html file which opens either firefox, chrome, or internet explorer. Tested them all.

The files are all stored locally. It pretty much looks like its hanging. Thanks for your help btw.

spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 
looks like its hanging


Where/when does the: java.security.AccessControlException: access denied (java.io.FilePermission grandIcon.jpg read)
happen?
This exception shouldn't happen for all local files.

Add lots of printlns to your code to show where the execution flow goes. If it is hanging, then the place it is hanging will be after the last print out and before the next one that didn't print.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

So I added teh printlns but im not sure how to see them other then when i run them via jgrasp applet viewer or eclipse. In the browser how would i see them. I think the problem is that as soon as it tries to load the first image it throws the exception, so it hangs because of that. Do I have to sign it or something. It's wierd because I have a few other peoples from my class, and there's all work fine.

spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

I have added a bunch of printlns but I dont know how to see them other than in the jgrasp console. In the browser they dont display. I believe the reason its hanging is because of the fact that its throwing an exception on the first image it tries to load. Very strange indeed. I have a few other people assignments that have virtually the same thing, but theres loads. Any clue why? Maybe the digital signature? O man this is a headaache.

spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

It should not throw an AccessControlException for local files.
I'm confused by that.

You do understand that you must tell us exactly what is happening!!
You can't assume something or think that all errors are the same.

One problem I have believing you is that the line number in the error message:
at FamilyTreeTabbedPane.init(FamilyTreeTabbedPane.java:25)
line 25 is NOT where the image is being read in the code you posted which shows the first ImageIcon being called at line 20.
That means you have made changes between what you posted and when you got the error.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Im very sorry I didn't realize that I was wrong and that I meant to post this one. So sorry.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class FamilyTreeTabbedPane extends JApplet implements ActionListener, MouseListener{
	//Make buttons and labels local to the class so that they can be accessed by all methods, and the action performed method
	JButton gButton, gTwoButton, gThreeButton,pButton, pTwoButton, pThreeButton,sButton, sTwoButton, sThreeButton;
	JLabel grandText, parentText, siblingText;
	//create audio clips
	AudioClip gac,pac,sac;
	JTabbedPane tabPane;
	public void init(){
	
		//initialize my jtabbedpane
	   tabPane = new JTabbedPane();
		//set up audio
		gac = getAudioClip( getCodeBase(),"make my dreams come true.wav");
		pac = getAudioClip( getCodeBase(),"sweet disposition.wav");
		sac = getAudioClip( getCodeBase(),"shes got you high.wav");
		//set up tabpane mouse listener to listen for when clicked event
		tabPane.addMouseListener(this);
		//set size of the applet
		setSize(1400, 610);
		//create a new tab with an icon and the appropriate returned panel
		tabPane.addTab("Grandparent",new ImageIcon("grandIcon.jpg"), grandparentsTab());
		tabPane.addTab("Parents",new ImageIcon("parentIcon.jpg"),parentTab());
		tabPane.addTab("Siblings",new ImageIcon("siblingIcon.jpg"),siblingTab());
		//set the color of the tabbed panels
		tabPane.setBackgroundAt(0, Color.CYAN);
		tabPane.setBackgroundAt(1, Color.GREEN);
		tabPane.setBackgroundAt(2, Color.magenta);
		//add tabPane to the applet
		add(tabPane);
	}
//I normally would not have created three different method names, It adds alot of tedious work that can easily be circumvented by overloading the same one
//but in the instructions it said create three seperate methods for each tab, and I wasn't sure if method overloading was allowed, so I played it safe.	
	public JPanel grandparentsTab(){
		//create jpanel and Button
		JPanel grand, borderFrame;
		//setup audio clip
				//create jButtons
		gButton = new JButton(new ImageIcon("Grandparent.jpg"));
		gTwoButton = new JButton(new ImageIcon("Grandparent2.jpg"));
		gThreeButton = new JButton(new ImageIcon("Grandparent3.jpg"));
		//create jLabel
		grandText = new JLabel("               ",JLabel.CENTER);
		//create jpanels
		grand = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		//add action listeners
		gButton.addActionListener(this);
		gTwoButton.addActionListener(this);
		gThreeButton.addActionListener(this);
		//add to panel
		grand.add(gButton);
		grand.add(gTwoButton);
		grand.add(gThreeButton);
		//add items to borderFrame
		borderFrame.add(grand,BorderLayout.CENTER);
		borderFrame.add(grandText,BorderLayout.NORTH);
		//return to panel
		return borderFrame;
	}
	//refer to grandTab
	public JPanel parentTab(){
		JPanel parent, borderFrame;
		pButton = new JButton(new ImageIcon("Parents.jpg"));
		pTwoButton = new JButton(new ImageIcon("Parents2.jpg"));
		pThreeButton = new JButton(new ImageIcon("Parents3.jpg"));
		parentText = new JLabel("                    ",JLabel.CENTER);
		parent = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		pButton.addActionListener(this);
		pTwoButton.addActionListener(this);
		pThreeButton.addActionListener(this);
		parent.add(pButton);
		parent.add(pTwoButton);
		parent.add(pThreeButton);
		borderFrame.add(parent,BorderLayout.CENTER);
		borderFrame.add(parentText,BorderLayout.NORTH);
		return borderFrame;
	}
	//refer to grandTab
	public JPanel siblingTab(){
		JPanel sibling, borderFrame;    
		sButton = new JButton(new ImageIcon("siblings.jpg"));
		sTwoButton = new JButton(new ImageIcon("siblings2.jpg"));
		sThreeButton = new JButton(new ImageIcon("siblings3.jpg"));
		siblingText = new JLabel("               ",JLabel.CENTER);
		sibling = new JPanel();
		borderFrame = new JPanel(new BorderLayout());
		sButton.addActionListener(this);
		sTwoButton.addActionListener(this);
		sThreeButton.addActionListener(this);
		sibling.add(sButton);
		sibling.add(sTwoButton);
		sibling.add(sThreeButton);
		borderFrame.add(sibling,BorderLayout.CENTER);
		borderFrame.add(siblingText,BorderLayout.NORTH);
		return borderFrame;
	}
	public void actionPerformed(ActionEvent event) {
		Object src = event.getSource();
		
		if(src == gButton)
			grandText.setText("This is my grandma!");
		else if(src == gTwoButton)
			grandText.setText("This is my grandma and my Family!");
		else if(src == gThreeButton)
			grandText.setText("This is my grandma and my mother!");
		else if(src == pButton)
			parentText.setText("My mom, dad, and my nephew!");
		else if(src == pTwoButton)
			parentText.setText("My mom and dad dancing!");
		else if(src == pThreeButton)
			parentText.setText("My mom and dad 80's style!");
		else if(src == sButton)
			siblingText.setText("Me my brother and my sister!");
		else if(src == sTwoButton)
			siblingText.setText("Wow was I young here!");
		else if(src == sThreeButton)
			siblingText.setText("Me and my sister!");
		
	}
	public void mouseClicked(MouseEvent me)
	{
	
		int tabindex = tabPane.getSelectedIndex();
		
		if (tabindex == 0)
		{
			gac.play();
			pac.stop();
			sac.stop();
		}
		else if (tabindex == 1)
		{
			pac.play();
			gac.stop();
			sac.stop();
		}
		else if (tabindex == 2)
		{
			sac.play();
			gac.stop();
			pac.stop();
		}
	}
	
	public void mouseEntered(MouseEvent me){}
	public void mouseExited(MouseEvent me){}
	public void mousePressed(MouseEvent me){}
	public void mouseReleased(MouseEvent me){}

}
spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

That doesn't change that fact that locally executed applets should not get AccessControlException.

Do you have a .java.policy file in this folder: C:\Users\Thomas
Have you used the policytool to make any changes to permissions?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Nope I dont believe that I do. I'm at school right now so I cant check but I know there is a policy file in the bin folder of eclipse that grants permissions to everything, which i assume is for the appletviewer. And no i have not used the policytool. I have found a solution though. It circumvents reading from im assuming the stream, by getting the base code of the image initially so that it never actually pulls the image from its location.

I'm using

"anImageIconHere"= new ImageIcon(getImage(getCodeBase(), "siblings.jpg"));

to set the image to the imageicon and then i use the imageicon as the parameter rather than creating it t and setting it when the method is called

spartanace
Newbie Poster
16 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

I think your solution works because getCodeBase returns the URL the applet was loaded from (ie the server), so your getImage downloads the image file from that location, thus not needing local file access or any special permissions. I believe this is the "correct" way to do it anyway, rather than fiddling with policy files on every machine that might run this applet.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
it never actually pulls the image from its location.


It must ultimately read the file from the local system. If it were this easy to bypass the security requirements, there really is not any security at all.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Hi Norm. Why do you say "It must ultimately read the file from the local system"?
I'm assuming the image files are in the same place (on the server) as the applet itself, so getImage(getCodeBase(), "someFile.jpg") will read the image from wherever the applet was loaded from, which is allowed by standard applet security... or have I missed the point somewhere?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

I didn't think there was a server involved with this problem. I thought that All the files were on the OP's local disk. Hence my remark that all the files were being read from the local disk.
I wasn't thinking about getting files from a server. The getCodeBase() should work as you say.

Earlier I asked: Where are all the files? Are they all local, no server?
The OP's answer: The files are all stored locally.

There was no mention of a server, so I assumed that all the files are local.

Strange!!?? Need a webcam to look over the OPs shoulder and see what he is doing.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: