Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ how do I generate a new class?

When I go to my project structure and right click -> new, I only see new file/folder etc... but no class generation or packages. How do I get IntelliJ to do this? (I have Intellij 14 Ultimate - see image below)

intellij

like image 917
Ben Flowers Avatar asked Jan 18 '15 16:01

Ben Flowers


People also ask

How do I create a new class in IntelliJ?

In the Project tool window ( Alt+1 ), right-click the node in which you want to create a new class and select New | Java Class. Alternatively, select the node, press Alt+Insert , and select Java Class.

How do you create a new class in Java?

In the Project window, right-click a Java file or folder, and select New > Java Class. Alternatively, select a Java file or folder in the Project window, or click in a Java file in the Code Editor. Then select File > New > Java Class. The item you select determines the default package for the new class or type.

How do I create a new class in IntelliJ Macbook?

To create a new class, press ⌘N on MacOS or Alt+Insert on Windows or Linux on the directory in the Project Window, and select “Java class”. When creating a new class, we can type the whole package path, separated by dots, followed by the class name, and IntelliJ IDEA will create the package and the Java file.

How do I create a test class in IntelliJ?

Create a new test class manuallyRight-click the test root folder or package in the test root folder in which you want to create a new test and select New | Java Class. Name the new class and press Enter . Press Alt+Insert and select Test Method to generate a new test method for this class.


1 Answers

IntelliJ will offer the option to create new class in the new menu, only if you right click within the part of your project structure, which is marked as Sources root or Test sources root.

In your case, right click the src/main directory in the project structure view and select Mark directory As/Sources Root. That should fix it.


Also, Gradle expects Java classes to be placed in src/main/java (and test Java classes in src/test/java) by default. I think that if you followed this convention, IntelliJ would mark those folders as (Test) Sources Root automatically. So maybe it would be better solution to adjust your project structure according to this convention.

Here is a quote from Gradle guide regarding this issue:

Gradle expects to find your production source code under src/main/java and your test source code under src/test/java

like image 184
Bohuslav Burghardt Avatar answered Oct 20 '22 18:10

Bohuslav Burghardt