How to organize source files of Java program? - java

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

Related

are libraries and natives part of resources?

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)

Maven Spring project structure in Eclipse

I have a Maven project in Eclipse which I am using to build a Spring MVC application. However, I am very confused about how the project structure should be. I've looked at various sources online and they often differ.
My current project structure (a bit of a mess..):
Which source folders do I need in Java Resources? Obviously src/main/java and src/test/java but what about src/main/webapp? What goes in there?
I see that there is a src directory generated when I build the project. What's the purpose of this directory?
Do I put my static resources e.g. 'style.css' in WebContent/resources or in a different directory?
Finally, how should my Deployment Assembly mappings look?
Update as per suggestion (not yet solved):
src/main/webapp contains the css html and js related files. if you are aware of webContent folder the same things resides inside the webapp folder.You will need to remove the webcontent folder and place all the files in src/main/webapp folder.

In concordion, How to link the Java and the HTML?

I've done the tutorial of concordion and I'm looking to put the specification file and the Junit test in separate folder but I'm getting the an IOException, cannot find the file.
I've seen that it is possible in the source code of concordion it self : the two files are in separate folder here and here.
I have no idea how it have been done.
Thanks
you need to make sure that the folders containing the specification file and the compiled JUnit class are on the classpath. Additionally, the specification file and JUnit class most be in the same package.
In the example that you have included from Concordion, both the Exception.html and ExceptionTest.java files are in the spec.concordion.results.exception package. The specification file is under the src/test/resources folder, and the JUnit class is under the src/test/java folder.
If using Eclipse, the folders need to be configured as source folders on the build path (in the above case, the src/test/java and src/test/resources folder would be source folders).
If using Maven or Gradle as a build tool, the src/test/java and src/test/resources folder are used by convention and no additional configuration is needed. Concordion itself uses Gradle as the build tool.

Maven sources vs resources

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

Java Project vs Maven Project (Eclipse), dir structure clarification needed

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.

Categories