简体   繁体   中英

Java Applet on Linux unable to gain Permissions

I'm taking an online class and I'm occasionally getting hung-up because the class is semi windows-centric. Currently I've got a java applet in which I'm trying to play an audio clip with. I'm using Idea Intellij and I've put an audio file in the same location as the class files. I've also updated the policy file that idea uses to run the applet, but no matter what I do, it can't seem to find the audio file. I'm 75% sure it's something I've not figured out with the policy file (argh!), but I can't figure out what that is. I'll paste in a code snippet, my policy file and some output from when I try to run it. Please help - this is the second time I've been stuck due to linux-specific differences.

Apologies for the formatting hiccups...

    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) 

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

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

OK, I feel silly. Turns out trying to load .ogg files in native java on linux doesn't work. I had thought it was going to use Linux based codecs, but I was wrong. It was going to use what java can decode natively - which is for example, .wav files. When I switched to .wav files everything was happy and started to work. Sheesh!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM