首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.io.FilePermission异常-是否从jar文件读取?

java.io.FilePermission异常-是否从jar文件读取?
EN

Stack Overflow用户
提问于 2014-04-10 20:46:53
回答 1查看 262关注 0票数 1

你好,我有一个Java Applet,它可以播放我本地文件系统中的一个文件。当我在网页中嵌入小程序时,我得到了一个io.FilePermission异常。我知道解决这个问题的唯一方法就是让我的Applet签名,而我不希望这样做。

我该如何从它自己的jar文件中读取该文件呢?

感谢大家的帮助。

下面是我的代码

代码语言:javascript
复制
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();
  }
  }
}

更新代码:

代码语言:javascript
复制
  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();
  }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2014-04-10 20:55:57

试一试

代码语言:javascript
复制
URL url = getDocumentBase();

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

项目结构

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22988516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档