Why does getResourceAsStream() not work in JAR files? - java

I have the following code in a static method:
clips.open(AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("folder/sound.wav")));
Also, folder is in the same directory as Sound.java. When I run the program in Eclipse, the sound is played. However, when I export the file to a JAR file, the sound no longer plays.
If I change getResourceAsStream() to getResource(), both Eclipse and the JAR file play the sound. Why does this happen? I have read around and many people suggest that getResourceAsStream() simply doesn't work in JAR files. Is this the case, and if so, why not?

I suspect that the issue is that Class.getResourceAsStream(String) returns an InputStream, and Class.getResource(String) returns a URL. Thus, in the first case, we are in effect using
AudioSystem.getAudioInputStream(InputStream inputStream)
and in the second case, using
AudioSystem.getAudioInputStream(URL url).
Why does this matter? I think the main issue is that when the InputStream is the parameter, mark/reset tests are done on your audio file and it is failing. When the URL is used as a parameter, this test does not occur. You might check out the API for AudioSystem.getAudioInputStream to confirm what I am reporting here.
Do you get an error message when the getResourceAsStream fails? Does it mention the mark/reset test error by chance? If you are getting a different error message then this might not be the correct answer and something else is going on.

Related

JOrbis can't read ogg file from JAR

I want to play an .ogg file, so i found this piece of code.
So it downloads .ogg file from the given url and plays it. I've tried it with external files like
ExamplePlayer player = new ExamplePlayer(new File("D:\\sound.ogg").toURI().toURL().toString())
and it works. But when i try this with internal files and get it using
ExamplePlayer.class.getClassLoader().getResource("sound.ogg")
it says that "There is a hole in the first packet data.". I think maybe it doesn't work because of JAR's compression or something.
So questions are: why it doesn't work? how could i fix it? If i can't fix it, is there any other way to play .ogg files using java? Thanks.
UPD: I found a lib, but the problem still the same, it cannot read from jar file. It
I'm looking at an application I wrote several years ago that uses ogg resources, and seeing that I first import the ogg file to an InputStream object using getResourceAsStream method.
Usually with wav files, importing via the URL is preferred. Unfortunately I don't recall why I did it this way--too much new tech under the bridge. First guess is that it's a requirement from the jcraft code, otherwise I would have used my preferred method.
Even if this works in the context of an IDE, IDK if it will also work after putting the code into a JAR. getResourceAsStream is often dicey in jars.
Before I invest more time, please let us know if switching to getResourceAsStream does the trick. Maybe the fix is that simple!
+++++++++++++
EDIT: Ugh. Looking at my code, I see where I copied a class from JCraft and edited it, supplying an InputStream via wrapper code. The edit was made in order to output float PCM data from the decoder rather than having the decoder play the sound. The float PCM is then saved in a custom object similar to AudioCue, and the app I wrote this for uses that for playback. All I can remember was that it took a lot tweaking to get this to work.

AWS Lambda function can't read my .bin files (Java)

I wrote a program in Java and deployed it to AWS Lambda by creating a JAR file containing all the necessary files. My request handler calls a method that tries to create 4 new FileInputStreams, with .bin files as inputs, in a try-catch. For example: InputStream stream = new FileInputStream("random.bin"). However, it's going to the catch statement after trying to create the first one due to an IOException. I have my class files in src/main/java/package. I had the .bin files in the directory that contains the src folder, and I also tried putting them all in one folder, but that didn't resolve the problem. Very confused how to resolve this problem. It's also not working when I call to read in a text file, so it's not just limited to binaries. I was thinking of putting the binaries in my S3 bucket and somehow reading them from there, but I haven't found anything much online detailing how to do that in Java.
Thanks!
Your jar puts files in different locations then what your expect. This question might offer so insight on how to correctly access files in the jar.
AWS Lambda only allow write access in the /tmp folder of your function's filesystem. Try changing your code to match this path and try again.

A library I am using is opening an input stream and not closing it. How can I close it?

Every few days, I get a SocketException, too many files open. I tracked the issue to a temporary pdf file that is being used during a certain process. The process passes a name of a temporary file that the library creates. At some point, the library opens an input stream but doesn't close it. Given that my code only has the name of the file, is there any way for me to close the stream?
Details:
Java Web App running in Tomcat6
The best approach is to request a version of this library with this bug fixed.
If this is not possible, get the sources, fix the bug yourself.
If you can't (only a binary jar file), try a tool like jd-gui, decompile the faulty class, fix, recompile that class and replace the .class in the jar.
If it still does not work use ASM and add a close statement at the right place. THIS SOLUTION SHOULD BE AVOIDED. It's complex if you do not master this technology.

Opening file after renameTo intermittently throws FileNotFoundException

I have code that renames a file and then immediately attempts to open it.
On Windows XP this sometimes (but rarely) throws a FileNotFoundException.
The return value from renameTo is true.
Is this a known issue? Perhaps there can be a delay in the filesystem after a rename succeeds before the file actually appears?
Thanks
I have the same problem on Linux, moving files within a CIFS filesystem. The equivalent method Files.move(java.io.File, java.io.File) from the Google Guava library seems to not have this problem. What makes Guava better is that it explicitly copies the file from the source to the destination byte by byte if renameTo() doesn't return true. When I started writing this answer I didn't know that Guava delegated to File.renameTo() first, but still: no problems with Guava.

java.util.zip.zipexception : unknown compression method

I am working with a third party API http://jortho.sourceforge.net/
While using the API, I always get an exception
java.util.zip.zipexception : unknown compression method
There are no zip files involved, I tried not using the .jar file of the API, instead using the class files directly, but it still gives me the same error.
What possibly could be the reason? How can I start off with my debugging?
There is a class WordIterator which uses java.util.zip.InflaterInputStream but I don't think the problem is with the API.
I am stuck! What can be the possible way out?
I sounds to me as if someone is trying to unzip a corrupted zip-file/stream, or something which isn't a zip-file or stream at all.
Since JOrtho is GPL, your best shot is to get the source code and debug the thing yourself. In Eclipse, you can set an exception breakpoint on ZipException and the debugger will stop immediately before the exception is thrown.

Categories

Resources