I have the following code
System.out.println("Path::: "+path);
InputStream resource = this.getClass().getResourceAsStream(path);
Now path is: src/resources/FF/1-Battle.mp3
This is my folder structure:
src
|- resources
| |- FF
| | |- 1-Battle.mp3
|- folder
| |- prog.java <- this is the place where the code is run
The problem is that resource (The input stream) is ending up null
This needs to work from a jar file so i need it to be relative from the jar file root directoy not the hard disc directory, and hence i cannot create a File since inside a jar file the files are not classifiable under java.io.File
Why can't you just read it using FileInputStream
? Something like this:-
InputStream fileStream = new FileInputStream(new File(path));
and make sure, the path is a proper relative URL.
Did you try using a relative path to where the program is executed? Like '../../resources/FF/1-Battle.mp3"?
It looks like you are trying to read an mp3 file from within a jar file since the mp3 file is within your src folder. Try this:
try {
ClassLoader cl = this.getClass().getClassLoader();
InputStream resource = cl.getResource("resources/1-battle.mp3").openStream();
}
catch(Exception ug) {
System.out.println("Failed to load mp3 from jar:" + ug.toString());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.