Yuchen
Yuchen

Reputation: 33036

java.lang.RuntimeException: No main class detected

I am trying to run Scala application using docker. I've created a sample project with the following structure.

build.sbt

name := "test"
version := "1.0"
scalaVersion := "2.12.2"

project/build.properties

sbt.version = 0.13.15

src/main/scala/HelloWorld.scala

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}

Now if I do sbt run, everything works fine and I have the lovely hello-world greeting.

$ sbt run
...
[info] Set current project to test (in build file:/Users/yuchen/Documents/test/)
[info] Running HelloWorld 
Hello, world!
[success] Total time: 1 s, completed 22-May-2017 4:30:28 PM

I aded a Dockerfile:

FROM  openjdk:8

ENV SCALA_VERSION 2.12.2
ENV SBT_VERSION 0.13.15

RUN touch /usr/lib/jvm/java-8-openjdk-amd64/release

RUN \
  curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \
  echo >> /root/.bashrc && \
  echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

RUN \
  curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb && \
  apt-get update && \
  apt-get install sbt && \
  sbt sbtVersion

WORKDIR /root

CMD sbt run

They are all copied from https://hub.docker.com/r/hseeberger/scala-sbt/~/dockerfile/ and with the additional last line CMD sbt run.

And I tried running docker commands:

docker build -t test . 
docker tag test test/test:1.0
docker run test/test:1.0

However, I keep getting this error:

[info] Set current project to root (in build file:/root/)
[info] Updating {file:/root/}root...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
java.lang.RuntimeException: No main class detected.
        at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 0 s, completed May 23, 2017 4:00:19 AM

What does it mean and how do I resolve this?


FYI: I am aware of http://www.scala-sbt.org/sbt-native-packager/formats/docker.html, but just want to try writing Dockerfile as I am learning this.

Upvotes: 1

Views: 3057

Answers (1)

Andy Shinn
Andy Shinn

Reputation: 28493

I don't really know sbt. But this appears that you are not copying the project into the image (there is no COPY or ADD in the Dockerfile).

Try doing something like:

FROM  openjdk:8

ENV SCALA_VERSION 2.12.2
ENV SBT_VERSION 0.13.15

RUN touch /usr/lib/jvm/java-8-openjdk-amd64/release

RUN \
  curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \
  echo >> /root/.bashrc && \
  echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

RUN \
  curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb && \
  apt-get update && \
  apt-get install sbt && \
  sbt sbtVersion

WORKDIR /myapp

CMD sbt run

COPY . /myapp

The addition here is just a COPY to copy all the code to /myapp and to changing the working directory to this folder as well. This would copy the build context to /myapp and then the WORKDIR should run sbt run from the same folder.

Upvotes: 1

Related Questions