question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The scala document not match the right @main method define

See original GitHub issue

Compiler version

scala 3.1.3
$ scalac --version
Scala compiler version 3.1.3 -- Copyright 2002-2022, LAMP/EPFL
$ sbt --version
sbt version in this project: 1.6.2
sbt script version: 1.6.2

Minimized code

https://docs.scala-lang.org/scala3/book/taste-hello-world.html The document show not right code, which not equals the java public static void main

which will cause sbt assembly can not know the entry method.

The current document:

@main def hello() = println("Hello, world!")

What the really equals code [as for java]

object main:
  def main(args: Array[String]): Unit  =
    println("hello world")

Output

#java -jar target/scala-3.1.3/myapp-assembly-0.1.0-SNAPSHOT.jar
Error: Main method not found in class com.example.main, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Expectation

 $ java -jar target/scala-3.1.3/myapp-assembly-0.1.0-SNAPSHOT.jar
 hello world

@main works without inside object class; so my request is: why @main can not working the same , with or without the object main

The document told us: A @main annotated method can be written either at the top-level or in a statically accessible object. https://docs.scala-lang.org/scala3/reference/changed-features/main-functions.html

No , it is not right, it only can work at top level, if put @main inside a statically accessible object, it won’t compile right

object main:
  @main def hello(): Unit  =

The generate class file

$ javap -c main.class
Compiled from "main.scala"
public final class info.bsmi.sc3.main {

  public static void hello();
    Code:
       0: getstatic     #13                 // Field info/bsmi/sc3/main$.MODULE$:Linfo/bsmi/sc3/main$;
       3: invokevirtual #19                 // Method info/bsmi/sc3/main$.hello:()V
       6: return

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
micheloucommented, Jul 9, 2022

See also section “A.12.4 The Oberon Environment” in document Oberon Language Definition for people (much) younger than Martin and myself 😉

0reactions
oderskycommented, Jul 9, 2022

We want to have several main methods. The decision to name the class after the method is really the core of the design.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Main Methods
Main Methods. Scala 3 offers a new way to define programs that can be invoked from the command line: A @main annotation on...
Read more >
Main Methods - Scala 3 - EPFL
Scala 3 offers a new way to define programs that can be invoked from the command line: A ... The @main method can...
Read more >
Error: Selection does not contain a main type - Stack Overflow
Right click on the folder where you put your main class then click on Build Path --> Use as Source Folder . Finally...
Read more >
4. Pattern Matching with Regular Expressions - Java ... - O'Reilly
I used the pattern main(String [a-z] with the grep utility described ... methods returns boolean , with true meaning a match and false...
Read more >
Myth about the file name and class name in Java
When the above file is compiled as javac Trial.java this will create 2 .class files as ForGeeks.class and GeeksTest.class . Since each class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found