0

I'm trying to create a Kotlin console app using IntelliJ Idea.

For some reason it doesn't run as a console app.

I'm using IntelliJ IDEA 2020.3.1 (Community Edition) on Windows 10.

What I do is as follows - I create a new Kotlin project out of the "Console Application" template. I choose openjdk-15 as the Project JDK: New Project It generates a project consisting of a simple "Hello world" function:

fun main(args: Array<String>) { println("Hello World!") } 

When run it, it simply terminates (as expected) - without opening any console window though: console app completes immediately

If I add some code that would require interaction from the user:

fun main(args: Array<String>) { val name = readLine() println("Hello $name!") } 

Once try to execute it, it builds but then hangs, never completing. Apparently it waits for the user input that's never submitted.

This is understandable, but underlines the problem - so where's the console??

console-app-hangs-never-completing

  • I tried to fiddle with "Edit Configurations"
  • Do I possibly need a different JDK?

What is the solution if I simply want to build a regular console application running in text mode? (Like one I could trivially create using Visual Studio.)

2
  • 1
    You run it with gradle so you don't actually have a console. Run it with IntelliJ (select in Preferences, I think under Gradle). Then it should run inside the IntelliJ console. If you wan't to ship the app you're gonna have to build it and then run the .jar file in a console. Commented Jan 11, 2021 at 20:10
  • Ah, indeed! It's in Settings under Build, Execution, Deployment / Build Tools / Gradle / Build and run using:. Thanks - I've had no experience building console apps, and it wasn't really obvious to me. You can convert your comment to an answer so that I can accept it. Commented Jan 11, 2021 at 20:34

1 Answer 1

1

You ran it with Gradle so you don't actually get an interactive console (just the text output of Gradle).

Go to settings and under Build, Execution, Deployment -> Build Tools -> Gradle set Build and run using: to IntelliJ.

When you run your program IntelliJ (and not Gradle) complies and runs it and you can actually see an interactive console window (inside IntelliJ).

If you wan't to build/ship the app you'll have to build it with Gradle (I recommend the Shadow Plugin to make a single jar file out of it) and run the .jar file inside a console (java -jar myApp.jar).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.