You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ IDEA中Kotlin脚本main函数无输出、反射访问警告及文件扩展名问题咨询

Hey there! Let’s break down your Kotlin issues one by one to get you back on track with the Google bootcamp:

1. Why Your .kts Script Has No Output

The key issue here is understanding how .kts files work differently from standard .kt files. Your code defines a main function, but Kotlin script files (.kts) don’t automatically execute a main function like compiled .kt files do. Scripts run code top-to-bottom, and since you only declared the function without calling it, nothing gets executed.

Fix this in one of two simple ways:

  • Option 1: Remove the main function entirely (the simplest approach for scripts):
    println("Hello, world!")
    
  • Option 2: Explicitly call the main function at the end of your script:
    fun main(args: Array<String>) { 
        println("Hello, world!") 
    }
    // Trigger execution by calling the function
    main(arrayOf())
    

2. Fixing the "Illegal reflective access" Warning

This warning comes from JDK 9+'s module system, which restricts certain reflective operations that IntelliJ and Kotlin tools still use. The Gradle property you tried affects the Gradle daemon, but Kotlin scripts run directly via IntelliJ’s script runner—so that config doesn’t apply here. Try these steps instead:

  • Adjust IntelliJ’s Kotlin compiler settings:
    Go to Settings → Build, Execution, Deployment → Compiler → Kotlin Compiler, then add --illegal-access=permit to the Additional command line parameters field.
  • Update your script’s run configuration:
    Open the run configuration dropdown → Edit Configurations, then add --illegal-access=permit to the VM options field for your .kts script.
  • Try switching to JDK 11 (LTS):
    JDK 15 is a non-LTS version, and many tools have better compatibility with long-term support releases like JDK 11. This might eliminate the warning entirely without extra configuration.

3. .kts vs .kt: What’s the Difference?

Let’s clear up the file extension confusion:

  • .kt: Standard Kotlin compiled files. These need to be compiled to bytecode before running, and require a main function (or project-specific entry points like Android’s Activity) to start execution. They’re used for full-scale projects like Android apps or backend services.
  • .kts: Kotlin script files. These are interpreted (or just-in-time compiled) and run code top-to-bottom without needing a main function. They’re ideal for quick tests, automation scripts, or prototyping—think of them like Python’s .py scripts, but for Kotlin.

内容的提问来源于stack exchange,提问作者Micro1225

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:22:40