1

I want to execute the wmv video file present in PicturePackage. 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. But when i execute it via jar file build by netbeans, it does not run the video file. 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

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

ashu
  • 1,197
  • 2
  • 14
  • 30

1 Answers1

0

When you run it from Netbeans, your wmv file exists as a separate, independent file. 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. This jar entry will not be available/interpretable to an external video player.

You have to extract the wmv, save it as a temporary file and open that. 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
    }
}
icza
  • 389,944
  • 63
  • 907
  • 827
  • Is there any other way to run the file in jar. If not, then how can we "extract" from the jar??? Thanks for your conncern – ashu Jul 31 '14 at 07:32
  • External video players typically do not support playing a video file packed in a zip or jar file, so I woudn't count on that. Easiest would be not to pack the video in the jar, simply place it next to the jar file. – icza Jul 31 '14 at 07:34
  • Edited to show how to extract the video to a temp file. – icza Jul 31 '14 at 07:43
  • Not working. I used JoptionPane to get the exception. It says FileAlreadyExist Exception. Despite I deleted all files in temp folder – ashu Jul 31 '14 at 07:53
  • `Files.createTempFile()` generates a unique, non-existent file. Please post your stack trace because it is unclear for me where the exception occurs. – icza Jul 31 '14 at 07:57
  • How do I write stack trace in comments. it allows only 600 characters and stack trace is pretty large... – ashu Aug 01 '14 at 16:52
  • Add it to your question for example. – icza Aug 01 '14 at 17:07
  • Added to the question the complete output of e.printstacktrace(). I guess the problem comes in copying the file because temp file is already created during createtempfile() – ashu Aug 01 '14 at 17:15
  • I added Files.delete(temp); after Path path line and it starts working. – ashu Aug 01 '14 at 18:09
  • Ah, now I know. Obviously `createTempFile()` creates the file, so we have to tell the `Files.copy()` to overwrite that with the option: `REPLACE_EXISTING`. Edited. – icza Aug 01 '14 at 19:51
  • Thanks icza, that works. Meanwhile I found another code and want to know which one would be better – ashu Aug 02 '14 at 07:20
  • String temppath= System.getProperty("user.home")+"\\AppData\\Local\\Temp\\admin_input.wmv"; InputStream in = getClass().getResourceAsStream("/PicturePackage/admin_input.wmv"); File f= new File(temppath); try { OutputStream out= new FileOutputStream(f); byte[] buffer = new byte[4096]; int len = in.read(buffer); while (len != -1) { out.write(buffer, 0, len); len = in.read(buffer); if (Thread.interrupted()) { throw new InterruptedException(); } } } – ashu Aug 02 '14 at 07:21
  • My solution is simpler and more efficient, use mine. – icza Aug 02 '14 at 11:41