4

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
  • 121
  • 1
  • 2
  • 10

2 Answers2

12

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.

Rich
  • 15,048
  • 2
  • 66
  • 119
  • 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. – Dominik Apr 26 '17 at 15:10
  • That should work fine then. Please could you add the relevant bits of "ChessChallenge6.scala" to your question? – Rich Apr 26 '17 at 15:39
  • 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. – Dominik Apr 26 '17 at 16:19
4

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.

cdmckay
  • 31,832
  • 25
  • 83
  • 114
  • `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. – vivian Jun 12 '20 at 01:37