Hack Forums
[Java] Cant play mp3 files using absolute path (after building executable jar) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Cant play mp3 files using absolute path (after building executable jar) (/showthread.php?tid=5694556)



Cant play mp3 files using absolute path (after building executable jar) - Account of the Gods - Aug 14, 2017

Code
C:\Windows\system32>java -jar C:\\Stickmen.jar
Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Lau
ncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImp
l.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherIm
pl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(
LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: MediaException: MEDIA_UNAVAILABLE : C:\Windows\system32\menuMusic.mp3
(The system cannot find the file specified)
        at javafx.scene.media.Media.<init>(Media.java:407)
        at MenuScene.<init>(MenuScene.java:25)
        at Stickmen.start(Stickmen.java:38)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162
(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Platfor
mImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.
java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformI
mpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatch
er.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.ja
va:191)
        ... 1 more
Exception running application Stickmen

C:\Windows\system32>
C:\Windows\system32>



I used some workaround but that would cause just more errors.

Whats your way to handle it?


* - GenericSkid - Aug 14, 2017

Not a solution but, ehy are you storing your audio in System32? That seems like a really odd place to be doing things, plus for file IO it requires higher privileges. Why not store the audio relative to your application?


* - Mohinder - Aug 15, 2017

You might want to store your files on a more solid place. You could create a folder in the user.home directory and create a path to that.

If you use
Code
System.getProperty("user.home") + "\" + folderName + "\menuSound.mp3"
you will have a much more dynamic path.


* - Account of the Gods - Aug 15, 2017

(Aug 14, 2017 02:41 PM)Lord Pankake Wrote:  Not a solution but, ehy are you storing your audio in System32? That seems like a really odd place to be doing things, plus for file IO it requires higher privileges. Why not store the audio relative to your application?

nonono. It stores it there automatically. I have mp3 file in "default package" and instead of path I have "file.mp3" - it works from IDE but when i run it from .JAR then it reads system32/file.mp3 or system32/game.jar!/file.mp3 (notice the weird "!")


(Aug 15, 2017 12:30 AM)Mohinder Wrote:  You might want to store your files on a more solid place. You could create a folder in the user.home directory and create a path to that.

If you use
Code
System.getProperty("user.home") + "\" + folderName + "\menuSound.mp3"
you will have a much more dynamic path.

Is there a way to run it from the JAR im providing to the user? instead of messing with his local files. im not creating a hack.


* - GenericSkid - Aug 15, 2017

(Aug 15, 2017 02:09 AM)foreignseo Wrote:  nonono. It stores it there automatically. I have mp3 file in "default package" and instead of path I have "file.mp3" - it works from IDE but when i run it from .JAR then it reads system32/file.mp3 or system32/game.jar!/file.mp3 (notice the weird "!")

Do you mean it's checking that location, or it actually does put things in there? Either way that's exceptionally odd unless you're specifically telling your program to use "system32" as your "user.home" (basically, the execution directory). Would you mind posting some code? It could just be a case of mis-handling paths.

(Aug 15, 2017 02:09 AM)foreignseo Wrote:  Is there a way to run it from the JAR im providing to the user? instead of messing with his local files. im not creating a hack.

Use Class.getResourceAsStream.
PHP Code

<?php 
InputStream in 
= getClass().getResourceAsStream("/file.mp3"); 
// handle stream input and send to whatever audio library you're using 



* - MindOfLogic - Aug 15, 2017

You might not have permission to your system32 directory from eclipse?


* - Mohinder - Aug 15, 2017

(Aug 15, 2017 02:09 AM)foreignseo Wrote:  nonono. It stores it there automatically. I have mp3 file in "default package" and instead of path I have "file.mp3" - it works from IDE but when i run it from .JAR then it reads system32/file.mp3 or system32/game.jar!/file.mp3 (notice the weird "!")



Is there a way to run it from the JAR im providing to the user? instead of messing with his local files. im not creating a hack.

Im not saying this is a hack or something. You can copy your files to a folder you create in the user.home directory. From there you can run it, or just use the resources out of your jar file.