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

JavaIntellij IdeaProject

Java Problem Overview


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'

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

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;

Solution 6 - Java

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.

Solution 7 - Java

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

Solution 8 - Java

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.

Solution 9 - Java

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

Solution 10 - Java

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.

Solution 11 - Java

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

Solution 12 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCrowieView Question on Stackoverflow
Solution 1 - JavaArnaud DenoyelleView Answer on Stackoverflow
Solution 2 - JavakuporificView Answer on Stackoverflow
Solution 3 - Javauser3218923View Answer on Stackoverflow
Solution 4 - JavaBrian AgnewView Answer on Stackoverflow
Solution 5 - JavaXian ShuView Answer on Stackoverflow
Solution 6 - JavaShatyUTView Answer on Stackoverflow
Solution 7 - JavaManish KumarView Answer on Stackoverflow
Solution 8 - JavaalphcoderView Answer on Stackoverflow
Solution 9 - JavaCoder RoadieView Answer on Stackoverflow
Solution 10 - JavaliuyongView Answer on Stackoverflow
Solution 11 - JavavvaubanView Answer on Stackoverflow
Solution 12 - JavaManoj Reddy MettuView Answer on Stackoverflow