【问题标题】:Java app cant find files after building it into a .jar in Netbeans?Java 应用程序在 Netbeans 中将其构建为 .jar 后无法找到文件?
【发布时间】:2020-10-04 21:03:36
【问题描述】:

我编写了一个显示图像和播放声音的应用程序。 这就是为什么它需要从位于项目的 src 文件夹中的名为“recources”的文件夹中加载图像和声音。

对于图片,我使用以下代码加载 png,然后显示它我的框架:

ImageIcon img = new ImageIcon(getClass().getResource("/recources/img.png"));

这适用于在 Netbeans 中运行应用程序以及在我将其构建到 jar 之后。

声音是这样加载的:

public Sound(){

    try
    {

        File audiodata = new File("/recources/audio.wav");
        if(audiodata.exists()){
            audioInput = AudioSystem.getAudioInputStream(audiodata);

            clip= AudioSystem.getClip();
            clip.open(audioInput);
            clip.start();

        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

这也在 Netbeans 中工作,但在完成的 jar 中不能。 --> NullPointerException

我需要对我的代码进行哪些更改?

什么也不起作用:

我还想使用以下代码加载带有 jdbc 驱动程序的 sqllite 数据库:

public void getConnection() {
    try {
        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        Connection con = DriverManager.getConnection("jdbc:sqlite:recources/database.db");
    } catch (SQLException ex) {
        Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
    }

}

它有同样的问题,当我在 Netbeans 中运行它时,它会找到数据库,但在构建的 jar 中却找不到。

我将资源文件夹作为源添加到我的项目属性中。

【问题讨论】:

    标签: java netbeans build jar


    【解决方案1】:

    尝试将声音类更改为:

    public Sound(){
    
        try
        {
    
            File audiodata = new File(this.getClass().getResourceAsStream("/recources/audio.wav").toString());
            if(audiodatei.exists()){
                audioInput = AudioSystem.getAudioInputStream(audiodata);
    
                clip= AudioSystem.getClip();
                clip.open(audioInput);
                clip.start();
    
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    

    并获取连接到:

    public void getConnection() {
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Connection con = DriverManager.getConnection("jdbc:sqlite:"+this.getClass().getResourceAsStream("/recources/database.db").toString());
        } catch (SQLException ex) {
            Logger.getLogger(Lagerverwaltung.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }
    
    

    刚刚更改了文件的访问方式。希望它有效。一定要告诉结果。

    【讨论】:

    • "this.getClass().getResourceAsStream("/recources/database.db").toString()" 给出错误:“非静态变量 this 不能从静态上下文中引用”。我还将它添加到声音类中,并在运行时给出 NullPointerException。
    • 当我在声音类中运行 clip.stop() 而不是在加载或执行 clip.start() 时,它会给出 NullPointer。如果我将 url 更改为错误的 url,它会在加载文件时给出它,所以加载功能但剪辑不再工作。
    • 更新:我得到了使用这个解决方案的声音:Solution
    • 好的,你得到了解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    相关资源
    最近更新 更多