Create non-flat Java packages in Intellij - java

I want to create file foder like this:
But in Intelij it always show this:
How can I solve it, pls?

Just unselect option in Project -> (Gear icon ⚙) -> Compact Empty Middle Packages.
Then select a folder and create next package node.
See on image how to turn off flat mode

It flattens empty packages. You could either disable the setting as the other answer, or simply put files into those packages.
I think the real problem, though, it that you are actually making a file structure like this
core
item
lyKhi
nguHanh
You should right click on "core" when you create a 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.

How to solve java file mapped to not java file type text

I wanted to add a java class to a project, but I did a mistake and added it as a text file. Then, I removed it and tried to add it again as a java class, but unfortunately it doesn't work anymore. And I get the following error:
Unable to parse template "Class" Error message: ABC.java mapped to not java file type text
As I searched, I faced some solutions like
this one but I can't find File Type under Editor or anything similar that I can change as suggested in the solution.
Any help would be appreciated.
PS: I am using IntelliJ 2018.3.4.
Thanks in advance,
Go on File->Settings and a dialog will open. In that dialog navigate on the left part to Editor/File Types and then you need to choose from the section Recognized File Types Java Class and remove from bottom section the problematic extension (note you have a small minus button on the right)
https://www.jetbrains.com/help/idea/creating-and-registering-file-types.html
This is a variant of Markov's answer that worked in my scenario.
Try going to File->Settings->Editor->File Types and to the Recognized File Types tab. Click the "Auto-detect file type by content" choice, and delete the name of the file you are trying to create from the "File Name Patterns" section by using the minus button.
Select required file type in top list (Recognized File Types) -- do it for "Text files"
In bottom list (Registered Patterns) select undesired pattern -- "SomeFileName.java"
Click on "-" (Minus) button to remove pattern from the list
Repeat 1-2-3 for other undesired patterns
If desired, you can also do this manually by editing config file while IDE is closed. Because it's IDE-wide setting, it is not stored in .idea folder (which has project settinsg only) -- the file to look for is called filetypes.xml and shoudl be located in ~/Library/Preferences/IntelliJIdeaXX/options ( see http://devnet.jetbrains.net/docs/DOC-181 for other OS etc )
Certainly inelegant, but you can simply copy the contents of the file to the clipboard (ctrl-c or similar), delete the file (maybe make a temporary backup somewhere outside the project), then in IntelliJ go to the desired package, right click, select new, select Java Class, name it correctly, and then you can paste in the contents of your file (ctrl-v or similar).

IntelliJ does not show 'Class' when we right click and select 'New'

We're creating a new project in IntelliJ and must have something wrong because when we right click on a directory, select New and then get the context menu, Java based options are not shown. Currently get things like File, some HTML options, XML options.
We have assumed so far it's something we haven't configured correctly in the Project/Module configuration. The new module we are talking about is part of a multi module project. We created it using a Maven web archetype.
Any help configuring the project nature?
Edit: The answer is basic: 'That moment when you realise somethings not working because you haven't been clicking 'Apply'... :) We had a good laugh at ourselves when we discovered this'
The directory or one of the parent directories must be marked as Source Root (In this case, it appears in blue).
If this is not the case, right click your root source directory -> Mark As -> Source Root.
This can also happen if your package name is invalid.
For example, if your "package" is com.my-company (which is not a valid Java package name due to the dash), IntelliJ will prevent you from creating a Java Class in that package.
you need to mark your directory as source root (right click on the parent directory)
and then compile the plugin (it is important )
as result you will be able to add classes and more
If you open your module settings (F4) you can nominate which paths contain 'source'. Intellij will then mark these directories in blue and allow you to add classes etc.
In a similar fashion you can highlight test directories for unit tests.
Project Structure->Modules->{Your Module}->Sources->{Click the folder named java in src/main}->click the blue button which img is a blue folder,then you should see the right box contains new item(Source Folders).All be done;
I will share another interesting point. If you try to create a package with the reserved keyword then it will be treated as a normal directory and not a package. I was having this issue where I was creating a package named import and it was converting that to a directory.
Another possible solution is that the project name is not acceptable. For example, creating a project with spaces in the name does not block the project creation but the proper sources are not marked and when those are marked manually, I still was unable to create classes. Recreating the project with hyphens (-) instead of spaces corrected the problem for me.
Make sure you are not creating a package name which is same as predefined keywords in java like enum, int, long etc.
In my case I was trying to create a class under "enum" package. As soon as I changed package name to "enums" I was able to create class in it.
Had this issue too. Invalidating Caches/Restart did the trick for me. Please upvote so the the IntelliJ folks take this more seriously. This gives the IDE a terrible UI/UX experience.
https://youtrack.jetbrains.com/issue/IDEA-203100
There is another case where 'Java Class' don't show, maybe some reserved words exist in the package name, for example:
com.liuyong.package.case
com.liuyong.import.package
It's the same reason as #kuporific 's answer: the package name is invalid.
If you just created your project, let IntelliJ finish indexing your project.
You need to mark your java directory as Source Root ,
Right Click on Java directory
Select Mark Directory as option and click on the sub menu option Source Root
Most of the people already gave the answer but this one is just for making someone's life easier.
TL;DR
You must add the test folder as source.
Right click on java directory under test
Mark it as Tests
Add src/test/java in Test Source Folders
Thats it, IntelliJ will consider them as test source.

How to group a folder structure in a common navigator view in an eclipse-rcp application?

I have a question concerning the folder structure of a common navigator view (package explorer) in an eclipse-rcp application.
My goal is to group the source folder of a project.
As you can see in the picture above the source folder of the project “Sample” is src + main + resources. I would like to group that source folder so that there is only one line displaying the text “src.main.resources”. This should not be reached by changing the folder structure in the file system.
So my question is: Is it possible to group the source folder programmatically without touching the physical folder structure?
Any help would be appreciated!
Cheers!
After doing a lot of research, I'm trying a different approach. I am using a decorator class to modify the label of the source folder.
To reach the goal of the original question, I would like to hide the first two levels of the source folder so that there is only one line displaying "src/main/resources". Sadly, IFolder.setHidden(true) does not work because it also would hide the children of the node.
Is there a possibility to hide these two folders by using a filter for example? Or do I have to modify the Tree of the Navigator?
Thank you in advance!
There are a number of resources and articles you can look into:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fcnf_config.htm
http://scribbledideas.blogspot.dk/2006/05/building-common-navigator-based-viewer.html
http://scribbledideas.blogspot.dk/2006/05/building-common-navigator-based-viewer_22.html
http://scribbledideas.blogspot.dk/2006/06/building-common-navigator-based-viewer.html
http://scribbledideas.blogspot.dk/2006/06/building-common-navigator-based-viewer_18.html
http://scribbledideas.blogspot.dk/2006/06/building-common-navigator-_115067357450703178.html
With this information, you should be able to do everything with CNF :-)

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.

Categories

Resources