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.
Related
this week I realized some new things about working with Java in VSCode, some projects had their bin location in AppData. I eventually fixed the problem, only to realize afterwards it was still happening, but only when I tried to add jar files to my project.
Here is a image of the command being run in a normal project.
And here is a picture of my problem.
This is a picture of my hierarchy.
And yes I have tried to set the bin location of my project in the settings.json in the .vscode folder.
I also added a launch.json but I don't know if its that important.
I really don't understand why this is happening, I don't use code runner, and I edited the classpath configuration here.
Thanks in advance for any help.
Because your project references the jar package. You can Ctrl + click the directory link to view the contents of the file, which is actually the command to use the jar package.
If there is no reference to the jar file, no temporary files will appear in the command.
And your configuration in setings.json is correct, it will specify that the generated .class file is in the bin folder of the current directory.
"java.project.outputPath": "bin",
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.
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.
I have eclipse project.
I add directory src/directory and put there SomeClass.java
I compile my application.
in somedirectory in filesystem I see .class file.
But in eclipse somedirectory looks empty.
What the reason? How to fix it?
I press F5 many times!
eclipse hides class files when you are in the package explorer. Try to switch to the navigator (Ctrl+3 and type "navigator") then a "bin" folder should appear and that one contains your class file.
First, the Eclipse src folder and output folder that will contain the compiled classes are two different folders. These two folders' location are configured in the Project's properties page, under the Java Build Path tab. More information on the configuration can be found here.
So when you press in F5 in Eclipse to refresh the folder, you are basically refreshing the source folder, and you will never see the .class file in there. The output folder that contains the .class files are hidden by the Project Explorer view.
If you absolutely want to see these files via Eclipse, switch to the Resource perspective and follow the instructions as given by the answer of this SO entry or rather follow instructions given by #markusw , CTRL-3 + Navigator view is way quicker!
The hierarchy of the source directory src, should be the same as the package name of the classes inside it. In other words, if you have a class whose package name is com.example.util, then that class should be located inside the directory src/com/example/util.
I'm doing a simple Java project on Netbeans. And I'm stuck not knowing how to add my own image into the code. It kept throwing IOException no matter where I put the image. This sounds ridiculous but it really got me stuck.
If you run the program from netbeans, the root folder should be the same as the root of the project folder. That is, the folder which contains build.xml and manifest.mf.
If putting your image directly in that folder, and loading it without any path given doesn't work, then you will have too look at your code and see if you can find any errors.
Try looking at you project directory and find where the html/xhtml files are. Mostly it will be located inside the web directory within the project directory projectname/build/web. That will be source for the image tag in your html page you can directly give the name of the image for "src".