Anish Muthali
Anish Muthali

Reputation: 782

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

Upvotes: 2

Views: 2206

Answers (3)

Arpit Suthar
Arpit Suthar

Reputation: 754

A scala programm needs a main class to work like java but if u dont want to define a "main" then you can go for "object" with extending "App". This will work fine. In this case you will not asked to have a "main" class.

object FirstProg extends App {
   println("My first prog...!")
}

Upvotes: 0

Omid
Omid

Reputation: 1989

Here is all steps you have to take:

1- Install Scala and make sure the path is included in the windows path variable.( to check it, type scala in terminal, it should go to scala console 2- cd to directory with HellWorld.scala 3- the code should be this:

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

4- scalac Helloworld.scala

it should work.

Upvotes: 0

n0741337
n0741337

Reputation: 2514

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.

Upvotes: 1

Related Questions