Explain the structure of spring project? - java

I am new to Spring and just created a project using Spring CLI.
When I opened the project using STS i see the following folder structure.
There are two src folders.
What is the difference between them ? src/main/java and just src.
Also suppose I want to create a new folder like webapp under main, where should I create it ?
I looked a lot online but couldn't find any answers.
Please help.

There are two src folders
No, it's just one src folder and what you see is a way of displaying a Maven project under Package Explorer view of eclipse/STS. Change the view to Navigator and you find that it's just one src folder.

If you want to create a new folder "webapp", please don't. If you use Spring, you won't need it !
How you should read this project:
src/main/java is where you write your .java files that will go in production
src/main/resources is where you will put your *.properties file (mostly) and other files, that are not .java files.
src/test/java is where you write your .java file that will run tests against your src/main/java/**.java files.
target is where your java files will be compiled. You will find your .classes file, but also the files inside src/main/resources. Those files will be inside your .jar when you decide to create one.
You think you see 2 src folder, but in reality, it is your IDE that gives you this. If you go on your file system, I am sure you will have only one src folder. It is your IDE, thanks you maven and some default configuration, which "flags" different folder in category, to be able to compile and run your tests. There is 4 categories :
sources: src/main/java
resources: src/main/resources
Test sources: src/test/java
Test resources: src/test/resources (sometimes)
If you to build a webapp using Spring, go read this: https://www.baeldung.com/spring-mvc-tutorial. It is well explained, and you will be able to run a project in a short time.

Related

is maven resources directory good place to put xml/json file which you are gonna read during your program?

I have a maven project where I run automated tests only. Under
src/test/resources
I have a folder
books
which contains 2 xml files
(src/test/resources/books/1.xml,
src/test/resources/books/2.xml).
During my test I check how many files I have in folder
'books'
int filesNumber = new File(".\\src\\test\\resources\\books").listFiles().length;
Everything works fine localy on my machine when I run my test with maven
"clean test"
The problem starts when I run my tests on TeamCity server because I get a
NullPointer
when it looks for 'books' folder.
It just can't find it. Can someone tell me why? Maybe I shouldn't put this folder to the resources directory? If yes then which directory I should use..
It is fine to put test resource in src/test/resources, but don't read them as files. Read them as resources, for instance:
// assuming MyTest class in books package
MyTest.class.getResourceAsStream("books1.xml");
Or
MyTest.class.getClassLoader().getResourceAsStream("books/books1.xml");

Eclipse, Java from 2 src. folders?

Do anyone know what is wrong with this?:
-Launched Eclipse IDE(Java).
-Created new Java Project
-I got src folder (Source folder)
-Created a new Package in the src folder, and a java class in that Package
-And, I created a new source folder called Folder2
-And in Folder2 I created a new Package and Class file in it.
I tryed to connect these 2 classes from 2 different src folders, but the 2nd class is not found.
In the 1st class I used classTwo.main(null); , but the 2nd class from the 2nd source folder was not found.
I have tried same thing and its working for me.
See be tutorial Organizing sources
make sure setting is same as below:
When creating multiple source folders, and using the classes in them, you need to make sure that the source folders themselves are added to the Java Build Path settings in the project. To access these settings,
Right Click on Project Name in Package Explorer View -> Properties -> Build Path.
There you should see both of your folders shown as source folders.
This way, eclipse should take care of building the .java class files and generating the .class files into the build folder.
Another thing, I dont get why you are creating two different source folders for Java classes. The accepted and widely followed approach is to have multiple packages (even if there are hundreds of them) inside the src/main/java source folder for Java source files. The general project structure followed is:
src/main/java - Java Source packages with .java class files
src/main/resources - resource files like .properties, .xml files which are a part of the actual application code
src/test/java - Java Source packages for .java JUNIT tests.
src/test/resources - resource files needed for the execution of tests which are not part of the actual application running code. eg. specific setting files for JUNIT tests

Create project from existing project Netbeans

I have downloaded some source code that contains src folders with some .java files. I want to try to run the code in netbeans.
This is the structure of the folder :
C4.5/src/main/java/myc45/
and in these folder include some .java files.
What should I do first?
When you create a project in Netbeans, one of the options in the project creation window is create project from existing source. If you have an existing project, you can also edit the project properties and tell it what the source folders are.
As an alternative to #PaulJAbernathy 's solution:
create a new project in Netbeans
via the projects windows, create a package myc45 (the package name used in the code you want to import) - you can do so by rightclicking, new Package
now, inside the src directory of your project directory you'll find a directory called myc45. Drop the source files into that directory using whatever file browser you commonly use. You'll see that Netbeans picks up the files almost immediately in the project explorer.
you can now use the code.
A bit messy, but there are advantages: if eg you want to transform a bunch of existing code files into a Maven type project, this is probably the easiest way.

Adding test files to a Junit test in Netbeans 6.7.1 - getResource issues, missing file in build directory

I'm adding a Junit test to one of my classes using NetBeans 6.7.1, I need to be able to load a an xml file specific to testing, so I've included it in the "Test Packages" folder (along with my actual test). I'm running into 2 issues,
(1) getResource is looking in the wrong directory
(2) my xml test file doesn't get copied when I run tests (note, this functionality works with I add files to the "Sources Packages" directory).
In my test class:
this.getClass().getResource("/")
returns:
D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample\build\classes
I need it too return:
D:\UserProfiles\myName\My Documents\NetBeansProjects\ProjectExample\build\test\classes
(As that is where the test classes are being compiled)
It seems rather hacky calling getResource, getting the parent, and then looking in test\classes. Is this the proper way of getting the path to my test file ? maybe this is a bug in netbeans ?
Also, when I right click on my testFile and "run tests" , only my test class files get copied to the test/classes directory and not my xml test file. How do I tell Netbeans to make sure to copy a regular xml file along with class files to the build directory.
I would also like to avoid hacking the ant build to copy my test files.
I recently ran into this problem using NetBeans 6.9.1. Here's how I solved it.
Open Project->Properties->Libraries
Select the Run Tests Tab
Click on Add Jar/Folder
Navigate to where you've stored the resource files
Add the folder
Now running tests using those resources will work. I tested this with NetBeans 6.9.1 and a simple Java Application.
NetBeans creates resource files in the src directory by default (default package), so I added the src folder in step 5 above. Once you do that tests looking for a resource file in the classpath will find it.
Since NetBeans packages the resources found in src folder by default, you don't need to copy the files around and keep them in sync.
If you want test resources different from production resources, you can add the test resources in the default package under the test folder. Then instead of adding the src folder in step 4/5 above, add the test folder.
Just put your file in the same package as your test, say, data.xml in package foo.bar. No copying or build script hacking is necessary, just refer to the file like this:
getClass().getResource("data.xml");
Alternatively, you can do this:
getClass().getResource("/foo/bar/data.xml");

Recommended Source Control Directory Structure?

I am going to be using Subversion for source control on a new J2EE web application. What directory structure will you recommend for organizing code, tests and documentation?
I usually have
Project Directory
src - actual source
doc - documentation
lib - libraries referenced from source
dep - installation files for dependencies that don't fit in lib
db - database installation script
In work with Visual Studio, I'm not sure if this works the same in the java world. But i usually put stuff in different project folders in src. For each source project there's a separate test project. Build files go in the main project directory. I usually put a README there too documenting how to setup the project if it needs more than just checking out.
EDIT: This is the structure for a single working checkout of the project. It will be duplicated for each branch/tag in your revision control system (remember, in most SVN system, copies are cheap). The above example under Subversion would look like:
/project
/trunk
/src
/doc
/...
/branches
/feature1
/src
/doc
/...
/feature2
/src
/doc
/...
I found some old questions here on SO that might be interesting for you:
Whats a good standard code layout for a php application
Contains a link to an article on Scalable and Flexible Directory Structure for Web Applications (focus on PHP, though)
How to structure a java application, in other words: where do I put my classes?
Structure of Projects in Version Control
To expand on what Mendelt Siebenga suggested, I would also add a web directory (for JSP files, WEB-INF, web.xml, etc).
Tests should go in a folder named test that is a sibling of the main src folder - this way your unit test classes can have the same package name as the source code being tested (to ease with situations where you want to test protected methods or classes, for example... see the JUnit FAQ for this, and this question also on Where should I put my test files?).
I haven't had much use for it myself, but a Maven project will also create a resources folder alongside the src folder for non-source code that you want to package/deploy along with the main source code - things such as properties files, resources bundles, etc. Your mileage may vary on this one.
I use Eclipse for creating J2EE web applications and this will create the following project structure:
WebAppName\
\lib
\src
\tests
etc...
I would then create an SVN folder on our trunk called WebAppNameProject. Within this folder I would create folders called WebAppNameSource, Documentation etc. Within the WebAppNameSource folder I would place the project source generated by Eclipse. Thus I would have the following folder structure in SVN:
\svn\trunk\WebAppNameProject
\WebAppNameSource
\lib
\src
\tests
etc...
\Documentation
Hope this helps.

Categories