I have project, with source folder "app", inside this package I have package "models". Can I create folder or any other kind of subdirectory within this package? So that eventually I would have something like
-app
-models
-Folder1
-file1
-file2
-Folder2
-file3
-file4
When I try to force creating folder inside (by clicking new->other->folder), I cannot add anything to it.
Creating a folder within a package simply creates a new package
e.g. The folder structure
- app
- models
equates to package app.models
Adding a new folder, Folder1 to this structure
e.g.
-app
-models
-Folder1
equates to the package app.models.Folder1
a folder in a package is just another package, so you want new->package then type in app.models.folder1
Packages and folders are slightly different conceptually.
You generally group your classes that deal with the same functionality in the same package. For example the core classes, the model of your app can be in com.example.myapp.core and the ui classes in com.example.myapp.ui .These packages are represented on the disk by a folder structure.
In my opinion, you should not change this package structure to add files that are not Java classes. I would suggest to add a resources folder at the top of your app tree so that your data and your classes are separated.
But if you want to just add subpackage, do not create a new folder, just create a new package such as app.models.Folder1.file1 and you will get the structure you want.
You can refer to this question to know more about the packaging conventions: Are there best practices for (Java) package organisation?
The default package representation is Flat. You want to change it to Hierarchical.
Related
I was watching a lecture on Java when I caught myself not specifying package in Java file.
They lecturer was using Java 11 on Netbeans, and I Java 1.8 on IDEA. By default Netbeans created him a package, and the main java file has package specified.
But on IDEA, I noticed that I don't need to specify package name, when creating .java file inside src\main\java folder. And I have a question why?
Why is src\main\java a folder, and not a package?
What is the difference between a folder and a package?
In Python, they create __init__.py file inside of a package, in order to make Python interpreter see the package. How is it working in Java, if there is no __init__ file? How does Java understand that this is a folder, and that is a package?
Why does Java need to introduce and separate folder and package terms?
Why is src\main\java a folder, and not a package?
Which folders become packages depends on where the "root" is defined. In IDEA, src/java/main is the root. Any folders inside of that are mapped to packages. If for example, you create a folder src/java/main/foo and create a Foo.java in that folder, you will have a class named Foo in a package named foo.
What is the difference between a folder and a package?
A package is a way to group related Java classes. Sometimes a package is implemented as a folder in your file system. Sometimes it is something else. (See below.)
Why does Java need to introduce and separate folder and package terms?
One reason to differentiate between folders and packages is that some packages aren't folders. For example, you can download a .jar file from the Internet which contains a Java library. The library inside that .jar file defines packages and classes that you can use in your own code, but there are no folders for the packages.
The reason for a new term "package" is to create an abstraction that isn't tied to the folders in your local file system.
The difference between a package and a folder is not directly evident unless you define your classpath yourself.
All directories and all zip files you add to the classpath create a baseline. Such as the .../src/main/java folder. Underneath that baseline you can still create folders with classes. But for Java these will be treated as packages.
So in the filesystem packages are resembled by folders. In zip files (and jar files are nothing but zip files with some metadata) there are no filesystem folders but still something folder-like that can be extracted into filesystem folders.
And it is thinkable that people would write even other Classloaders that do not just do simple filesystem or zipfile access. They can do virtually anything which could even range to loading classes from different websites - just based on their package names.
As soon as a class is not in the default package (in a folder on the classpath directly) it needs the package name declared in it's source code. And that is what you spotted. Well done!
Summary: .../src/main/javais the default package (that does not need to be specified) because that folder is added to the classpath when the JVM executes. Any classes in subfolders need their package defined.
A small detail: The classpath is set for the compiled classes, not the java source files. You could keep all java sources in the same directory and just declare by package into which 'folder' the compiled output should go. But it is good practice to organize the source files in the same structure that will be emitted by the compiler. Any IDE will help you maintain that structure.
I have some .java files that I'd like to include in my build path to utilize some of the classes and such inside them. I've added them to the /lib folder but it doesn't allow me to call data types and methods and such from the .java files in my app project (in the .MainActivity). How can I accomplish this?
The simplest thing is just to add the Java source code to your existing project, alongside your existing Java code.
In your case, the Java files do not have a package declaration. That's... not good. But, you can add one, then put the files in the appropriate directory for that package in your project. For example, you could use the package used by some of your existing classes, then put the files in the same directory as those classes.
If the Java files already had a package, you could create a directory tree off of your java/ directory based on that package, then put the Java files in that directory.
I have been looking for a way to make a "package folder" in visual studio express 2013, the way I might do it in java is a "package" I know that I can make whole new projects called "Visual Studio Package Projects" via a wizards but all I really want is a ~container~ that puts a dot in the class name! A folder by any other name!
THIS IS WHAT I AM LOOKING FOR
THIS IS WHAT I AM PRESENTED WITH
If you create class inside folder, the namespace (which is almost the same as packages in Java) for this class is based on the folder(s) it is inside.
Actually, packages in java are folders too.
Just add a new folder - then by default, new classes will be in that namespace. So for example, if you have a project called Foo, and you add a folder called Bar, then you'll end up with:
namespace Foo.Bar
{
}
at the top of classes in that folder. Namespaces are the closest C# has to Java packages. They're not quite the same, as packages in Java also affect access control - but they're close.
A folder can be created in a project root. And any number of nested folder can be created to categories common files. And intended class can be placed inside that folder. This way you can get ~container~ that puts . in namespace used in class.
I was looking through a sample project project provided by the android developer website and I see that a file Beam.java has been declared in a package as package com.example.android.beam; with the folder structure "..\src\com\example\android\beam\Beam.java". There are two other files, BuildConfig.java and R.java in a similar directory structure, but in a completely different directory "..\gen\com\example\android\beam\Beam.java" but with the same package declaration of package com.example.android.beam;. Are all these files in the same package even though they are in different folders? Did the IDE put the latter two files in a different folder only to make it clear that they are autogenerated, or is there a another reason as well?
The folder called "gen" is an automatically generated folder which contains files that are also automatically generated. In other words, don't worry about the "gen" folder, your packages and files and whatever you make will be in the "src" folder.
For example, the Android IDE generates the file "R.java" which contains the element id's used in your code, ie. "R.id.some_element", "R.string.some_string".
can files in different folders be in the same package?
Yes, as long as they all are compiled and packaged to be in the same directory in the .dex file.
They are autogenerated so as you said, they are put in /gen/... . What YOU write must be put in /src/...
There are a couple of questions on SO that sort of hit this, but I am totally new to Java development and I don't know the correct way to approach this.
I have a C# solution, containing two projects (my app, and a unit test project) and within the app, most things are put into folders eg. Interfaces, Exceptions etc.
I am trying to recreate this in Java / Eclipse, but I don't know how. I ended up with lots of packages, which sounds really bad. I also tried adding a source folder but that ended up being outside of the package.
Could anyone point me in the right direction?
Namely, which of those should I use to represent my unit test project/set of unit tests, and subfolders which exist just for organising stuff.
Edit: It also says use of the default package is not advised. What should I be doing?
Edit 2: Here is what it looks like. Does this look vaguely correct? My original C# solution is on the right.
In a typical java eclipse project, you will have one or more source folders (for example one for app code, one for your unit tests).
Each folder contains a package tree, typically starting with your base package, for example com.mycompany.myapp.
In order to avoid name collisions, packages names are usually start with the domain name of the entity who is the author of the code, starting with the top-level-domain and going backwards (more general to more specific). That way, each class fully qualified name is unique. For example if google creates a class named List, it will be known as com.google.List, and it will not enter in conflict with the existing java.util.List interface.
You can have a unlimited number of packages inside this base package, for example :
com.mycompany.myapp.persistence
com.mycompany.myapp.domain
com.mycompany.myapp.services
com.mycompany.myapp.web
It all depends on your project and the way you want to organize your code and your classes.
At the logical level, packages are named with dots as separator. They contain java classes.
At the physical on disk level, each package is a directory. The java classes are contained in .java files (most frequently one class per file).
In Eclipse a "source folder" is a folder inside your project that is known to Eclipse to contain java source files. It will be compiled included in the output (for example JAR file) when you build your project.
In Eclipse, you usually view them at the logical level, showing packages. When you tell Eclipse to "create a new package", it will create the directory for you. For example, if you tell it to create the com.mycompany.myproject package, it will automatically create a com folder containing a mycompany folder containing a myproject folder.
In java source tree structure must match package structure
so foo.bar package must be laid out in
src/foo/bar
Also default package may not be advised - but you can still use it - better to put things in a package though
In java different project development structure are flowed according to type of project.
So as you are new to java and Eclipse so it's better to install maven plugin and create maven project and choose a archetypes according to your project type like a standalone or web based.
The maven plugin will create the project structure including packages,test packages source folder etc. You can get more about project structure from this
Using the default package may create namespace collisions. Imagine you're creating a library which contains a MyClass class. Someone uses your library in his project and also has a MyClass class in his default package. What should the compiler do? Package in Java is actually a namespace which fully identifies your project. So it's important to not use the default package in the real world projects.