Location of image in exported project folder - java

In my current code I write the specific location of the images I want to use in my project, now, these locations are only correct until I move the image to a different directory or open the application in a different computer.
Where do I place the images (where to place them in the exported folder) I want to use in my project?
The exported project is a zip file, once extracted, I have 2 folders within the extracted folder, one named nbproject other is src, one text file named build another .mf file called manifest inside nbproject I have two text files and two .properties files inside src I have my four classes.
Where do I put the images I want to use in the project? And once I place them, what directory do I write in my project?
Here's an example of how I use an image:
// Main menu background image
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
And then I draw the image all over the screen
My exported folder contents:
http://i.imgur.com/qd4PeJo.png
http://i.imgur.com/W5YQ7OC.png
EDIT: Thought it worked but it keeps giving an exception on other people's computers, perhaps this isn't the reason beause I moved the images around in my PC and it worked but still.

Since you are writing the directory in your code.
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
When you run the project in another computer or move the image to another location, the program can't find the path to the picture, so there is an error. Instead of using the full path. Copy the images to your project folder and use relative paths.For instance : "images/Vanguard.jpg".

The path where your program looks for resources can depend on how you run the program. If you ran it via console/terminal the initial path starts at the path of the *.class file you ran. If you run it from an IDE this path may change, in Eclipse the path starts at the Project dir and not the src folder. You can find out the exact path by calling System.getProperty("user.dir") that will return the current working path as a String.

Don't use a strict path, use a relative path like so:
Main.java
String path = "Project Images/bg option for Vanguard.jpg";
bgg[0] = new ImageIcon(getClass().getResource(path)).getImage();
In order for this to be loaded, you must place a folder called Project Images in the directory in which your Java files are. Then place the image bg option for Vanguard.jpg in this folder. Be sure to compile the program in you IDE so it can make a copy for the compiled version.

Related

Is the relative path for a java project different for Intellij and cmd?

I have a java assignment, and at a specific point, we have to create a new folder and write some text files there. The issue is, when testing my code on Intellij, it works fine, but when testing it on cmd, I need to change it a tad bit?
My project structure:
.../project/src/greedycomparisons // greedycomparisons is the file where I'm creating the dir from
.../projct/data // the directory I want to create, to have my text files in
More specifically:
I'm trying to create the new subdirectory from a file I have inside my src folder. Therefore, the thing I tried first was:
File directory = new File("./data/");
if (!directory.mkdir()) dosth; // nothing happens on first call
/* making the String I want as my file's name, say string */
File file = new File(directory, string);
Which works fine (on Intellij), creating the subdirectory (and the files later on) exactly where I want them to. And then, likewise, I try accessing the files I made there from another file in my src folder, by again using "./data" before the name of the file I was trying to access, which again works as expected on Intellij.
But when I tested this on cmd, I need to change the directory name to "../data" in order for the code to work, which in turn does not work on Intellij (specifically giving me a "The system could not find the path specified" error) when I change it to that. Given that I have to submit it as an assignment and I don't want any ambiguity in regards to my files, is there something more "universal" that I can try, so my code works regardless?
If you check your IntelliJ configuration, by clicking at the top where you have the name of the file you are executing, you can click on Edit Configurations, and you will see one of the fields you can configure, called "Working directory".
IntelliJ is smart enough to set it to your project folder, outside the /src folder, because it is not good practice to write data inside your /src folder which should only have source files, unless they are resources you want to commit with your code.
On the other hand, when executing it from cmd, your working directory is just where you are trying to run the compiled program from. You haven't specified in which folder you are in when doing so, but most probably you are inside some subfolder that has the classes, which is why you needed to do the ...
If you package your classes in a .jar file, the way IntelliJ made it should work. If you run your .jar file with java -jar myjarfile.jar and the ./data folder is at the same level, then it would work.

Getting the location of a folder next to my .jar file

In a program I am making, I need to access images in a folder placed alongside it. The program works fine when I run it with Eclipse, but when I export it to a .jar file, it does not get the location of the folder properly.
File roomF = new File("Assets/Rooms/1/0.png");
In the IDE, roomF refers to the correct location:
C:\Users\[Username]\Desktop\Eclipse Java\[Project name]\Assets\Rooms\1\0.png
However, in the .jar file, it refers to this:
C:\Users\[Username]\Assets\Rooms\1\0.png.
How can I fix this?
You should not have resources outside of your JAR or dependencies that need a local path in order to work. Create a resource folder in your project and load the file from there.
There are two solutions to this:
Place your JAR at:
'C:\Users[Username]\Desktop\Eclipse Java'
This way, when you are accessing Assets/Rooms/1/0.png, it points to the correct location.
Make a folder where you will place your JAR everytime. Create the path 'Assets/Rooms/1' in the folder and place your file 0.png there, hence you wouldn't be needing to place your JAR each time you make a new one as said in step 1.

Load image from project folder, not bin folder (Java)

When I call:
Image photo = new Image(getClass().getResourceAsStream("/images/myPhoto.png"));
It seems to look in the bin folder of the class that I called that line in. Of course, there is no images folder in my bin folder and it yields an error. How do I re-direct it to the main directory of my project and into my 'images' folder? I tried adding the folder to my eclipse project and then right clicking and setting 'add to build path' but that just caused more compile errors.
I also don't want to define a strict location like:
c:/users/me/java/etc because if this JAR is ever on another person's computer, it won't work.
Thanks for the help.
.getClass().getResourceAsStream(fileName) considers that the location of the image is the same as the calling class.
So try this:
this.getClass().getClassLoader().getResourceAsStream("images/myPhoto.png")
according to where is your image in the classpath.

How to make a jar runnable in order to save images

I have a problem:
I put my images in my project folder, for example, E:\All Work IT\Java All\JavaWork\TestingDB and when I use this line of code to use the images
ImageIcon icon = new ImageIcon("start.png");
It works perfectly!
But when I want to make a Runnable jar file (Export -> select Runnabable jar file) and open the specific jar, the images don't appear on the buttons, menu, and so on.
Where do I need to put my images to solve this problem, and with what code?
Thanks.
new ImageIcon(file) specifies a local file which won't work inside a JAR.. Try changing the line to use a the class loader.
new ImageIcon(getClass().getResource("/start.png"));
If you're in a static context
new ImageIcon(Foo.class.getResource("/start.png"));
I'm unsure of your project structure or IDE, if any, so I'm unable to make any recommendations on where images should go. Just make sure the images are included in the JAR export and end up at the top level, or structure matching your getResource() call
One if the simplest ways would be to include the images within the jar file itself, this reduces the number of resources you'll need to deploy; much easier to deploy a single jar then a jar file and a bunch if images
It also reduces the possible issues with execution context and having to calculate the relative paths
Who you achieve this will depend on how you are building the jar, for instance in Netbeans and Eclipse, you can copy the images into the src directory. In maven, you'd need to include the images in the resources directory
This will change the way you need to use to load the images or resources
You will need to use Class#getResource or Class#getResourceAsStream depending in your needs, for example
ImageIcon img = getClass().getResource("/start.png");
Remember, you will need to use the full path to the image from the context of the src directory
This means if you place the resources within the images directory under the src directory, then you will need to use /images/start.png for example

Placing application files in bin folder

How can I place the files being used by my application, more particularly text files that contains data that my Java program uses. For the images since it is static, I just copy and paste them in the bin folder. But I have some text files that I create during runtime and I don't know where to place them. I need a place where I can save them in and edit them sometime.
By the way, I am using eclipse IDE.
And how would I code it? Like retrieving etc.
I am reading files with Scanner, creates them with Formatter
If you using Eclipse IDE you can just place the text files in the source folder and eclipse will copy them to the bin folder when building the application. When editing the files in the source folder within Eclipse it will update the copy in the bin folder. When you edited them with an external program you need too choose the “Refresh” menu item in the Eclipse IDE.
Place them relative to the .java file of the class using them so that the copy in the bin folder will be relative to the .class file as well. The you can access them via MyClass.class.getResourceAsStream("path relative to MyClass.class"); which gives you an input stream. This works even if you package your application as a JAR file.

Categories

Resources