0

I have a file named Hello.scala that contains this simple code:

object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world!")
    }
}

I compiled it using this command:

$ scalac Hello.scala

The result was two files named HelloWorld.class and HelloWorld$.class. What is these files? How can I use these files?

Note: My problem is not duplicate files. I want to know what is this files and how can I execute them?

Thanks for answer

Omid Ebrahimi
  • 1,150
  • 2
  • 20
  • 38
  • possible duplicate of [Multiple .class files generated for a class?](http://stackoverflow.com/questions/1031962/multiple-class-files-generated-for-a-class) – Boris the Spider Apr 13 '15 at 15:48
  • My main problem is that I don't khow how can I use these classes? How to run one of these files and get "Hello, world!"? – Omid Ebrahimi Apr 13 '15 at 15:59
  • 1
    @BoristheSpider First, the link only covers multiple class files for inner classes, not the semantics of scala companion objects and such. Also, that tutorial is out of date as noted, you need http://www.scala-lang.org/documentation/getting-started.html – Justin Pihony Apr 13 '15 at 16:04
  • 1
    @JustinPihony I think the reason for the multiple `.class` files is still the same - multiple classes; but fair enough - Scala generates much random magic. On the updated link - besides the formatting, I don't really see much difference in the tutorial... – Boris the Spider Apr 13 '15 at 16:06
  • No, but there is always the possibility, so why not point to the up to date reference instead of the one not being updated at all? – Justin Pihony Apr 13 '15 at 16:09
  • 1
    Just a tip: using scalac directly gets to be a huge pain really quickly. Investing in a basic understanding of SBT up front is almost definitely worth it. – Travis Brown Apr 13 '15 at 16:09
  • Thanks @BoristheSpider. I get my answer. I could execute HelloWorld.class using your tutorial. :) – Omid Ebrahimi Apr 13 '15 at 16:17
  • Thanks @JustinPihony for your updated Tutorial. – Omid Ebrahimi Apr 13 '15 at 16:18

1 Answers1

1

This files are normal Java Class / Bytecode Files.

You can run them with

scala HelloWorld
//Hello, world!

Normally you also could run them with

java HelloWorld$ // Because it is an object which gets the $ sign at the end

But this throws an error as there has to be some wiring done to handle the special case of Object.

Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52