首页 文章

执行在jar中打包的wmv文件

提问于
浏览
1

我想执行PicturePackage中的wmv视频文件 . 我使用以下代码:

try {
        File f;
        f = new File(getClass().getResource("/PicturePackage/admin_input.wmv").toURI());
        Desktop.getDesktop().open(f);
    } catch (URISyntaxException | IOException ex) {
        Logger.getLogger(Help.class.getName()).log(Level.SEVERE, null, ex);
    }

当我在netbeans中运行时,此代码运行并播放视频 . 但是当我通过netbeans构建的jar文件执行它时,它不会运行视频文件 . 任何具体问题我都没有照顾???

编辑:

我试过这个

File tempFile = null;
try (InputStream in =
    getClass().getResourceAsStream("/PicturePackage/admin_input.wmv")) {
Path temp = Files.createTempFile("temp", ".wmv");
Files.copy(in, temp);
tempFile = temp.toFile();
// This will try to delete the file when you close your java app
tempFile.deleteOnExit(); 
} catch (Exception e) {
// Handle the exceptions properly
}

// Here you can use tempFile to open it
if (tempFile != null) {
try {
    Desktop.getDesktop().open(tempFile);
} catch (IOException e) {
    // Handle exception
}
}

这就是我得到的堆栈跟踪

java.nio.file.FileAlreadyExistsException:位于sun.nio.fs.WindowsException的sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:81)中的C:\ Users \ Ashu \ AppData \ Local \ Temp \ temp1136027223125637051.wmv . 在sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)的sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)的java.nio.file.spi上的rethrowAsIOException(WindowsException.java:97) .FileSystemProvider.newOutputStream(FileSystemProvider.java:430)at java.nio.file.Files.newOutputStream(Files.java:170)at java.nio.file.Files.copy(Files.java:2841)at gatetestadmin.Help . 来自gatetestadmin的gatetestadmin.Help.access $ 000(Help.java:23)的jButton1ActionPerformed(Help.java:148) . 在javax.swing.AbstractButton.fireActionPerformed上帮助$ 1.actionPerformed(Help.java:63)(AbstractButton.java:2018 )在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2341)at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButto) nModel.java:402)javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)at java.awt.Component.processMouseEvent(Component .java:6505)在java.awt.Container.processEvent(Container.java:2229)的java.awt.Component.processEvent(Component.java:6270)的javax.swing.JComponent.processMouseEvent(JComponent.java:3320) java.awt.Component上的java.awt.Component的一个java.awt.Container.dispatchEventImpl(Container.java:2287)中的java.awt.Component.dispatchEventImpl(Component.java:4687)处于java.awt的java.awt.Component.dispatchEvent(Component.java:4687) . 位于java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)的LightweightDispatcher.retargetMouseEvent(Container.java:4832)位于java.awt.Container.dispatchEventImpl的java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) .java:2273)位于java.awt的java.awt.Window.dispatchEventImpl(Window.java:2719),位于java.awt的java.awt.Component.dispatchEvent(Component.java:4687) . 位于java.awt.EventQueue $ 3的java.awt.EventQueue.access $ 200(EventQueue.java:103)上的EventQueue.dispatchEventImpl(EventQueue.java:735),java.awt.EventQueue $ 3的java.awt.EventQueue $ 3.run(EventQueue.java:694) . java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:)中的java.security.AccessController.doPrivileged(Native Method)运行(EventQueue.java:692) 87)java.awt.EventQueue $ 4.run(EventQueue.java:708)at java.awt.EventQueue $ 4.run(EventQueue.java:706)at java.security.AccessController.doPrivileged(Native Method)at java.security . 保护域$ 1.doIntersectionPrivilege(ProtectionDomain.java:76)位于java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)java.awt.EventDispatchThread.pumpEventsForFilter的java.awt.EventQueue.dispatchEvent(EventQueue.java:705) (EventDispatchThread.java:161)在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)at at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

1 回答

  • 0

    从Netbeans运行它时,您的wmv文件作为单独的独立文件存在 . 这可以由外部视频播放器播放 .

    当您将应用程序打包到jar中并将其作为jar运行时,wmv将被打包到jar中,您创建的 f 文件将引用该jar条目 . 此jar条目将不可用于外部视频播放器/可解释 .

    你必须提取wmv,将其保存为临时文件并打开它 . 或者不要将视频文件包含在jar中,将其放在jar旁边 .

    以下是将视频提取到临时文件的方法:

    File tempFile = null;
    try (InputStream in =
            getClass().getResourceAsStream("/PicturePackage/admin_input.wmv")) {
        Path temp = Files.createTempFile("temp", ".wmv");
        Files.copy(in, temp, StandardCopyOption.REPLACE_EXISTING);
        tempFile = temp.toFile();
        // This will try to delete the file when you close your java app
        tempFile.deleteOnExit(); 
    } catch (Exception e) {
        // Handle the exceptions properly
    }
    
    // Here you can use tempFile to open it
    if (tempFile != null) {
        try {
            Desktop.getDesktop().open(tempFile);
        } catch (IOException e) {
            // Handle exception
        }
    }
    

相关问题