Eclipse, Java from 2 src. folders? - java

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

Related

Eclipse - Using a .class file in Java

As part of a project I was given three files, a partially written .java file where my work will go and two separate .class files. I created a new Java Project and added the .java file and started adding/editing the starter code.
How/where should I put the .class files?
I tried adding the class files to the bin folder and dragging/dropping them into the package in Eclipse.
You should go to the project's Java Build Path property page and add a Class Folder for those two files. The contents of bin will be erased and overwritten without notice.

Explain the structure of spring project?

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.

How do I say to Eclipse where to save my compiled packages to another path?

So I have this Java Eclipse project where I use a custom package:
Project/
src/
defaultpackage/
*.jar
mypackage/
class1.java,
class2.java ...
How do I say to Eclipse where to compile all mypackage classes as mypackage.jar in a different output folder? I know that by default Eclipse will create the jar's inside the project directory.
My idea is to have a folder for my compiled packages out of the project structure, actually create myjarlib folder, so everytime I compile a different package will create a / ... / myjarlib / newpackage.jar only if I specified this for the newpackage.
In your structure example, the 'src' folder is a source folder. You can configure where eclipse emits the compile-on-save generated class files on a per-source-folder basis.
You cannot configure this on a per-package basis.
So, change it at the source folder level. If you want one package to go in directory A, and the other package to go to directory B, make 2 source folders, and configure for each. Example:
Project
src
main
pkg1
SomeJavaCode.java
test
pkg2
SomeTestCode.java
Here Project/src/main is a source package (containing the source for class pkg1.SomeJavaCode), as is Project/src/test. right click on the project and select 'properties' to modify which directory(ies) are source folders. Also in the 'properties' of any project (not the source folder), go to section 'Java Build Path', pick the "Source" tab, click on the sourcefolder you want to edit, mark the 'allow output folders for source folders' option (by default, eclipse sends the compile-on-save results of all source folders to the same folder, this checkbox lets you configure it on a per-folder basis), and configure it.

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.

why xml files in eclipse project's source directory is copied to target/classes directory?

I have xml files in eclipse project's source directory, like:
src/java/main/com/xx/zz.xml
1.When using eclise to build automatically, xml files are copied to target/classes.
2.When using 'mvn complie', xml files are not copied to target/classes.
For the second case, I found this:why xml files in eclipse project's source directory is not copied to target/classes directory?.
but for the first case, I cannot find any document.
Can someone explain it for me ?
Thanks!
Eclipse works quite a bit differently than standalone Maven. Maven uses javac from JDK. By default javac only processes .java files and generates .class files in the same directory as .java sources. Maven asks it to generate classes in a separate directory and javac only moves .class files there.
The reason for this is that javac gives you more freedom in organizing your source files than most developers use. For instance javac does not care if your class is located in a folder structure that mimics declared packages. You can put a module together by putting several .java files along with some other files like .properties or .xml in the same folder. Your .java files can have different package declarations. For instance you can have files A.java:
package aaa.bbb;
class A {}
and B.java:
package zzz.uuu;
class B {}
If you ask javac to put classes in a target directory, it will create necessary subfolders for .class files, that will resemble the package structure. However it cannot do so for properties and xml files, because they do not contain package declarations. Such 'resource' management is left for the build tool.
Maven expects that you put such resources in another source folder (resources). They will be copied to generated package structure, so you should match it in resource folder. This is why it does not copy non-java files in source folders.
Eclipse expects you to put even .java files in a target package structure and complains if your package declaration does not reflect relative path of the file. This is where you have less freedom compared to bare javac. Thanks to this rule it can copy resources along with .class files and does not need another 'resource' folder.
You can configure Eclipse to use source folder as output folder. Eclipse will not touch resources in this case.
If you right click on the project in eclipse and select 'properties', then Java Build Path you see an input at the bottom for the Default Build Path, which should be target/classes. The source folders are shown in the main dialogue. If you click on the source folders then you can modify each, to exclude the xml files (if that is what you want to do).
Maven will include your xml files automatically if you put them in src/main/resources.
If you don't want to have xml files in build directory, you need to configure eclipse excluded source file types -
right-click on the file in the Project Explorer, choose Resource Configurations > Exclude from Build and choose the configurations that you want.

Categories