Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intelli-J: cannot create class-file?

This is honestly a tiny problem but it's keeping me from proceeding with my small Java practice app.

I'm currently practicing making an API call in Java. I was trying to create a simple class file called "Film". However, when I try to create it as a class-file, Intelli-J keeps telling me that it is unable to create a class-file. I'm trying to set this file up in a folder called "models". All of these class files are going to be utilized as part of an app that performs API calls.

Is there some naming convention I haven't followed? I've been Googling but haven't really found anything.

like image 993
Leia_Organa Avatar asked Jul 07 '16 20:07

Leia_Organa


People also ask

How do I create a class file in IntelliJ?

Press ⌘1 (macOS), or Alt+1 (Windows/Linux), to open the Project Window and then use ⌘N (macOS), or Alt+Insert (Windows/Linux), to generate a new file.

How do I create a .class file?

To create a new Java class or type, follow these steps: 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.

How do I add a class to a Project 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.


2 Answers

More directly, based on what @Vishal Jumani touched on, you need to tell IntelliJ what directories are 'source' or 'test' directories.

A directory structure you can use as an example, but you can use whatever you wish.

enter image description here

Right click on the directory you wish to mark as 'root' --> find 'Mark Directory As' --> Select 'Sources Root'

enter image description here

Now you should be able to add java classes in the directory without IntelliJ interrupting you!

like image 157
Chad Van De Hey Avatar answered Oct 02 '22 05:10

Chad Van De Hey


This would usually be because of the way your project is setup. It may not be setup correctly to indicate where your source code is.

  • To setup your project, in the Project Tab, click on the top most folder and select F4. This should bring up the Project Settings dialog window.
  • Now click on Modules in the LHS, and then select the Sources Tab on the RHS
  • Select your src folder and click on the Sources button, to indicate this is your source folder. Now IntelliJ is aware that this is where your code is.
  • Click on Apply and OK. This should close the Dialog Window.
  • Now right click on the src folder and then select New -> Java Class to create your Java class
like image 26
Vishal Jumani Avatar answered Oct 02 '22 05:10

Vishal Jumani