我想执行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
}
}这就是我得到的堆栈痕迹
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( 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)在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.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)在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)在java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.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 ).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) 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 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
发布于 2014-07-31 07:29:33
当您从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
}
}https://stackoverflow.com/questions/25053023
复制相似问题