How to create Kotlin console application that would actually open in a console window - windows

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:
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:
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??
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.)

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

Related

Can't run more than one LibGDX game

I have developed a game with LibGDX library.
I am using socket programming libraries as a module in the project.
I am planing that every process of my LibGDX game will be a client for my server.
I structured it.
But I can't run more than one game at a time to test multiplayer functionality.
When I run the game first, everything is okay, but the second time I click run anything run until the first instance is closed.
I think that's because of Gradle.
I tried to change "Build and run using IntelliJ Idea" from Gradle settings, but this time I had an exception. It cannot read my assets file.
How can I solve this problem?
enter image description here
I have solved it with a Gradle command.
./gradlew desktop:run
Same problem here. Desktop app starts but gradle build never ends and all following builds have to wait until I exit the app or stop the gradle build process.
Setting the "allow multiple instances" option in the Application run configuration did not help.
Making a gradle configuration with "desktop:run" has the same effect and "allow parallel run" did not help.
This started after the Android Studio updating to version "Arctic Fox 2020.3.1"
Starting from command line with "./gradlew desktop:run" in several terminal windows works, but is laborious.
Update with a temporary solution (until the error is fixed):
Create a JAR-Application run configuration. Build the jar in the "before launch" section with Gradle task "desktop:dist" and point to the jar in "Path to JAR".
Hints:
You can safely ignore all the "xmas" strings in the image :)
"XstartOnFirstThread" is only necessary on Mac OS.
On some recent "intellij/android studio" versions is a bug that breaks "desktop:dist" the solution is to include "duplicatesStrategy = DuplicatesStrategy.INCLUDE" in desktop/build.gradle:
task dist(type: Jar) {
manifest {
attributes 'Main-Class': project.mainClassName
}
dependsOn configurations.runtimeClasspath
duplicatesStrategy = DuplicatesStrategy.INCLUDE
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
Enable Allow multiple instances option in Run/Debug Configuration:

Cannot debug / stop a springboot app with IntelliJ Idea community

My Idea setup cannot stop on breakpoints and not even stop the launched/debugged process.
Environment:
ubuntu 20.04.1 LTS
openjdk version "1.8.0_275" 64 bit
IntelliJ Idea 2020.2
Steps to reproduce:
Create a maven "demo" application from https://start.spring.io/ (Spring Web dependency will suffice)
Download IntelliJ Idea and launch idea.sh
Open pom.xml file to import the project in the IDE
Add a #RestController returning "Hello world" for #GetMapping("/")
Drop a couple of breakpoints in the App and in the newly created controller
Configure a maven launcher with spring-boot:run command line
Hit the debug button
The application launches and you can call http://localhost to see the "Hello world"; you can even change the code and the app nicely reloads but ... it does not stop on breakpoints.
When you hit the stop button the IDE detaches from the process but does not stop it.
What's wrong with my setup?
BTW: debugging works smoothly for a simple cli maven java application.
Thanks to the suggestion from https://stackoverflow.com/a/47064387/509565 I've worked out the solution:
Use spring-boot:run -Dspring-boot.run.fork=false as your laucher command line
Ensure that "Delegate IDE build/run actions to Maven" in "Build ...> Build Tools > Maven > Runner" settings is NOT checked

Continuous Gradle build

I'm a relatively new user with Gradle, I'm working for the first time on a project (and first time with JHipster as well).
To run my application through the terminal, I execute the following command:
gradlew.bat
But at each new modification I have to kill the application ctrl + c and run it again.
I already tried using the command gradlew.bat -t build -x test (I don't want to run test each time) and gradlew.bat --continue, but both did not work.
As the bootRun task, which is the default task gradle doesn't know when to start watching, so it is not possible with gradle only. Using your ide (e.g. intellij) and the springboot devtools it is possible.
Start the application from our ide
Make changes in the code
In intellij press ctrl + F9 to compile the code and the application should restart
When you want to do it with gradle only you need two terminals and start e.g. javaCompile in continuous mode and bootRun in another terminal. The boot devtools will take care of restarting the application when it detects the newly compiled files.
gradlew compileJava -t in on terminal
gradlew bootRun in another terminal

Can the IntelliJ IDEA run tab have color?

I am running a spring boot application inside IntelliJ IDEA and noticed a difference if I run it via the run tab (run menu) and via manual command from the terminal tab.
If I run it through a maven run target (play button), I do not see any colors inside the 'run' tab. If I run it via 'mvn spring-boot:run from the 'terminal' tab I see the pretty color highlights. The maven run configuration also runs the same command, spring-boot:run.
Application started from the play button/run configuration (run tab):
Application started from the terminal tab via mvn spring-boot:run (terminal tab):
Inside build.gradle, add the following block to get colorized log output when running your Spring Boot app inside IntelliJ IDEA via gradle bootRun.
bootRun {
jvmArgs = ["-Dspring.output.ansi.enabled=ALWAYS"]
}
It's supported for the Spring Boot Run/Debug configuration type. It explicitly passes
-Dspring.output.ansi.enabled=always
JVM option enabling the color output.
As far as I know, ANSI colors support is not available when you run it in IntelliJ IDEA using Maven or Gradle configurations in the built-in console. Feature request is welcome.
In IDEA 2017.1 EAP I'm getting colours even if I'm just running the application with the standard run command
Edit:
Might be that Community edition doesn't support Spring Boot at all (https://www.jetbrains.com/idea/features/editions_comparison_matrix.html)
You can obtain the very same output also in the Community Edition with the previously mentioned option:
-Dspring.output.ansi.enabled=always
Simply go to "Run" -> "Edit Configurations..." and add the option in the "VM options:" field for your main class.

How to debug a Gradle build.gradle file (in a debugger, with breakpoints)?

Is there a tool that will allow me to set breakpoints in a build.gradle file and step through tasks in a debugger?
Note: I believe that I'm asking a different question than similar stackoverflow questions about debugging Gradle plugins, where (presumably) the intent is to step through custom Groovy or Java plugin code located in a separate file. I want to set a breakpoint in a Gradle task in a simple build.gradle file, like...
task example {
println "I want to set a breakpoint here"
}
...so that when I run gradle example I can inspect the context in a debugger.
(For those who would point me to IntelliJ...although JetBrains' website advertises that they support debugging Gradle scripts in IDEA UI, AFAICT this is untrue, as this was reported broken in IDEA13 EAP and hasn't been fixed in IDEA14. See Debugging Gradle build files in Intellij / Android Studio )
Is there any debugging tool that allows me to set a breakpoint in a build.gradle file, or is there something about the Gradle DSL that makes it fundamentally impossible to set breakpoints in a task such as my example, above?
There is the easier way:
just add in your command line -Dorg.gradle.debug=true --no-daemon
For example:
gradle nameOfTask -Dorg.gradle.debug=true --no-daemon
Then you should start your IDE and run remote debugging with localhost port 5005, that all.
Gradle is waiting to you, because standard option server=y
org.gradle.debug
When set to true, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 to the JVM command line and will suspend the virtual machine until a debugger is attached.
Link to docs
IntelliJ 2018.2 added the ability to debug Gradle scripts in a similar fashion to how you might run/debug other projects. You can see the announcement in the release notes here.
Here is a screenshot of some of the documentation from 2018.2:
It does not yet support the kotlin-dsl (see gradle/kotlin-dsl/issues/39).
Personnaly I do this when I need to debug build scripts:
Inside you terminal do
export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Then run your build
gradle clean install
Finally put some breakpoints and launch the remote debug configuration inside your IDE on the port 5005 and you’re good to go!
export GRADLE_OPTS does not work for me.
Try this:
1 Add remote debug config
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
2 add breakpoint in gradle
3 ./gradlew your task --stacktrace -Dorg.gradle.daemon=false -Dorg.gradle.debug=true
4 Attach debug in studio
click icon
Then the breakpoint stops !!
If console is not goiong, click this in debug pannel in studio.
After reading various answers here the following steps will help you debug build.gradle to being able to break and investigate variables inside any custom task. I am using Eclipse remote debugging facilities
Place this simple code where you want to break: try {throw new RuntimeException('Break');} catch (RuntimeException e) {}
As recommended start your task with gradle mytask -Dorg.gradle.debug=true --no-daemon in the command prompt (don't have to do it in Eclipse)
In Eclipse do Run -> Add Java Exception Breakpoint, choose RuntimeException and click "OK"
Again in Eclipse go to Run -> Debug Configurations -> Remote Java Application and create new configuration that listens on localhost:5005. Name it whatever you want. Select a project that contains build.gradle you are debugging. Click Apply and Debug
At this point the execution will start but will pause at the Exception-throwing line. And you can then start looking at your variables in the `Debug -> Variables" view, inspect the stacktrace, step through the code etc.
No magic, alas, you will not see anything highlighted in build.gradle but you can pretty much guess where you are at
Obviously on subsequent runs you don't need step 3 and in 4 you can reuse previously created configuration
If you want to use this in the multiple places simply create a method, use different type of exception and feel free to enhance this idea in any way possible
For example:
void halt() {
try {
throw new RuntimeException('Break');
} catch (RuntimeException e) {
print('Paused')
}
}
task iterateDeclaredDependencies {
doLast {
Object configs = configurations.all
halt();
print(configs)
}
}
I use
set JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8989"
then 2 gradle processes are listening :
Listening for transport dt_socket at address: 8989
Listening for transport dt_socket at address: 8989
I can connect to both of them with 2 remote debug launch configurations in Eclipse although it's the same port.

Resources