How to set main class in build?

Viewed 47967

Upon sbt run I have multiple choices of main class.

I would like to set a main class so I've writen in build.sbt:

mainClass := Some("aMainClass")

But sbt fails with:

build.sbt:1: error: not found: value aMainClass

I've also tried with project/Project.scala file :

import sbt._
  class ExecutableProject(info: ProjectInfo) extends DefaultProject(info)  {
  override def mainClass = Some("aMainClass")
}

error :

 project/Project.scala:3: not found: type aMainClass

How to set the main class in a build?

3 Answers

My sbt version is sbt 1.5.4 (Ubuntu Java 11.0.11).

And I find mainClass in (Compile, run) :=Some("Hello") doesn't work in my build.sbt.

I finally find the offical example for 1.x version in here

// set the main class for packaging the main jar
// 'run' will still auto-detect and prompt
// change Compile to Test to set it for the test jar
Compile / packageBin / mainClass := Some("myproject.MyMain"),

// set the main class for the main 'run' task
// change Compile to Test to set it for 'test:run'
Compile / run / mainClass := Some("myproject.MyMain"),

you can add it to your settings. It works for me.

But I don't know how to integrate those commands into one.

Related