I am new to java and I have been learning things recently that gets me started up like packaging, naming convensions, resources folder and similar stuff the right way.
My question right now is,
I see some people using the libraries/natives folder like this
Project -> src -> libraries
and as well
Project -> src -> natives
but some other people do have them in a resource folder like so
Project -> src -> resources -> libraries
and as well
Project -> src -> resources -> natives
So I am not sure which one is more correct, but I guess it should be in resources, since they are part of them. Please explain to me.
A common convention is the Maven Standard Directory Layout, core part being:
src/main/java Application/Library sources
src/main/resources Application/Library resources
If you're using Maven, then there is no libraries folder, since Maven is responsible for managing the .jar files, otherwise .jar files and associated native .dll/.so files are usually put in a lib folder next to src. Alternatively, the native .dll/.so files are put in a bin folder, which contains all files that should be on the PATH.
src/main/java Application/Library Java sources (.java)
src/main/cpp Application/Library native sources (.cpp)
src/main/resources Application/Library resources
lib Third-Party libraries (.jar)
bin Third-Party native files (.dll, .so)
and Application/Library scripts (.bat, .sh)
Related
I am new to project environment setup. Below is my project structure in eclipse
Project Name
--> .settings
--> .bin
--> lib
--> resources
--> src
--> .classpath
--> .project
I am attempting to export src folder as jar.
When i export to jar, all the above folders & files are created in jar. But i need to convert only src folder as.
Also when i export to executable jar, all the third party libraries are exported as class files in jar. is it right.
What is the best practice to export project. Only src folder or everything.
Which i need to use jar/runnable jar. My requirement is to write start/stop bat file to call jar and execute java program.
Please advice me. Thanks in advance.
First it's important to know what these folders actually do. Following are the workings of several of these files.
.settings -> This file records project specific settings and workspace preferences.
.bin -> folder is usually where the compiled files are copied to.
lib -> contains external libraries that are used in your project (like Apache Commons)
resources -> the resources like images, text, pdf, audio, video are usually copied here
src -> the folder where the project's source files are located.
.classpath -> It contains information that the JDT feature needs in order to properly compile the project: the project's source folders, the output folders , and classpath entries.
.project -> This file is maintained by the core Eclipse platform, and its goal is to describe the project from a generic, plugin-independent Eclipse view.
So you can see that if you exclude some of the files like lib, resources, bin etc... Your jar file will probably stop working. Your jar file needs compiled files and their dependencies.
For example: All your compiled .class files are in bin folder. And your jar works because of these .class files and NOT .java files that are in src. If you delete this bin folder then your jar will probably stop working.
Also, your project may be using some external library supplied by someone else. Like Apache Commons or google/guava and these are usually in lib folder. So you can't delete this folder as well.
However, if you no longer expect to use .java code, then you can exclude files that were created by eclipse to manage this project. See this post.
see also:
1. What's in an Eclipse .classpath/.project file?
2. exclude files from jar or war in eclipse
what is the difference between these two folders? Why my beans have to go in resources, so my web app to work?
src/main/java Application/Library sources
src/main/resources Application/Library resources
The src/main/java contains your java source codes. that is, your java packages, *.java files.
The src/main/resources contains "resources" file of your project. e.g. properties file, configuration files (xml, ini, conf....) they are in classpath of your project.
Usually the compiled sources (*.class files) and those resources would be in target/classes.
Your web app sources (e.g. Jsp/jspx, js, html...) should go to src/main/webapp
I've created small program in Java. It uses Java classes, some images and generates some other resources (.php files, images, stylesheets etc).
I'm compiling sources as .jar and creating an .exe program, which should use my .jar and other resources.
What is the best practice to organize all sources of my Java program?
I will recommend the below maven structure as the standard convention even if you are not using maven.
src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/scripts Application/Library scripts
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
LICENSE.txt Project's license
NOTICE.txt Notices and attributions required by libraries that the project depends on
README.txt Project's readme
When i created the dynamic web project, i will have 2 folders with name Libraries and lib. So during the project i added .jar files as follows
Build Path --> Configure Build Path--> Libraries (tab) --> Add external JAR's -- > OK
When i do this, it will add files to Libraries, However i would like to add .jar files to lib folder.
I tried copying all .jar files from Libraries to lib, but it says
Copied .jar files directly to lib folder in directory structure, but i doesn't display in eclipse.
I would appreciate your suggestions and inputs.
Copy jars into WEB-INF/lib directory by copying files themselves (not from Libraries in eclipse view) and hit refresh from eclipse, they will appear in the Web App Libraries
P.S: It is almost always better to use a build tool like maven/ant to manage dependencies
Open the lib folder in the file browser and paste all the copied
jars.
Refresh the lib folder in the
When i create a new Maven project in Eclipse, directory structure contains both src/main/java and src/main (down below)
Question:
I understand my code should fall under src/main/java, what is the purpose of the src/main? Why does Eclipse create it?
src/main/java is Maven's standard layout for placement of your Java source codes.
Check http://java.sg/maven-standard-directory-layout/ for a list of standard Maven directories.
src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
LICENSE.txt Project's license
NOTICE.txt Notices and attributions required by libraries that the project depends on
README.txt Project's readme
For src/main/java to exist src/main/ must first exist so eclipse just shows you all the folders in your project including src/main/
If you want to remove them from your view in package explorer, you can create a filter for the package explorer view and exclude Non-Java elements.
Look for the down arrow in the top right of the package explorer view for the filters option.
You can have other sub-directories under src/main that are not source files.
If you see Maven documentation you need to have resource files under src/main/resources.
Obviously the parent directory src/main needs to be created to create child directories.