简体   繁体   English

执行打包在jar中的WMV文件

[英]Executing wmv file packaged in jar

I want to execute the wmv video file present in PicturePackage. 我要执行PicturePackage中存在的wmv视频文件。 i am using following code: 我正在使用以下代码:

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

This code runs and plays video when I run in netbeans. 当我在netbeans中运行时,此代码将运行并播放视频。 But when i execute it via jar file build by netbeans, it does not run the video file. 但是当我通过netbeans构建的jar文件执行它时,它不会运行视频文件。 Any specific issue I am not taking care of??? 我没有处理的任何特定问题???

EDIT: 编辑:

I tried this one 我尝试了这个

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

And this is the stacktrace I get 这是我得到的stacktrace

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

When you run it from Netbeans, your wmv file exists as a separate, independent file. 从Netbeans运行它时,您的wmv文件作为一个单独的独立文件存在。 This can be played by an external video player. 可以由外部视频播放器播放。

When you package your application into a jar and run it as a jar, the wmv will be packed into the jar, and the f file you create will refer to that jar entry. 当您将应用程序打包到jar中并作为jar运行时,wmv将打包到jar中,并且您创建的f文件将引用该jar条目。 This jar entry will not be available/interpretable to an external video player. 此jar条目将对外部视频播放器不可用/无法解释。

You have to extract the wmv, save it as a temporary file and open that. 您必须解压缩wmv,将其另存为临时文件并打开。 Or don't include the video file in the jar, place it next to the jar. 或者不要将视频文件包含在罐子中,将其放在罐子旁边。

Here's how you can extract the video to a temporary file: 将视频提取到临时文件的方法如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM