URI not hierarchical need to use File class for a method

前端 未结 1 1366
栀梦
栀梦 2021-01-28 11:03

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

I have s

相关标签:
1条回答
  • 2021-01-28 11:14

    You can copy the file from the jar to a temporary file and open that.

    Here's a method to create a temporary file for a given jar resource:

    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;
    }
    

    And here's an example of how you'd use it:

    Desktop.getDesktop().open(createTempFile("/videos/tutorialVid1.mp4"));
    
    0 讨论(0)
提交回复
热议问题