Proper packaging of runnable Jar project in netbeans

Viewed 176

So my task is to create a small program that displays a list of media files and run these media files with default OS media player separately.

My current solution was to create a package that holds all media files, something like:

-com.media
       |_a.mp4
       |_b.mp4

The following code copies to a temp dir the selected mp4, then runs the default os media player:

public File copyTempMedia(File tempAppFolder, String videoName) {

    URL f = getClass().getResource(String.format("%s/%s", Constants.MEDIA_LOCATION, videoName));
    File from = new File(f.getPath());
    File to = new File(tempAppFolder.getAbsolutePath());

    try {
        FileUtils.copyFileToDirectory(from, to);
    } catch (IOException ex) {
        Logger.getLogger(MediGUIModel.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Temp video copied: "  +  to.getAbsolutePath() + "/" + to.getName());
    return to;
}

public void triggerMediaPlayer(String fileLocation) {
    System.out.println("Triggering media player: " + fileLocation);
    try {
        if (OPERATIN_SYSTEM.contains("Linux")) {
            Runtime.getRuntime().exec("sh -c " + fileLocation);
        } else if (OPERATIN_SYSTEM.contains("Windows")) {

            Runtime.getRuntime().exec("cmd /c " + fileLocation);
        }
    } catch (IOException ex) {
        Logger.getLogger(MediGUIModel.class.getName()).log(Level.SEVERE, null, ex);
        ex.printStackTrace();
    }
}

When I run the program through Netbeans it works as espected, but when I do a clean/build the run the .jar created from the build, the media file doesn't seem to be read, so my questions are:

  1. Why does it work through Netbeans and not through build .jar ?
  2. Is this the best solution to this problem ?
  3. Should I package the media differently ?

Thanks in advance.

Edit

So after running through console instead of double clicking jar, is get a null pointer exception in the line where I read the file:

URL f = getClass().getResource(String.format("%s/%s", Constants.MEDIA_LOCATION, videoName));

Why does it work in Netebeans but not on build/jar ?

Is there another place in the jar I could place the media files, so that they are read with no problem through getResource or getResourceAsStream ?

4 Answers
Related