Search code examples
eclipsescalacmdcompilationexecution

Cannot run scala files from command prompt


I could not run a Scala project from command prompt. It was able to get it to work when I wrote the program in Scala Worksheet, but I want to get it to run from Eclipse using CMD. I did:

C:\WINDOWS\System32>scalac Hello_WORLD.scala
error: source file 'Hello_WORLD.scala' could not be found
one error found

Then, I tried to skip the compiling and go straight to the execution:

C:\WINDOWS\system32>scala Hello_WORLD
No such file or class on classpath: Hello_WORLD    

Here is my code that I wrote in Eclipse. I created a Scala Project and a Scala class within the src folder.

class Hello_WORLD {
  def main(args: Array[String]){
     println("HELLO!")
  }
}

Can you please help me? Please try not to leave rude comments. This is my first time trying out Scala. I would greatly appreciate your help. I tried looking at the Scala documentation and other posts on StackOverflow, but none of them helped my situation. I made sure that the environment variables were configured correctly. If you need any more information, please let me know in the comments.

Regards,

Ani


Solution

  • There's an option in Eclipse:

    • From the Package Explorer tab -> Select the project src directory -> Right click -> New -> Scala Application
    • From File -> New -> Scala Application

    Choosing one of those for the default package, lets me create this:

    object HelloWorld extends App {
      println( "Hello World")
    }
    

    Notice that the "main" comes from extending App. It's an object instead of a class and you can run it inside of Eclipse via:

    • Choosing it in the Package Explorer -> right click -> Run As -> Scala Application
    • Run -> Run Configurations -> Scala Applications -> New -> Hello World -> Run

    If you want to run that from the command line, you can Export your code to a jar file using:

    • Package Explorer tab -> right click -> Java -> Jar file -> browse to choose the name/location of the Jar file.