How to create subfolders with a build path in eclipse - java

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

Related

Where is the right way that should you put your resources (i.e Images and Sound files) when trying to create a JAR file?

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.

loading image from a project's folder (not bin) in java

I imported some images into my project javaproject under the folder name images. Now, since earlier the paths were referring to C: drive, I figured I've to change the path. I tried using
this.getClass().getClassLoader().getResourceAsStream("images\\img1.jpg");
I'm not sure what happened there. There was no error yielded whatsoever, but the image is not displaying. I'm fairly new to programming in general so I can't think alternate methods.
How do I re-direct it to the main directory of my project and into my images folder?
Also, I cant have a strict location like:
c:/users/me/java/etc because if this JAR is ever on different computer it won't work.
Any help is appreciated. Thanks!
if you want to use the above approach then all resources files have to be stored under src/main/resources

In Java where do I put my sprite files to make this open source code work?

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.

Runnable JARs missing Images/Files (Resources)

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!

Eclipse IDE project directory structure help [Java]

I have been learning Java and have had no problem with projects in which multiple .java files were in the same Default Package.
I am now trying to separate code and create folders for images, but everything I have tried has failed.
How do I properly add folders for images and other classes, and properly set the path to it?
All paths should be relative to the project, I know that much ;_;
Here's a link to a picture of my IDE and error message from program output:
http://img262.imageshack.us/img262/8415/directory.png
Thanks!!
First off, your Java code itself should go into a package, not the default package. Next, you may be able to refer to the image file by prepending src/ to your path:
"src/Textures/Crate.png"
But better would be to get the image as a resource, not as a file using the Class#getResourceAsStream.
Drag using the mouse your Textures folder to the LWJGL 6 project folder.
Try giving src/Textures/Crate.png as the path to the FileInputStream constructor.

Categories