簡體   English   中英

執行打包在jar中的WMV文件

[英]Executing wmv file packaged in jar

我要執行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
}
}

這是我得到的stacktrace

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),位於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)位於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:138)上的java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

從Netbeans運行它時,您的wmv文件作為一個單獨的獨立文件存在。 可以由外部視頻播放器播放。

當您將應用程序打包到jar中並作為jar運行時,wmv將打包到jar中,並且您創建的f文件將引用該jar條目。 此jar條目將對外部視頻播放器不可用/無法解釋。

您必須解壓縮wmv,將其另存為臨時文件並打開。 或者不要將視頻文件包含在罐子中,將其放在罐子旁邊。

將視頻提取到臨時文件的方法如下:

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