Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala runner doesn't do executable jars #12569

Closed
som-snytt opened this issue May 23, 2021 · 11 comments · Fixed by #13246 or #13263
Closed

Scala runner doesn't do executable jars #12569

som-snytt opened this issue May 23, 2021 · 11 comments · Fixed by #13246 or #13263
Assignees
Milestone

Comments

@som-snytt
Copy link
Contributor

Compiler version

3.0.0

Minimized code

➜  snips sdk use scala 2.13.6

Using scala version 2.13.6 in this shell.
➜  snips scalac -d hi.jar hi.scala
➜  snips scala hi.jar
hello, world
➜  snips sdk use scala 3.0.0

Using scala version 3.0.0 in this shell.
➜  snips scalac -d hi.jar hi.scala
➜  snips scala hi.jar
Error: Could not find or load main class hi.jar
Caused by: java.lang.ClassNotFoundException: hi.jar
➜  snips scala -classpath hi.jar Main
hello, world

Output

Error: Could not find or load main class hi.jar

Expectation

Scala runner creates and runs an executable jar. It looks like it doesn't create META-INF/MANIFEST.MF.

@okarmusk
Copy link

I've got the same issue when run scala 3 from binary.
Bug is visible even if jar is build from sbt package where sbt is in version 1.5.2. Please check issue what has been added by @adpi2

@okarmusk
Copy link

I built the same source code with the same sbt version but I compile and run it with scala 2.13.6, it works.
Issue exists only in scala 3.

@som-snytt
Copy link
Contributor Author

I'll take a swing at this, since it's such a useful niche feature. By swing, I mean a bunt down the 3rd base line.

@dwijnand
Copy link
Member

Overlaps with #13081, so either make sure to coordinate with @BarkingBad.

@BarkingBad BarkingBad self-assigned this Jul 27, 2021
@BarkingBad BarkingBad linked a pull request Aug 3, 2021 that will close this issue
@BarkingBad
Copy link
Contributor

I tested it @anatoliykmetyuk and compiling files using scalac -d hi.jar hi.scala doesn't provide Manifest file. The way to get runnable jar is to run it by scala -save hi.scala and then run it as scala hi.jar

Should we introduce change to scalac that would make Manifest file?

@som-snytt
Copy link
Contributor Author

Scala 2 distinguished

  -Xmain-class <path>              Class for manifest's Main-Class entry (only useful with -d <jar>)
  -Xscript <object>                Treat the source file as a script and wrap it in a main method.

and REPL

Other startup options:

 -i <file>    preload <file> before starting the REPL
 -I <file>    preload <file>, enforcing line-by-line interpretation
 -e <string>  execute <string> as if it were in a source file
 -save        save the compiled script in a jar for future use

@anatoliykmetyuk
Copy link
Contributor

Should we introduce change to scalac that would make Manifest file?

I believe so. In any case this shouldn't be a -save option to produce a manifest for a runnable jar, as "save" isn't very related to "runnable".

@BarkingBad
Copy link
Contributor

I guess bringing back -Xmain-class should be easy.
However, I don't know if -Xscript should be in the scope of this task and whether we need it at all in the compiler, we can run scripts by running scala <path-to-script>

@anatoliykmetyuk
Copy link
Contributor

Yes, we don't need -Xscript anymore since we have a better, more seamless, scripting support in Scala 3.

@som-snytt
Copy link
Contributor Author

I don't have a grid for Scala 3 runner options yet, let alone the recommended install procedure.

Probably -Xscript is not worth supporting, but with parser phase broken out, it could be supported third-party. It just wraps code in a main method in parser:

Using scala version 2.13.6 in this shell.
➜  snips cat simple.sc

println(s"hello, ${util.Properties.userName}")
➜  snips scalac -Xscript Main -d /tmp simple.sc
➜  snips scala -cp /tmp Main
hello, amarki
➜  snips scala -e 'println(s"hello, ${util.Properties.userName}")'
hello, amarki
➜  snips scala -howtorun:script simple.sc
hello, amarki

I don't know if the feature is used outside -e, or I guess people still use shell scripts, but there is some simplicity in avoiding REPL semantics.

@anatoliykmetyuk
Copy link
Contributor

Yes, we discussed whether we should go Python-style or tolerate @main in scripts earlier. The argument for @main was not only simplicity but also the ability to accept command arguments in a natural way. Another argument was that scripts and programs ideally should be the same thing; we should strive to avoid differentiating between the two. Overall we got the consensus that @main in scripts is fine.

@BarkingBad BarkingBad linked a pull request Aug 6, 2021 that will close this issue
@Kordyjan Kordyjan added this to the 3.1.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants