When I export my code as runnable JAR from eclipse all the files that I've set it to grab such as button images and other files are missing even though they are actually in the JAR. I've added getClass().getResource in front of the files but then when I try to run the JAR nothing even happens, any suggestions?
Seems like you not putting your stuff in the right sense. In order to make it work, follow these steps :
Right-Click your Project in Project Explorer Tree.
Go to New -> Source Folder and then provide any Name to the Source Folder.
Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
Name this New Folder as images and copy your images to this Folder.
Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.
Now in order to access, say any image, you will use.
getClass().getResource("/images/yourImageName.extension");
which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.
The path needs to be right for the resource.
For "foo.gif" being at the root of the jar, you must refer to it using "/foo.gif".
If the program works correctly after a complete clean and rebuild, but fails as a jar, you most likely do not have the files included in the jar.
Try to put the folders in the jar the same way that you got them in the program. Put in the same resources in the same places that you have them in the project. The jar will reference to them the same way as in your compiler did.
You need to get the images using stream like this -
this.class.getClassLoader().getResourceAsStream("test.jpg") and make sure the images are present in the jar which you are referencing.
As nIcE cOw said, you just need to create a Source Folder in you Project Explorer Tree.
All the files inside that folder will be in the root project folder.
To refer to them, you must write your projects name slash the file name as it:
getClass().getResource("ProjectName/image.extension");
I hope this helps!
Related
I am using Intellij IDEA and whenever I build a JAR artifact it places itself on the productions folder. When I try to run the JAR file some custom defined functions (i.e. setting Icons on Components and adding sounds) are missing, while running it on the Intellij's IDEA is functioning correctly. I know that it involves using getResourceAsStream method but is there some sort of right way to place external resources?
Additional questions I may add, where is the correct destination for placing the META-INF folder? Should I make a resource folder for all my external files?
This is for Eclipse users, I believe the same thing can be accomplished in other IDE's relatively easily.
Create a resources folder
Put your non-code files in it
Use as "Source Folder"
After creating the JAR, the contents of "resources" will be packed in the JAR.
I want to create two main folders under src folder. When i try this it creates in normal way as seen in image.
How can i create folders in this way.
src/main/java
src/main/resource
I have tried searching google and other websites. Nothing seems working. Also the option when you press ctlr+F10 doesn't seem to solve my problem.
main>Folder --Right click--Build Path and select
use as a source folder
I need to create an application for sorting various types of polygons using various parameters such as height, volume or base area. Arguments for Filename which has parameters for polygons, Sort type, Sort method will be pass through command line.That file is in my resource folder outside my src folder in a project. I have implemented all programs, It works fine when I run using pass arguments through eclipse run configuration. But when I try to run my .jar file using cmd same arguments it gives me FileNotFoundException.
I opened my jar file using 7zip and noticed it never extracted my resource folder in .jar file. I searched online and tried including my resource folder in to build path of eclipse. But still does't work.
Follow these steps:
1) click project -> properties -> Build Path -> Source -> Add Folder and select resources folder.
2) create your JAR!
EDIT: you can make sure your JAR contains folder by inspecting it using 7zip.
Reefer this link as well How do I add a resources folder to my Java project in Eclipse
First, you need to create a source folder for resources, for instance name it res, and then move your image folder to res. When you generate the jar file, you will see the image folder, not the res folder or files separately.
This comes down to how you are generating the JAR file.
When you're exporting the jar in eclipse make sure to checkbox the button that says "Export java sources and resources" https://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/ensureJavaFiles.jpg
There are a lot of ways to do this one is to use Gradle is the recommended way, something like this will work Creating runnable JAR with Gradle
I've currently finished my project, but can't get it to work when it is exported. I use JAXB to read and write XML Files and also have dependencies on other external Folders, which are needed to use a POS-Printer.
I've managed to link my external XML Save-Files with absolute paths, but not with relative paths. So that worked, although not the way i wanted. Yet, using the external class folder for the printer didn't work at all.
This means, that in my Eclipse Project Build Path i've added a class folder, which contains all of these needed files (which are not only jars, so adding them one by one wouldnt work). So exporting my project to a jar either includes all the files into the jar itself, or doesnt include them at all.
Everything works perfectly in Eclipse, but not when i export it.
My folder structure looks like this:
src
/model
/view
/control
data
/articles.xml
/...
JavaPOS <--- needed folder with all its files
/jpos.xml
/xerxers.jar
/swt-..-.dll
I've tried:
InputStreams is = getClass().getResourceAsStream(url);
absolute paths
manipulating the manifest file and/or jar structure
runnable and non runnable jars with nearly every combination of options
putting the files inside the library "by hand"
changing the build path of the project
My Question is:
How do i get my jar-file to know where these files are?
EDIT:
Do you think Maven or an Ant file could solve my problems? I don't have any experience with those.
The Problem was, that i had more than one JRE installed and that the one eclipse was using, had all the dll files, but the other ones didnt have it. So i had to add them manually, because reinstalling the drivers of the printer didnt change anything. Gotta fix that somehow, but right now it works and that is all i wanted.
Turns out i didn't even need that Folder, just needed one file out of it and the missing dlls.
I am a beginner and I am trying to learn by messing around with some open source game code.
I was setting it all up in Eclipse but I don't know where to put these sprite gif files.
In the code I found this:
URL url = this.getClass().getClassLoader().getResource(ref);
And when I put all the class files under a java project together and tried to run I got this error message:
Can't find ref: sprites/ship.gif
Of course the code came with sprites including ship.gif. I just don't know where to put it. I tried making a folder under the java project called sprites and putting it in there.
I don't have a res folder.
The this.getClass().getClassLoader().getResource method look for relative path of the data from the package of the class.
Assuming your class is com.my.package.MyClass, you usually have your project organized containing at least in your case:
com/my/package/MyClass.java
com/my/package/sprites/ship.gif
Most of the time, you certainly have a resources or a images folder for your java project.
You can only load those resources if they are on the classpath. Try to add the sprites folder as a source folder on your Eclipse build path and try again.
Put the .gif in the jar.
This can be achieved by creating dedicated folder you add in the sources of eclipse. Often this folder is called "resources".
Be ware that in some cases a "/" is required at the begining of ref.
Thanks everyone.
How I fixed my problem:
When I downloaded the source I opened folders and went to the class files and copied them to my project and then I was trying to do the same thing with the sprites. What I didn't realize is that if I just drop the two main folders "org" and "sprites" into the source file of my project then it all works on its own. These two folders were the first things I had after downloading this open source code.