Linux上的Java Applet无法获得权限

时间:2012-10-30 16:12:53

标签: java permissions

我正在参加在线课程,我偶尔也会因为课程以半窗口为中心而挂断。目前我有一个java applet,我正在尝试播放音频剪辑。我正在使用Idea Intellij,我将音频文件放在与类文件相同的位置。我还更新了想法用于运行applet的策略文件,但无论我做什么,它似乎都找不到音频文件。我75%肯定这是我用政策文件没想到的东西(唉!),但我无法弄清楚那是什么。我将粘贴代码片段,我的策略文件以及尝试运行时的一些输出。请帮助 - 这是我第二次因为特定于Linux的差异而陷入困境。

格式化打嗝的道歉......

    public class MyApplet extends JApplet implements ActionListener
    {
        JButton play, stop;
        AudioClip audioClip;
        File myAudioFile;

        public void init()
        {
            play = new JButton("Play");
            play.addActionListener(this);

            stop = new JButton("Stop");
            stop.addActionListener(this
    [...took out unnecessary stuff...]
            System.out.println("Codebase is: " + getCodeBase());
            myAudioFile = new File(getCodeBase().toString()+"desktop-login.ogg");
            if(myAudioFile.exists()) {
                audioClip = getAudioClip(getCodeBase(), "desktop-login.ogg");
            }
            else {
                System.out.println("Failed to find file:"+ audioClip);
            }

        }
[...took out unnecessary stuff...]
    //------------------POLICY FILE CONTENTS--
    grant {
      permission java.security.AllPermission;
      permission java.net.SocketPermission "*", "accept, connect, listen, resolve";
    };

    grant codeBase "file:/home/mylogin/IdeaProjects/3RD/Lesson7/soundPlay/out/production/soundPlay/*" {
      permission java.util.PropertyPermission "user.home", "read";
    }; 

    //--------output (scrubbed a bit - it's failing at the audio clip play part because the audioClip object is null): 
    Codebase is: file:/home/mylogin/IdeaProjects/3RD/Lesson7/soundPlay/out/production/soundPlay/
    Failed to find file:null
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at MyApplet.actionPerformed(MyApplet.java:48)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 

2 个答案:

答案 0 :(得分:0)

使用这样的文件路径并尝试

file:////home/mylogin/IdeaProjects/3RD/Lesson7/soundPlay/out/production/soundPlay/

答案 1 :(得分:0)

好吧,我觉得很傻。原来试图在Linux上的本机java中加载.ogg文件不起作用。我以为它会使用基于Linux的编解码器,但我错了。它将使用java可以本机解码的东西 - 例如.wav文件。当我切换到.wav文件时,一切都很开心并开始工作。啧!