Skip to main content

Applets

3 replies [Last post]
jeffbruce
Offline
Joined: 2008-06-05

Are java applets fair game to discuss in this sub-forum? I looked at the alternatives (at least those a part of forums.java.net) and this sub-forum seems most appropriate.

Thanks in advance

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jeffbruce
Offline
Joined: 2008-06-05

I created a policy file with policytool and now it's partially working with appletviewer. The GUI is displayed and all the controls (ActionListeners) work properly. The problem now is that I'm getting an error when I try to list out the contents of a directory.

Particularly, I've identified these 2 lines as the source of the problem:
File f = new File(dir);
String[] children = f.list();
where the value of dir is file:/C:/Program%20Files/Java/jdk1.6.0_03/bin/TPPApp/SoundFiles

I think children is null after the call to f.list(). I tested this out with a System.out.println(children.length) and I got a null pointer exception with reference to the System.out.println(children.length) line.

Stack trace:
C:\Program Files\Java\jdk1.6.0_03\bin>appletviewer -J-Djava.security.policy=mypo
licy TPPApp\applet2.html
file:/C:/Program%20Files/Java/jdk1.6.0_03/bin/TPPApp/
file:/C:/Program%20Files/Java/jdk1.6.0_03/bin/TPPApp/SoundFiles
java.lang.NullPointerException
at PsychicPsychologist.levelBegin(PsychicPsychologist.java:254)
at PsychicPsychologist.runGame(PsychicPsychologist.java:228)
at PsychicPsychologist.init(PsychicPsychologist.java:53)
at sun.applet.AppletPanel.run(AppletPanel.java:419)
at java.lang.Thread.run(Thread.java:619)

Relevant methods:

public void init () {
System.out.println(getCodeBase());
dir = getCodeBase() + "SoundFiles";
System.out.println(dir);
sound_files = countFiles(dir);

p1_tags = new String[20];
p2_tags = new String[20]; // not needed yet

num_gen = new Random();
one_player = false;

high_scores = new int[10];

createComponents();
selectPlayers();
runGame();
}

private void levelBegin () {
showReady();
updateLabels();

while (!was_clicked) { // wait until ready button is clicked to commence next level
}

showGame();

// get random sound file
rand_index = num_gen.nextInt(sound_files.length);
mp3_filename = sound_files[rand_index];
mp3 = new MP3(dir + mp3_filename);

was_clicked = false;
is_closed = false;

level_number++;
tag_count = 0;
level_score = 0;
}

private String[] countFiles (String dir) {
File f = new File(dir);
String[] children = f.list();
//printChildren(children);
return children;
}

private void runGame () {
Reminder r;

while (true) { // game keeps playing until terminating condition

levelBegin(); // default settings for beginning of a level

mp3.play(); // play this random mp3 file

r = new Reminder(15, mp3, this); // activates in 15 seconds

while (!is_closed) { // collect input while mp3 file is playing
}

levelFinish(mp3); // check for matches between p1 / p2

}
}

swv
Offline
Joined: 2007-05-28

sure why not.

jeffbruce
Offline
Joined: 2008-06-05

Ok so here's my whole problem. I'm working on a java applet which will (eventually) be an online game. The stage I'm at is trying to launch the applet in a browser (or using appletviewer), but there have been several complications which I haven't been able to surmount. I've listed the series of steps I take in order to launch my applet with appletviewer and the stack trace below. Hopefully someone can work out what's wrong.

C:\Program Files\Java\jdk1.6.0_03\bin>appletviewer TPPApp\applet2.html
/C:/Program%20Files/Java/jdk1.6.0_03/bin/TPPApp/
java.security.AccessControlException: access denied (java.io.FilePermission file
:\C:\Program%20Files\Java\jdk1.6.0_03\bin\TPPApp\SoundFiles read)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:323)
at java.security.AccessController.checkPermission(AccessController.java:
546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at java.io.File.list(File.java:971)
at PsychicPsychologist.countFiles(PsychicPsychologist.java:335)
at PsychicPsychologist.init(PsychicPsychologist.java:40)
at sun.applet.AppletPanel.run(AppletPanel.java:419)
at java.lang.Thread.run(Thread.java:619)

Here are the two methods which allegedly play a part in this failed read due to security / permission issues.

public void init () {

System.out.println(getCodeBase().getFile());
dir = getCodeBase() + "SoundFiles//";
sound_files = countFiles(dir);

p1_tags = new String[20];
p2_tags = new String[20]; // not needed yet

num_gen = new Random();
one_player = false;

high_scores = new int[10];

createComponents();
selectPlayers();
runGame();

}

private String[] countFiles (String dir) {
File f = new File(dir);
String[] children = f.list();
return children;
}

I first developed an application in DrJava which works flawlessly. I'm in the process of transforming it into an applet which has turned out to be more work than I expected. The application plays mp3 files somewhere along its flow of control, and therefore reads mp3 files somewhere along the way. I downloaded a javazoom player at the beginning of development to play mp3 files and have included it as an extra classpath. As an application the mp3 files are read and played back flawlessly.

Does anyone have any ideas about these security error messages I'm getting? At first I thought it had to do with signing my applet. Correct me if I'm wrong, but I don't believe I'm operating outside of the sandbox. Sure, the applet reads a file from my computer, but the applet doesn't need to be signed to access files within its own codebase right?

I've also tried jarring my class files together with an images and sounds folder and this fails on the same grounds.