项目在IDE中工作,但在导出到Runnable JAR后出错(系统找不到指定的路径)

时间:2017-10-06 17:20:09

标签: java eclipse audio

我一直在尝试使用Eclipse在JavaScript中创建一个SoundBoard,起初我的确做得很好。在设置GUI并将文件链接起来后,实际播放的声音就会出现。

不幸的是,我的运气并没有持续下去。我将项目导出为 Runnable Jar文件,然后我选择“Extract required libraries into generated JAR”选项。我将JAR导出到我的桌面,运行它,但我不断收到错误消息:

  

src \ main \ SoundBoardTest \ Sounds \ Ping.wav(系统找不到指定的路径)

我在项目中有两个类,SoundBoardFrame(对于GUI和actionPerformed按钮动作侦听器,以及PlaySound,我保留所有方法来运行特定声音,基于您按下的按钮

以下是我的SoundBoardFrame类的代码:

package main.SoundBoardTest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JButton;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.TargetDataLine;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.Color;
import javax.sound.sampled.*;

public class SoundBoardFrame extends JFrame {

private JPanel contentPane;
static JTextField txtField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SoundBoardFrame frame = new SoundBoardFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public SoundBoardFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 600);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblSoundboard = new JLabel("-----   SOUNDBOARD   -----");
    lblSoundboard.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 45));
    lblSoundboard.setHorizontalAlignment(SwingConstants.CENTER);
    lblSoundboard.setBounds(0, 0, 774, 72);
    contentPane.add(lblSoundboard);

    JButton btnSound = new JButton("Sound 1");
    btnSound.setFocusPainted(false);
    btnSound.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            txtField.setText("Played Sound: 1");
            PlaySound.play();
        }
    });
    btnSound.setFont(new Font("Tw Cen MT", Font.PLAIN, 50));
    btnSound.setBounds(10, 93, 220, 80);
    contentPane.add(btnSound);

    JButton btnSound1 = new JButton("Air Horn");
    btnSound1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            txtField.setText("Played Sound: Air Horn");
            PlaySound.play2();
        }
    });
    btnSound1.setFocusPainted(false);
    btnSound1.setFont(new Font("Tw Cen MT", Font.PLAIN, 50));
    btnSound1.setBounds(10, 183, 220, 80);
    contentPane.add(btnSound1);

    JButton btnSound2 = new JButton("Ping");
    btnSound2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            txtField.setText("Played Sound: Ping");
            PlaySound.play3();
        }
    });
    btnSound2.setFocusPainted(false);
    btnSound2.setFont(new Font("Tw Cen MT", Font.PLAIN, 50));
    btnSound2.setBounds(10, 272, 220, 80);
    contentPane.add(btnSound2);

    JButton btnSound3 = new JButton("Sound 4");
    btnSound3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent argo0) {
            txtField.setText("Played Sound: 4");
            PlaySound.play();
        }
    });
    btnSound3.setFocusPainted(false);
    btnSound3.setFont(new Font("Tw Cen MT", Font.PLAIN, 50));
    btnSound3.setBounds(543, 93, 220, 80);
    contentPane.add(btnSound3);

    JButton btnSound4 = new JButton("Sound 5");
    btnSound4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            txtField.setText("Played Sound: 5");
            PlaySound.play();
        }
    });
    btnSound4.setFocusPainted(false);
    btnSound4.setFont(new Font("Tw Cen MT", Font.PLAIN, 50));
    btnSound4.setBounds(543, 183, 220, 80);
    contentPane.add(btnSound4);

    JButton btnSound5 = new JButton("Sound 6");
    btnSound5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            txtField.setText("Played Sound: 6");
            PlaySound.play();
        }
    });
    btnSound5.setFocusPainted(false);
    btnSound5.setFont(new Font("Tw Cen MT", Font.PLAIN, 50));
    btnSound5.setBounds(543, 272, 220, 80);
    contentPane.add(btnSound5);

    txtField = new JTextField();
    txtField.setBorder(null);
    txtField.setToolTipText("This text box displays a message if there is an error.");
    txtField.setBackground(Color.WHITE);
    txtField.setFont(new Font("Agency FB", Font.PLAIN, 30));
    txtField.setText("Test");
    txtField.setEditable(false);
    txtField.setHorizontalAlignment(SwingConstants.CENTER);
    txtField.setBounds(251, 93, 271, 259);
    contentPane.add(txtField);
    txtField.setColumns(10);
}
}

此外,忽略过去“Ping”的按钮代码,任何随机路径都会超出PlaySound类中的前几个方法。

以下是PlaySound类的代码:

 package main.SoundBoardTest;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;

    import javax.annotation.Resource;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.TargetDataLine;

    public class PlaySound {

            public static void play() {
                try {
                    AudioInputStream in = AudioSystem.getAudioInputStream(new File("src/main/SoundBoardTest/Sounds/Ping.wav"));
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
                    AudioInputStream in2 = new AudioInputStream(bufferedInputStream, in.getFormat(), in.getFrameLength());
                    Clip clip = AudioSystem.getClip();
                    clip.open(in);
                    clip.start();
                } catch (Exception e) {
                    SoundBoardFrame.txtField.setText(e.getMessage());
                    System.err.println(e.getMessage());
                }
            }
            public static void play2() {
                try {
                    File file = new File("src/main/SoundBoardTest/Sounds/Air Horn.wav");
                    Clip clip = AudioSystem.getClip();
                    clip.open(AudioSystem.getAudioInputStream(file));
                    clip.start();
                } catch (Exception e) {
                    SoundBoardFrame.txtField.setText(e.getMessage());
                }
            }
            public static void play3() {
                try {
                    File file = new File("src/main/SoundBoardTest/Sounds/Ping.wav");
                    Clip clip = AudioSystem.getClip();
                    clip.open(AudioSystem.getAudioInputStream(file));
                    clip.start();
                } catch (Exception e) {
                    SoundBoardFrame.txtField.setText("Error finding sound.");
                }
            }
            public static void play4() {
                try {
                    File file = new File("Sounds/Ping.wav");
                    Clip clip = AudioSystem.getClip();
                    clip.open(AudioSystem.getAudioInputStream(file));
                    clip.start();
                } catch (Exception e) {
                    SoundBoardFrame.txtField.setText("Error finding sound.");
                }
            }

    }

这就是我拥有的所有代码/类。我的项目的层次结构如下:

Hierarchy for my project in Eclipse: "SoundBoardTest"

我该怎么做或尝试?

0 个答案:

没有答案