1

Hi I have a Java Applet which plays a file of my local file system. When I embed the Applet in a web page I get a io.FilePermission Exception. I understand that the only way round this is to get my Applet signed which I don't wish to.

How would I go about read the file from it's own jar file?, As I understand this will be the best way.

Thanks for any help guys.

Heres my code

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class PlaySoundsApplet extends Applet implements ActionListener{
  Button play,stop;
  AudioClip audioClip;

  public void init(){
  play = new Button("  Play in Loop  ");
  add(play);
  play.addActionListener(this);
  stop = new Button("  Stop  ");
  add(stop);
  stop.addActionListener(this);
  audioClip = getAudioClip(getCodeBase(), "clip.wav"); //Exception here trying to read from the www root instead of jar file

  }

  public void actionPerformed(ActionEvent ae){
  Button source = (Button)ae.getSource();
  if (source.getLabel() == "  Play in Loop  "){
  audioClip.play();
  }
  else if(source.getLabel() == "  Stop  "){
  audioClip.stop();
  }
  }
}

Updated code:

  public class PlaySoundsApplet extends Applet implements ActionListener{
  Button play,stop;
  AudioClip audioClip;

  public void init(){
  play = new Button("  Play in Loop  ");
  add(play);
  play.addActionListener(this);
  stop = new Button("  Stop  ");
  add(stop);
  stop.addActionListener(this);
  try {
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("resources/clip.wav"));
    Clip audioClip = AudioSystem.getClip();
    audioClip.open(audioInputStream);
} catch (UnsupportedAudioFileException e1) {System.out.println("Unsoported Error");} 
  catch (IOException e1) {System.out.println("IO Error");} catch (LineUnavailableException e) {System.out.println("Line unavailable Error");}
  }

  public void actionPerformed(ActionEvent ae){
  Button source = (Button)ae.getSource();
  if (source.getLabel() == "  Play in Loop  "){
  audioClip.play();
  }
  else if(source.getLabel() == "  Stop  "){
  audioClip.stop();
  }
  }
}
  • Which line is the exception from? – user253751 Apr 10 '14 at 12:48
  • I take it is's from audioClip = getAudioClip(getCodeBase(), "clip.wav"); – user3519503 Apr 10 '14 at 12:49
  • 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 3) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Apr 11 '14 at 04:50
  • *"..I understand that the only way round this is to get my Applet signed which I don't wish to."* Bad news. Even sand-boxed applets need to be digitally signed in this day and age, in order to have a reasonable chance of working. But getting an applet resource by `File` will not work when deployed in any case, so use an `URL` instead. – Andrew Thompson Apr 11 '14 at 04:52
  • Thanks for clearing that one up Andrew. Looks like the new security update in Java is just a barrier to my project. – user3519503 Apr 11 '14 at 11:11

1 Answers1

2

Try

URL url = getDocumentBase();

AudioClip audioClip = getAudioClip(url, "music/JButton.wav")

Project sturcture

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76