Java getting a .wav file to run inside a .jar [duplicate] - java

This question already has answers here:
Load a resource contained in a jar
(3 answers)
Closed 8 years ago.
I am trying to get my .wav file to run from inside a .jar file once I have exported the program, and have researched extensively on the solution, but have found nothing that works.
Here is my current code:
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File("soundfile.wav")));
clip.start();
What can I do to fix this?

A resource contained within the context of a Jar file is commonly known as an embedded resource.
These resources can not be accessed like normal file system resources (ie via the File class), instead, you need to use Class#getResource, which will return a URL, which can be, in you case, passed to AudioSystem.getAudioInputStream
You could use something like...
clip.open(AudioSystem.getAudioInputStream(getClass().getResource("/path/to/resources/soundfile.wav")));
In this example, the path is relative to the package location of the class instance. That means if the resource is in another directory, you will need to to provide a full path to the resource

Related

How to read from resource file outside jar [duplicate]

This question already has answers here:
How to read text file from classpath in Java?
(17 answers)
Closed 3 years ago.
I want to read the configuration file outside the jar, how should I do?
I tried classloader.getResourceAsStream("config.xml") and succeeded while I am running the code inside Intellij. Now I want to build the jars to a folder and place the config.xml under the same folder, not inside the jar, but the program fails to detect the config.xml.
Is there a graceful way of reading the config.xml instead of using File with relative path in the code, which doesn't work while debugging/running inside the IDE?
Yes, turn it into a system property, and provide it to anything running any Java process/application.
Let's say you have a config.xml file located inside /some/path/down/the/line/, then you can do: java --classpath ... -Dapp.config=/some/path/down/the/line/config.xml tld.domain.Application.
Then all you have to do in your Java code is to reference that name/path: final String configFile = System.getProperty("app.config");, and use any well-known routine to read it from there.
Basically, you have to make sure the file/path/location is provided somehow to the Java classpath.

How to get directory of a .java file [duplicate]

This question already has answers here:
How to get the current working directory in Java?
(26 answers)
Closed 3 years ago.
Part of my (java) code needs to access a database. When opening a connection it checks if the actual database file exists (I use sqlite). I want to make my code portable, so I want to avoid hard coding the path of the database file. Is there anyway in java to get the path of the .java file? Because I know exactly where the database is from the .java file accessing it.
I've tried using current directory with File but it doesn't give me the path of the actual .java file. When I use android studio the current directory is different than when I simply use a terminal.
The best way to get the path information would be using Paths and Path class from the java.nio. You can input an absolute path or relative path to the Paths.get(String str) to get the Path.
To get the project directory, you can use:
Paths.get(System.getProperty(“user.dir”))
Paths.get(“”)
It will get the complete absolute path from where your application was initialized.
The Paths.get() method will return a Path Object, on which you can call toString() or toUri() to get the path. Hope this helps.
Javadocs - Paths

File not found in Java [duplicate]

This question already has answers here:
How do I load a file from resource folder?
(21 answers)
Closed 6 years ago.
I have following project structure:
src
--Main.java
--resources
----Users.txt
And I am trying to create a file from Main.java like that:
File file = new File("resources/Users.txt");
However, I never succeed at doing so.
Why?
If it is a regular file outside a .jar, you are using a relative path. That means, the path to the file is formed from the path where you are calling the file from + the relative path. To make it work, you should invoke java within src folder

Accessing resources inside a JAR [duplicate]

This question already has answers here:
How do I read a resource file from a Java jar file?
(9 answers)
Closed 5 years ago.
I'm making a test program for resource loading inside a jar and I'm having problems.
I've read that you should use ClassLoader or alternatively getClass() to access a file inside a jar. Since the method that i use to load the resource is static, I use ClassLoader.getSystemResource(String path).
My program finds the file but says that the path contains invalid syntax for filenames or directory names. This is the code that I use to load my resources:
String path = ClassLoader.getSystemResource(file).getPath();
System.out.println(path);
try {
wave = WaveData.create(new BufferedInputStream(new FileInputStream(path)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
AL10.alBufferData(buffers.get(i), wave.format, wave.data, wave.samplerate);
wave.dispose();
Yes, I'm loading soundfiles for OpenAL.
Anyway, I should remind you that it actually finds the file but the path has invalid syntaxing. This is an example of the path I get when I run it in jar form:
file:\C:\Users\name\development\jars\test.jar!\sounds\test.wav
I've tried to remove the "!" and I still get the same error. If i remove the "file:" it doesn't find the file. Note that the program runs fine when I'm running in Eclipse.
If you are getting a file from a jar use getResourceAsStream() and use the package names for the path.
getResourceAsStream("/com/project/resources/sounds/myfile.wav")
Not every URL corresponds to a file in file system. In particular, a particular entry in a JAR file isn't itself a file, and hence cannot be read with a FileInputStream. You can open an InputStream for a class path resource using Class.getResourceAsStream() or ClassLoader.getResourceAsStream().

Read a file, which is included inside a jar [duplicate]

This question already has answers here:
Java resource as File
(6 answers)
Closed 9 years ago.
I have a jar file, say archive1.jar, resides in /home/ext/libs. This has a couple of classes, and also a text file (data.txt) at the root of the hierarchy of the jar. One of the classes of this jar (say Data.java) reads the file to initiate some internal data structure.
Now, the archive1.jar is included as a library of another program, called Parsing, which is executed in a completely different directory /home/progs (archive1.jar is specified in the classpath of the execution command of the program).
The problem I am facing is that now Data.java cannot understand the path of data.txt anymore, because it assumes that data.txt resides in /home/progs instead of being inside archive1.jar in /home/ext/libs.
My question is what change should I make inside Data.java in order for it to understand that the data.txt is inside the archive1.jar? In short, how to make Data.java be able to read data.txt inside archive1.jar, no matter where the calling program is.
Note: In thread Java resource as file, thing was a bit different, and also there was no answer to how to read the file inside the jar. One said it might be impossible.
The way to access resource files inside JAR archives is by using getClass().getResourceAsStream() . Using this ensures that the resource is loaded from the archive not from the filesystem. For example if you have data.txt in the same location as Data.java within the JAR archive, then you would access the data.txt in this way
InputStream is=getClass().getResourceAsStream("data.txt");
Once you have got an InputStream to the resource proceed with the way you like.

Categories

Resources