URIs do not require hierarchical use of the File class for a method

I need to open a video file with my code, and it works fine in Eclipse, but when I export to the runnable JAR, I get the error "URI is not hierarchical."

I have seen people suggest using getResourceAsStream (), but I need to have a file object since I use Desktop.getDesktop.open (File). Can anyone help me out?

Here is the code:

try {
    URI path1 = getClass().getResource("/videos/tutorialVid1.mp4").toURI();
    File f = new File(path1);

    Desktop.getDesktop().open(f);
} catch (Exception e) {
    e.printStackTrace();
}

if that helps my list of folders like

  • Src

    • video
      • videoFile.mp4

EDIT: I plan to run this only on windows and use launch4j to create exe.

0
source share
1 answer

You can copy the file from the bank to a temporary file and open it.

jar:

public static File createTempFile(String path) {
    String[] parts = path.split("/");
    File f = File.createTempFile(parts[parts.length - 1], ".tmp");
    f.deleteOnExit();
    try (Inputstream in = getClass().getResourceAsStream(path))  {
        Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
    return f;
}

, :

Desktop.getDesktop().open(createTempFile("/videos/tutorialVid1.mp4"));
+1

Source: https://habr.com/ru/post/1750847/


All Articles