Scratch file not running on IntelliJ IDEA - intellij-idea

I am trying to run a Kotlin scratch file in IntelliJ just like you would run a Java scratch file, but it doesn't work.
I created a new Kotlin scratch file, but can't run the code even after attempting the following:
Create a main function and print something inside.
Print something with println outside of the main function.
Create a class with a main function inside and println something.
When I create a new kotlin project, the project runs fine.
The issue happens only with scratch files. I am using kotlin plugin version 1.1.51 on IntelliJ 2017.1
As the screenshot shows, the icon that allows you to run kotlin code doesn't show up on the left of the editor window:

Your observations are correct, and this is a known issue.

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.

Intelllij IDEA Ultimate 2018.1 groovy code completion does not work

I can create a Groovy project and even run it. But when I type main or print there are no auto-complete / code completion suggestions at all. I have groovy-2.4.1 in the external library folder along with Java 1.8. I find Intellij frustrating and non-intuitive to use.
In a groovy script main does show correct code completion but when I click enter it puts "main()" without the String[] args that I can see in the suggestion.
You have created a Groovy class where code has to be embedded into a class to be executable and where only class member declarations are expected (e.g. functions, fields). Select to create a Groovy script (or just delete class declaration) to be able to write executable code in top level file. See also Scripts versus classes.
I was having the same problem when I was in a rush and forgot to add a method in which to write the code. Just type 'psvm + Enter' inside the class. This will create a main method in which code completion will work. Also, you can remove the public keyword as classes and methods in Groovy are by default public.

intellij cannot create class-file when creating test class with maven project

I created a maven project, and my hierarchy is :
MyPro:
.ideas
src
-main
-java
-mypackage
-MyClass.java
-test
-java
-mypackage
- (PLACEHOLDER)
When I try to create MyClassTest.java at PLACEHOLDER i.e. in test/java/mypackage, I got unable to parse template class, cannot create class-file.
But if I create TestMyClass.java file, it works. I wonder why is that?
Do you have MyClassTest.java or something similar listed under:
Settings → Editor → File Types→ Text files ?
If so, try removing that pattern and check if you can create the class.
I was able to reproduce this error in a standard maven project, after adding the following file type:
And then trying to create the MyClassTest.java:
And after removing the file type there were no problems creating the Test class.

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".