Reputation: 1096
Ok, simply put I am making a quiz game in a java applet, and I want to serialize an object which stores the high scores. When I do this it works perfectly in eclipse but not in a browser.
Here is the code of my applet where it reads the file: and yes I have all of the appropriate imports
package histApplet;
public class QuizApplet extends Applet
{
private static final String TRACKERLOC = "histApplet/track.ser";
private StatsTracker tracker;
private int difflevel = 1;
//other instance variables
public void init()
{
//other code
if(new File(TRACKERLOC).exists())
{
tracker = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try
{
fis = new FileInputStream(TRACKERLOC);
in = new ObjectInputStream(fis);
tracker = (StatsTracker)in.readObject();
in.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
}
else
{
tracker = new StatsTracker(difflevel);
}
//other code
}
And here is my html code
<html>
<head><title>QuizApplet</title></head>
<body>
<center><applet code="histApplet/QuizApplet.class" height=550 width=700>
</applet></center>
</body>
</html>
If I comment out this code it works in a browser but otherwise doesn't. I'm not sure why this doesn't work, and any help would be greatly appreciated.
Upvotes: 0
Views: 845
Reputation: 74750
As written by David, applets can't access the local file system.
They can send data to the host they came from (and receive answers from there), so you could store the highscores on the server, if you have some server-side program which accepts these highscores there.
An alternative would be using a JNLP-deployed applet, then your applet could access an applet-specific local storage with a PersistenceService.
Upvotes: 0
Reputation: 2725
Java Applets execute in a sandbox within the browser, so have limited access to resources in the client machine running the applet (into the browser). File system can't be accessed by an Applet, as explained in several sites SecuringJava, Oracle.
You need to sign your Applet (trusted code) in order to get access to the file system, Oracle.
Upvotes: 1