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

13

194

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'

Pool answered 17/9, 2013 at 14:19 Comment(1)
In my case package name was invalid. int.project.mypackage. int is not allowed in package name.Inherent
Q
460

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.

Queenstown answered 17/9, 2013 at 14:21 Comment(4)
If it is your test directory, mark it as the Test Source Root (it will appear in green)Queenstown
Also, intellj-idea automatically recognize folder as source root when we have structure in such way: src/main/javaWorktable
IntelliJ makes some really questionable design decisions.Kale
Very useful 👍🏼Lapoint
J
78

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.

Jorry answered 13/7, 2016 at 19:7 Comment(7)
This... this worked. Changed the dash to an underscore and it worked immediately.Statesman
I had named a package Enum and that was my issueAgog
Thank you! I tried deleting *.iml and .idea, invalidating caches to no avail... I was getting convinced that it spotted my python files first (I have two scripts for my Dockerfile which happen to be placed first in the project) and decided to mark my whole project as a Python project...Kibosh
Just like Arun (lowercase though), I was creating the package name as "enum". Creating it as "enums" solved the problem. I wish it could show in the New submenu a warning that "enum" (or whatever) is an invalid package nameCuticula
package name cannot contain a reserved keyword. Something like com.example.package is not valid nor com.example.class.Antipater
This really helped me, I replaced dash with underscore and solved the problem.Spacious
Thanks a lot! My package was called "enum" and it works after I changed it to "type".Mothering
S
21

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

enter image description here

enter image description here

Stormproof answered 12/1, 2016 at 22:24 Comment(0)
G
7

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.

Garganey answered 17/9, 2013 at 14:22 Comment(0)
H
7

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;

Happiness answered 15/8, 2016 at 0:56 Comment(1)
Thanks! this worked. For newbies searching for Project Structure, you can find it from File menu or press Ctrl+Alt+Shift+S in windows.Blackmore
P
4

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.

A sample of mine

Pottage answered 5/5, 2021 at 7:38 Comment(2)
Same here with "enum"Cuticula
Same switch hereAdventitious
E
4

You need to mark your java directory as Source Root ,

  1. Right Click on Java directory
  2. Select Mark Directory as option and click on the sub menu option Source Root

Mark Java directory as Source root

Exasperate answered 5/11, 2022 at 15:23 Comment(0)
P
3

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.

Partee answered 9/9, 2016 at 3:13 Comment(0)
B
2

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

Barozzi answered 28/11, 2018 at 0:3 Comment(2)
It's 2024 and invalidating the cache and restarting still sometimes does the trick. 😭Coparcener
Just ran into this on 2023.3 and invalidating cache and restart was the only fix. Tried refreshing gradle, repairing IntelliJ, restarting. Nothing worked except a invalidate cache and restart.Spume
M
2

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.

Moseley answered 7/6, 2021 at 6:14 Comment(0)
C
1

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.

Carden answered 27/11, 2020 at 3:1 Comment(2)
Hi Liu Yong. As you say, this is the same reason mentioned in another answer. If you believe that answer could be improved perhaps you can either edit it, or add your reply as a comment on that answer.Pool
As a result, could I please ask you to delete this answer as IMO it is a duplicate answerPool
P
1

If you just created your project, let IntelliJ finish indexing your project.

Packing answered 26/2, 2022 at 16:10 Comment(0)
V
-1

Most of the people already gave the answer but this one is just for making someone's life easier.

TL;DR

Screenshot of how to add test sources

You must add the test folder as source.

  1. Right click on java directory under test
  2. Mark it as Tests
  3. Add src/test/java in Test Source Folders

Thats it, IntelliJ will consider them as test source.

Valladolid answered 21/1, 2021 at 8:24 Comment(5)
Does this answer respond to the question or answer something else?Pool
Manoj I'm sure your answer has some substance, but could you add some words to describe what you are trying to communicate in the picture. I cannot immediately see what you are trying to say with the picture.Pool
Hi Manoj, the edits look good but, & I could be mistaken, this is an expansion of an existing answer. You are free to edit the previously existing answer. I do like how yours has turned out but as a principle the moderators of this site try to avoid duplication of work (a key computer science goal except I guess in the cloud 😅)Pool
As the answer looks like a duplicate I'll need to flag it for closing but I do recommend adding your formatting and image edits to the leading answer as I do think they look good and are an addition. That is, even if the content is a duplicate, the effort could help and be redeemed in other locationsPool
Hi Manoj, Ive had a think overnight and I think your answer could be instead an edit to the original answer. I like the visual approach. If I get time I'l try to come back and do the edit myself to preserve your work. Thank you for your contributions.Pool

© 2022 - 2024 — McMap. All rights reserved.