1

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

In the above accepted answer, it says the "New" does not give the option for Java Class because it was not the source root (blue directory).
However, as you can see, even though where I am right-clicking is source root, indicated by blue box, it is not giving me the option for Java Class

Any idea why?

This directory is imported from git so it did not follow the usual "Create Project" for java directly from Intellij

enter image description here

ealeon
  • 12,074
  • 24
  • 92
  • 173

6 Answers6

3

You are right clicking in the wrong spot -- youre "too high" up the tree. Go to where you code is (as in the package under "src") and try it there and you will see the option for a New Class.

EDIT: You don't need to be in the source root. You need to be in the source directories themselves. The blue dot I believe is a git status indicator. What you want is an entirely blue folder.

Go down under "src". Likely under "src" then "main" then "java", and you're there (that's blue for me) and then you will see your package folders. Below those are where you likely want to be adding classes.

JoeHz
  • 2,136
  • 1
  • 18
  • 29
2

As many mentioned above you can right click on the folder you would like it in. In your case:

If you push Alt+1, it might be easier for you to right click

src > New Java Class

Alternatively you can push:

  1. Alt+1 (Project View)
  2. Alt+Ins

or

  1. Ctrl+Alt+Ins
CooperH
  • 66
  • 7
1

1) Right click on src

2) Click New > package

3) Name your package and press OK

4) Right click the newly made package

5) New > Java Class

Rotav
  • 75
  • 5
0

If the folder where we are trying to create the class is not marked as source or test then the option is not available.

Right click the project and go to project structure then under project settings -> Modules, Select the folder where you wanted to create class and mark as Source or Test appropriately and then click apply and ok.

Now you will see the change of color of the folder/package(Sources->Blue, Test->Green) and you will get the option to create the class.

This worked for me.

0

I know this is an old thread, but it happened to me because my gradle files were not synchronized. So if you are within a team working on same project and face this issue, make sure to run gradle sync.

SidGabriel
  • 205
  • 1
  • 3
  • 12
-1

You should go inside java project then you can create java class.

Click here for example

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Dips
  • 1
  • 1