create source file inside a folder within a package in eclipse - java

I have a package where I'm putting all of my code.
However, I would now like to create a folder\package inside of that package like a child package.
I was unable to make a child package in eclise by folllowing the convention ExistingPackagename.ChildPackagename. So I went to my file system, made the desired structure. When I refresh, I see that my file system has the structure I want, but on eclipse, it still shows the Parent and the child packages at the same level. It shows my packages in the same level as
Parent
Parent.Child1
Parent.Child2
Parent.Child3

In reality there is no such thing as a sub-package in Java - each package is a completely separate entity, with the names being seemingly hierarchical only for convenience. For example, items with default visibility are not visible in sub-packages, despite what one might expect.
If your problem has more to do with presentation and aesthetics than substance, then perhaps what you are looking for is the hierarchical package presentation setting in the Eclipse Package Explorer: click on the little downward triangle/arrow at the top right of the package explorer and select "Hierarchical" in the "Package presentation" submenu:

You are probably in the "Project Explorer" view. Select Window -> Show view -> Package Explorer
Also, you don't have to create child packages from the file system. Just right click the current package, select New -> Package

Related

How can we make nested packages like this in java?

So this article i am following has this guy nesting packages like this. How can I achieve the same result?
I tried to nest a package like this but I was unable to make it like the image.
As #Sanjeevan is using Eclipse, here is a way to nest packages in Eclipse;
Right click the root folder of your desired "nested" package branch,
Select New > Folder,
Type a name for the package and click finish (in this example, the name of the first folder is parent),
Right click the parent folder.
Select New > Folder,
Type a name for the package and click finish (in this example, the name of the second folder is child),
You will see a folder named parent.child. This is your nested "package". When you create a new class under this folder, either package parent.child; will be added automatically to the top of your classes, or you must add them manually.
The solution was to type the sub packages with a '.'
So if we have a main package say 'com.main' and we wanted to make sure we wanted to create a sub package for that
we should write it as 'com.main.sub'
and to make sure we have it visually follow that, you can read this.
https://stackoverflow.com/a/25378808/20406462
The layout of the packages on disk is standardized, it's not something you can change, and it's the same whether you're using javac, IDEA, Eclipse, or anything else. They are always nested. What you're looking for is how to change they way they're shown to you.
Change the Package Presentation setting in the View to Hierarchical.

Intellij - when the folder structure is different than the package name

My package name does not contain my full folder structure.
For instance - in IntelliJ I have created a test class:
and its package was initialized with package com;
How do I set which is the starting folder for the package name?
What is the difference between blue and yellow/gold folder color in the IntelliJ project window?
How do I set which isw the starting folder for the package name
File -> Project Structure. On the left, select Modules.
In the rightmost window, you will have a tab named "Sources". There you will have the opportunity to change your source/test directories. Note that a directory must exist before it is selectable this way.
Although I wouldn't depart from the setup you currently have, it's pretty standard.
What is the difference between blue and yellow/gold folder color in intelliJ project window
You will see that in the tab above ;)

Java package in package?

I'm using eclipse 3.8 indigo and I don't know why, when I'm creating a new package the destination of the new package goes outside. I want to create new packages within a package: package in package. I tried to copy / paste the newly created package or to move, but it just copies.
For example, I want to create different packages: dialogs, views, tables, etc. in my main source package. For example:
com.new.application // this is the the main package created by wizard
com.new.application.view // package that contains all views.
In reality there is no such thing as a sub-package in Java - each package is a completely separate entity, with the names being seemingly hierarchical only for convenience. For example, items with default visibility are not visible in sub-packages, despite what one might expect.
If your problem has more to do with presentation and aesthetics than substance, then perhaps what you are looking for is the hierarchical package presentation setting in the Eclipse Package Explorer: click on the little downward triangle/arrow at the top right of the package explorer and select "Hierarchical" in the "Package presentation" submenu:
This is a global setting and will affect all your opened/un-opened projects.
There is no concept of package with in package. Each package is separate namespace. I think if you go to folder view instead of package view, there you may see one under another.
The eclipse package explorer has two view options: flat or hierarchical. You are probably in the flat view (which is the default, who knows why). Change the view to hierarchical by clicking on the small triangle on the top right corner of the package view and then changing the package representation.
Firstly, you should select hierarchical representation.
Secondly, you should entitle completely package name. For example, you have 'main' package and you will create 'sub' package, you should entitle this with 'main.sub'. If you have just 1 package in package, may be you can't see hierarchicaly these packages.
After that, package view:
Second sub package creation:
Finally package view:

Eclipse Organize Packages into Folder Hierarchy

I have a bunch of packages in an Eclipse project they have names like:
edu.xxx.proj.app
edu.xxx.proj.demo
edu.xxx.proj.utils
Is there a way in Eclipse to automatically collapse them into a folder structure? I would like them to be as follows on the workbench:
edu__
|_xxx__
|_proj__
|_app
|_demo
|_utils
Click the little down pointing triangle in the package manager and go to "package presentation". From there select "hierarchal" and that should take care of it.

Java package convention

I'm developing an Android project which currently has 4 packages:
com.myapp.app.activities
com.myapp.app.db
com.myapp.app.ws
com.myapp.app.utils
Would I be able to create an additional package which is just
com.myapp.app
?
Eclipse isn't letting me create this package. It tells me a package with this name already exists.
If I start a new project and create a package called "com.testing.app" and then create a new package called "com.testing.app.activities" afterward, it works fine.
For Android developers:
What I'm wanting to do is extend the Application class and have it in a separate package. Suppose com.myapp.app can't be used, what's a good name for this new package?
Eclipse won't let you create this package because it already exists.
Packages in Java are represented in the filesystem as hierarchical folders: com.myapp.app.activities is in the com/myapp/app/activities folder. com/myapp/app already exists, so you can't create this package.
In Eclipse, juste create a new class, and in the "Package" section, precise you want to create it in the com.myapp.app package. This should work.
The package com.myapp.app already exists. You can create a class named com.myapp.app.MyClass, you'll see it right in the app package.
Another thing you can do is changing the layout of your packages from a flat layout to a hierarchical layout :
Resources :
help.eclipse.org : Project Explorer view
Eclipse by default hides empty packages. In the package explorer view, click at the small arrow in the right top: View Menu. Choose Customize View. In the Filters tab you need to uncheck Empty packages. Now empty packages will be visible in the package explorer.
It would appear that if I manually place a file into the directory and refresh the package explorer in Eclipse, the new com.myapp.app package appears

Categories

Resources