Cannot create kotlin class in itellij idea - kotlin

Cannot find the "create kotlin class" option when I right click on src inside a kotlin project in intellij idea! Doing an online course where they tell to create a kotlin class by right clicking on src but I cannot find it in the latest version

You need to right click on the "kotlin" directory, not "src". If you used the InteliJIdea to create the project you should have a project directory structure like this
src -> main -> kotlin
Right click on Kotlin and you will see it.

Related

How to create a Kotlin console application with Gradle in Intellij IDEA

I found this tutorial from JetBrains: https://www.jetbrains.com/help/idea/create-your-first-kotlin-app.html?section=Gradle%20Kotlin
It is dated August 19th, 2020.
I am using the same IntelliJ IDEA version as in their documentation: 2020.2.
However, my project creation wizard looks quite different from theirs.
They provide this screenshot:
But for me, it looks like this:
and when I click on Next:
I don't see where I can choose the Console Application template, or Gradle.
I found a second tutorial - https://kotlinlang.org/docs/tutorials/jvm-get-started.html , which shows yet a third variation of the New Project wizard:
Are the tutorials out of date? Am I doing anything wrong? How do I create a Kotlin project, based on a console application template, with Gradle?
The wizard you have seems to be obsolete now. There was a brand-new one, released as a part of Kotlin 1.4 recently(see here). Most probably, the problem is caused by the Kotlin IDE plugin being outdated or something. Please try to delete in and re-install using the Preferences -> Plugins menu. Comment here with the results, please. I'd like to know if this would help.
Indeed it's quite weird, I've never seen the dialog to look like yours (mine looks like the one in the tutorials). However, choosing the template doesn't do anything special - it simply creates the main file, which you can do so yourself.
So create a new project with the "JVM/IDEA" option. If it already opens up a main.kt file, you don't need to do anything else. If it didn't, look in the src folder - you should see a folder named main with a folder named kotlin (with a blue icon instead of grey) inside - here's where you wanna create your main file (right click -> new kotlin file/class -> main.kt and make it a file, not a class). Finally, put this in the file:
fun main(args: Array<String>) {
println("Hello world!")
}
Note: if you don't have a kotlin folder, create the file in the folder with the blue icon (might even be src). Also, if this doesn't use Gradle (for some reason), create a Gradle project instead, and at the "Additional libraries and frameworks" option, uncheck Java and check Kotlin, then continue with creating main.kt if it isn't created.
You may create a Kotlin + gradle project from the terminal:
$mkdir myProject; cd myProject; gradle init
follow the tutorial.
And then start the Intellij & open the dir
You're good to go
Same process like this
The same happened to me but I figured out how to fix it:
Just disable Material Theme UI.

Creating class inside package A of A.B in IntelliJ

I have a maven project in IntelliJ IDE. I have created a package with name event.handlers. After which I created multiple classes inside this package.
There are no classes inside event everything is inside event.handlers.
Now I want to create a java class inside package event.
Is there any way from the IDE I can do that?
Currently, I am manually creating the java file inside the event folder in my code repository.
In the left Project structure pane, there is Settings button, in that please Uncheck, Compact Empty Middle Packages.
The settings will be like
.
Now you will have a tree structure, where you can right-click or (ctrl + enter/ alt + insert) on the package and create the file.
For example,
Try creating a Java Class at src folder named event.NewClass. This will create a NewClass.java inside src/event.
You can create folder with the dot symbol . at creating a Java Class or a Package, or Kotlin File/Class when Type is not File. For example create a Package with name com.company.example at src folder will automatically generate the folders recursively, src/com/company/example, create a Java Class with name com.company.example.AClass will automatically generate a file AClass.java inside the automatically generated folder tree src/com/company/example.
It might be easier to just create the class inside package AB and Refactor > Move to place it inside of A.
It seems that all other answers here are workarounds anyway, so if it's just a one-off, this might be the easiest option.
However, in my case, after doing this I had to "Invalidate Caches and Restart" before IDEA updated the UI properly.

Xtend in IntelliJ IDEA not generate file in src-gen

I combine Xtext and Xtend. In project, where I write code for my DSL, Xtext work but XTend not generate file in src-gen (this is sources root on: <project-root>/src-gen/).
I have this code in function doGenerate
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
fsa.generateFile("a.txt",'a')
}
Src-gen is always empty. In eclipse this work. How can I generate file in IntelliJ IDEA or how can I fix this problem. I run in gradle.
I run gradle task runIdea. I create Java project without any SDK.
When project is created, I add "file.mydsl" in src folder. Then I need change facet settings. I open project structure dialog (Ctrl+Shift+Alt+S), in the left-hand panel, click Facets, choose + icon and add DSL facet. Then apply and code is work. In src-gen create a file "a.txt".

Add external libraries to my project issue

I try to add external library to my current project, but my project can't pick up the library.
I have MyLib.java file in other directory.
/Users/cat/myfile/github/JavaLib/MyLib.java
I want to my current project (/home/project/HelloWorld/HellowWorld.java) to use my MyLib.java
I'm following the steps to add Library to Intellij(15CE)
Menu->Project Structure->Libraries
click (+) symbol->select Java -> select my path(/home/lib/)
Here is the screenshot
Now that you have added your JavaLib directory to the project you should be able to use the code inside JavaLib in your code now. Intellij should offer auto-complete when you start to type the name of a JavaLib class and automatically include the import for you in your code.
The disabled Apply button you circled isn't an issue. I get that in my view too. It just means nothing has changed that needs to be applied currently.

How to create subprojects inside Play with Intellij?

Currently I have the following Play project structure:
PlayApp
modules
common
sub_project_two
PlayApp is marked as a module, dependent on common.
modules is just a directory.
common is a sub project(also a play app).
sub_project_two is a sub project(also a play app), which is dependent on common.
Unfortunately I cannot just right click on "modules" and create new module(play app) and move on. Currently, I literally have to right click PlayApp and create the new module then move it to "modules", and it is running into dependency issues in Intellij and failing to import the classes inside "common".
What is the correct way of creating subprojects inside Intellij?
There is no any special way to create subprojects in Intellij. Subprojects are defined in sbt build file. Intellij will discover these projects and configure them as long as you have Scala plugin. I would imagine a following project structure in your build file:
lazy val PlayApp = Project("playApp", file(".")).aggregate(common, subProjectTwo)
lazy val common = Project("common", file("modules/common"))
lazy val subProjectTwo = Project("subProjectTwo", file("modules/sub_project_two"))
For more details visit: http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Multi-Project.html