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

如何为Kotlin Interactive Shell添加classpath条目?

如何为Kotlin Interactive Shell添加classpath条目?

方法一:启动时通过命令行参数指定

启动kotlinc-jvm(Kotlin Interactive Shell的启动命令)时,用-cp(或全称-classpath)参数直接指定classpath路径,多个路径按系统规则分隔:

  • Linux/macOS用冒号:分隔:
    kotlinc-jvm -cp ./my-library.jar:/path/to/compiled-classes
    
  • Windows用分号;分隔:
    kotlinc-jvm -cp .\my-library.jar;C:\path\to\compiled-classes
    

方法二:在Shell内部动态添加

进入Kotlin Interactive Shell后,用内置的:classpath命令添加条目:

  • 添加单个路径:
    :classpath add ./my-utils.jar
    
  • 添加多个路径(遵循系统分隔符规则):
    :classpath add ./lib1.jar:/path/to/lib2-classes  # Linux/macOS
    :classpath add .\lib1.jar;C:\path\to\lib2-classes  # Windows
    
  • 执行:classpath list可查看当前已加载的classpath条目,确认添加是否成功。

方法三:通过配置文件持久化设置

如果需要每次启动Shell自动加载固定classpath,可创建配置文件:

  1. 在用户主目录下创建.kotlin/kotlin-interactive-shell.conf(目录不存在则先创建)
  2. 在文件中添加classpath配置项,示例:
    # Linux/macOS
    classpath=./default-lib.jar:/path/to/common-classes
    
    # Windows
    classpath=.\default-lib.jar;C:\path\to\common-classes
    

后续启动Shell时,会自动加载配置文件中的classpath条目。

验证添加是否成功

添加完成后,在Shell中尝试导入目标类并使用:

import com.example.MyTargetClass
val instance = MyTargetClass()

若未抛出ClassNotFoundException,说明classpath已生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 11:35:22