No main class detected

11,348

Solution 1

Your question is somewhat unclear. Here are my best guesses as to what is wrong:

Directory structure is wrong

Please ensure that your scala file is in "src/main/scala/ChessChallenge6.scala", relative to the directory in which you run sbt.

I am not sure if you are using underscores in your directory names, or if that is some kind of formatting that you are using only in the question text. If you are using underscores, you will need to remove them (or configure sbt to look in non-standard directories for your sources).

If you are not using them, you should remove them from the question text, as they are confusing. (If you want to distinguish files from directories in a listing, a common convention is to add "/" to the end of a directory name, e.g. "src/".)

See http://www.scala-sbt.org/0.13/docs/Directories.html

Running sbt in the wrong directory

You must run sbt in the directory above src. In the latest version of your question, that would be in the chesschallenge6 dir.

Solution 2

I had this exact problem and the issue was that I was using a class instead of an object.

For example, make sure your main class is not like this:

class ChessChallenge6 {

  def main(args: Array[String]): Unit = {
    println("hello")
  }

}

Instead, it should be:

object ChessChallenge6 {

  def main(args: Array[String]): Unit = {
    println("hello")
  }

}

Notice that the first word is object and not class.

Share:
11,348
Dominik
Author by

Dominik

Fresh and fiery in programming

Updated on June 23, 2022

Comments

  • Dominik
    Dominik almost 2 years

    The problem is that when I open "cmd.exe" and go to the directory called chesschallenge6 to enter "sbt" command and "run" afterwards it doesn't work. I get an error message saying there is no main class specified. I checked if the main class name is the same as its file name and even tried "object ChessChallenge6 extends App" but it still didn't work. The solution is simple but I just don't see it.

    └── _chesschallenge6
        ├── _project
        ├── _target
        └── _src      
            ├── _test
            └── _main
                ├── _algorithm
                ├── _model
                └── ChessChallenge6.scala
    
  • Dominik
    Dominik about 7 years
    I don't use underscores. It's my own way to differentiate folders from files in my question. Don't mind it. Just copied from somewhere mindlessly.
  • Rich
    Rich about 7 years
    That should work fine then. Please could you add the relevant bits of "ChessChallenge6.scala" to your question?
  • Dominik
    Dominik about 7 years
    Should I still add them even when moving the main class to "scala" directory is the solution? PS. I edited the directories structure in the question so it shows how it really looks like at this moment. Sorry for the fuss, it's all fine now.
  • Key Jun
    Key Jun about 4 years
    why object bro?
  • vivian
    vivian almost 4 years
    ChessChallenge6 is a singleton object: a class that has exactly one instance. It makes sense that we only want a single instance of the main class. Defining a class alone requires then creating an instance of it. Also, Object only creates a singleton and implies we only want one.