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

移除sbt中run/fork := true引发异常,保留则无法读取控制台输入

Scala sbt构建配置冲突解决方案:fork模式与控制台输入、依赖文件找不到问题

问题概述

当前项目遇到两个互斥的sbt配置问题:

  • 移除run / fork := true后,运行时触发java.io.FileNotFoundException,提示找不到/Users/ajitkumar/Downloads/flice/sensor-nws/target/bg-jobs/sbt_4be36759/target/135c9252/81ecd14d/hadoop-client-api-3.3.1.jar
  • 保留run / fork := true时,执行scala.io.StdIn.readLine()读取控制台输入会抛出NullPointerException

解决方案

1. 解决fork模式下控制台输入NPE问题

sbt默认启用fork后,子进程不会连接父进程的标准输入,导致StdIn.readLine()无法读取输入而抛出空指针。只需添加connectInput配置即可修复:

修改sbt中的运行配置部分:

run / fork := true
// 新增配置,让fork的进程连接标准输入
run / connectInput := true
Test / fork := true

2. 解决非fork模式下依赖文件找不到问题

这个问题通常是依赖解析缓存异常或依赖缺失导致的,按以下步骤排查:

  • 执行sbt clean清理项目编译产物,再执行sbt reload重新加载配置
  • 用sbt dependencyTree查看完整依赖树,确认hadoop-client-api-3.3.1是否被正确引入(spark-core通常会间接依赖它),检查你的excludes方法是否误排除了相关依赖
  • 如果依赖存在但文件缺失,手动清理本地sbt缓存:删除~/.ivy2/cache或~/.coursier/cache目录,然后执行sbt compile重新拉取依赖

调整后的完整sbt配置示例

import Dependencies._
import sbt.Keys.{libraryDependencies, resolvers}
import sbt.Resolver

ThisBuild / scalaVersion := "3.1.0"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
ThisBuild / scalacOptions ++= Seq("-language:implicitConversions", "-deprecation", "-feature")

val AkkaVersion = "2.6.17"
lazy val root = (project in file("."))
 .settings(
   name := "sensor-nws",
   libraryDependencies ++= Seq(
     "org.scalameta" %% "munit" % "0.7.26" % Test,
     excludes(("com.typesafe.akka" %% "akka-stream" % AkkaVersion).cross(CrossVersion.for3Use2_13)),
     excludes(("org.apache.spark" %% "spark-core" % "3.2.0").cross(CrossVersion.for3Use2_13)),
     excludes(("org.apache.spark" %% "spark-sql" % "3.2.0").cross(CrossVersion.for3Use2_13))),
   resolvers ++= Seq(Resolver.typesafeIvyRepo("releases"),
     "Artima Maven Repository" at "https://repo.artima.com/releases",
     "spark.jars.repositories" at "https://oss.sonatype.org/content/repositories/releases"
   )
 )

//netty-all replaces all these excludes
def excludes(m: ModuleID): ModuleID =
 m.exclude("io.netty", "netty-common").
   exclude("io.netty", "netty-handler").
   exclude("io.netty", "netty-transport").
   exclude("io.netty", "netty-buffer").
   exclude("io.netty", "netty-codec").
   exclude("io.netty", "netty-resolver").
   exclude("io.netty", "netty-transport-native-epoll").
   exclude("io.netty", "netty-transport-native-unix-common").
   exclude("javax.xml.bind", "jaxb-api").
   exclude("jakarta.xml.bind", "jaxb-api").
   exclude("javax.activation", "activation").
   exclude("jakarta.annotation", "jakarta.annotation-api").
   exclude("javax.annotation", "javax.annotation-api")

run / fork := true
run / connectInput := true
Test / fork := true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 17:05:22