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

使用application.yaml启动Ktor报错:未指定端口或sslPort

Ktor使用application.yaml启动失败问题排查

问题场景

  • 采用嵌入式服务器启动Ktor服务时,代码可正常监听8080端口:
fun main() {
    embeddedServer(Netty, port = 8080, host = "localhost", module = Application::main)
        .start(wait = true)
}

fun Application.main() {
    install(Koin) {
        slf4jLogger()
        modules(myModule)
    }
    configureRouting()
}
  • 改用EngineMain.main(args)启动后,期望读取src/resources/application.yaml配置:
ktor:
  deployment:
    host: "localhost"
    port: 8080
  application:
    modules:
      - com.example.ApplicationKt.main

却触发错误提示:

Neither port nor sslPort specified. Use command line options -port/-sslPort or configure connectors in application.conf

问题原因及解决方法

核心原因是Ktor默认优先加载application.conf(HOCON格式)配置文件,而非application.yaml。使用EngineMain启动时,程序会先尝试读取application.conf,若该文件存在但未配置端口,或未正确识别yaml配置,就会抛出端口未指定的错误。

解决步骤

  • 清理冲突配置文件:检查src/resources目录,若存在application.conf且无需保留,直接删除;若需保留,需在其中正确配置端口或连接器。
  • 启动时指定配置文件:通过命令行参数明确指定加载yaml文件:
    -config=application.yaml
    
  • 代码中强制指定配置路径:如果不想依赖命令行参数,可在主函数中直接指定配置文件:
    fun main(args: Array<String>) {
        EngineMain.main(args + arrayOf("-config=application.yaml"))
    }
    
  • 校验yaml格式:确认application.yaml的缩进、语法无错误,且确实放置在正确的资源目录下(如src/main/resources)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:47:12