This question already has answers here:
How do I read a resource file from a Java jar file?
(9 answers)
Closed 8 years ago.
OK guys, so I'm trying to compile my game into a jar file, but I can't get the loading of images to work. When run from NetBeans, all is fine. But in the JAR, the URL is always null.
Here is the code I'm using:
URL url = this.getClass().getResource("/textures/Lava.jpg");
BufferedImage sourceImage = null;
try
{
sourceImage = ImageIO.read(url);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
I have tried unziping the JAR and checking the contents, my textures folder is there and the images inside also. Any ideas what I'm doing wrong?
Previously answered here:
Accessing a file inside a .jar file
Better use
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/" + filename)))
you should get the URL path as relative path of your system.
Related
This question already has answers here:
Java, display chm file loaded from resources in jar
(1 answer)
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 1 year ago.
So I have created a java application in eclipse and I use javafx, and then exported it in a runnable jar file. However, when I run my .jar file it gives the error of not finding my images. It works perfectly fine when I run inside eclipse.
My file structure:
In UserPane I have a function that takes the image name as "title" and returns the imageview:
InputStream is=null;
try {
is=new FileInputStream("./Images/"+title+".jpg");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image img=new Image(is);
ImageView imgview=new ImageView();
imgview.setImage(img);
imgview.setFitHeight(300);
imgview.setFitWidth(300);
return imgview;
I have tried some solutions, but it won't run in eclipse. I need it to run in eclipse and from the .jar.
I need it to be set in the imageview please.
Update: I did not use any of the suggestions mentioned because none of them were helpful, none of them worked. What worked for me is moving my .jar in the same folder where my images folder is, while still keeping the above code.
Thank you
FileInputStream is called that because it operates on files. In java, a file means exactly what it says: A file is a thing on a disk someplace.
An entry in a jar file is NOT ITSELF a file!
Therefore, when you write new FileInputStream? You lost the game. That can never work.
What you're looking for is a thing that lets you ask the JVM to give you resources from the same place the JVM is loading your class files.
Fortunately, that exists!
URL url = UserPane.class.getResource("/Images/" + title + ".png");
This gets you a url object, which you can pass to e.g. ImageIcon.
If you want to instead read it directly:
try (InputStream in = UserPane.class.getResourceAsStream("/Images/" + title + ".png")) {
// use 'in' here. It's `null` if the resource doesn't exist.
}
For this specific use case, you're looking for the URL variant. You don't need the InputStream at all.
This question already has answers here:
Read properties file outside JAR file
(8 answers)
How to get the path of a running JAR file?
(33 answers)
Closed 6 years ago.
I have a folder structured like this:
MyFolder:
file1.xml
file2.xml
project.jar
But if in a class I use:
File f = new File("file1.xml");
I receive an error, because it doesnt find the file. Why?
You should use a relative path in your code.
Example: File f = new File("./file1.xml");
If you are using Windows the code you posted will work, but not on Linux where the default parent file is your home.
But you can do in any OS by using:
public class MyClass {
public void loadFile() {
URL url = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
File jar = new File(url.toURI());
File f = new File(jar.getParent(), "file1.xml");
//your code
}
}
PS: This needs to be inside project.jar because you are getting the location where you jar file is.
This question already has answers here:
Get path of Android resource
(3 answers)
How can I write a Drawable resource to a File?
(2 answers)
Closed 8 years ago.
I am writing an Android app and I need to do something I would consider incredibly simple, yet am having the hardest time figuring out.
How can I obtain the path to a JPG file so that I can upload it to a server?
The file is snoopy.jpg and is in the res/drawable folder
I've tried:
File sourceFile = new File("android.resource://com.appname.something/drawable/snoopy");
But, that's not a file
I've tried:
File sourceFile = new File("drawable/snoopy");
and that doesn't work.
I tried putting snoopy.jpg in the root of the app directory and attempting:
File sourceFile = new File("/snoopy.jpg");
And that still didn't work.
Any help would be greatly appreciated!
Try this :
InputStream is = getResources().openRawResource(R.drawable.snoopy);
You can open an InputStream from your drawable resource using the above code. Also, before doing any file operation, you need to check if file.exist() and if it returns false then you need to create a file via f.createNewFile();
This question already has answers here:
How do I read a resource file from a Java jar file?
(9 answers)
Closed 9 years ago.
I'm writing a game in java and all is working more or less well, as you would expect when coding, but one issue I have come across is reading the image and sound files into the game. I wrote a method that works really well within Eclipse, but as soon as I try to test it in a runnable jar (exported using Eclipse) my game gets stuck on the loading screen because it can't read the files. I realized the problem there being that you cannot create File objects within a Jar, you have to use streams. This could work, but the images/sounds are located within folders and I'm not very sure how you can read files within a folder with streams(i.e not knowing the amount of files). Because of this I have tried to rewrite the method using the ZipInputStream (I found this as recommended when looking for solutions prior to posting this). This is what the method looks like now:
imageMap = new HashMap<String, BufferedImage>();
String imagebase = "/images";
CodeSource src = PlatformerCanvas.class.getProtectionDomain().getCodeSource();
if(src != null)
{
URL jar = src.getLocation();
try
{
ZipArchiveInputStream zip = new ZipArchiveInputStream(jar.openStream());
ZipArchiveEntry ze = null;
while((ze = zip.getNextZipEntry()) != null)
{
System.out.println(ze.getName());
if(ze.getName().startsWith(imagebase) && ze.getName().endsWith(".png"))
{
loading++;
imageMap.put(ze.getName().replaceAll(imagebase + "/", ""), ImageIO.read(this.getClass().getResource(ze.getName())));
}
}
zip.close();
System.out.println("Finished Loading Images");
}
catch (IOException e)
{
e.printStackTrace();
}
}
However, this completely skips over the while loop because getNextZipEntry return null. I tried using the base Zip archive libraries, but someone recommended the Apache Commons libraries, but neither worked. My Jar setup is this:
Jar
/game/gameClasses
/images/imageFiles
/sounds/soundFiles
/whateverOtherFoldersEclipseCreated
So my question is this: Why isn't this current method working and how do I fix it? Or, how can I load the images/sounds in a different way that works?
I have looked at many other questions and tried many different things, but none worked for me.
Or, how can I load the images/sounds in a different way that works?
One common technique is to include a list of resources in a known location in the Jar. Read the list, then iterate the lines (presuming one line per resource) & read the paths that way.
This question already has answers here:
How to create a folder in Java?
(8 answers)
Closed 3 years ago.
I tried to use the File class to create an empty file in a directory like "C:/Temp/Emptyfile".
However, when I do that, it shows me an error : "already made folder Temp". Otherwise, it won't create one for me.
So, how do I literally create folders with java API?
Looks file you use the .mkdirs() method on a File object: http://www.roseindia.net/java/beginners/java-create-directory.shtml
// Create a directory; all non-existent ancestor directories are
// automatically created
success = (new File("../potentially/long/pathname/without/all/dirs")).mkdirs();
if (!success) {
// Directory creation failed
}
You can create folder using the following Java code:
File dir = new File("nameoffolder");
dir.mkdir();
By executing above you will have folder 'nameoffolder' in current folder.