I am studying Scala and I am using Scala for win 10
I have write a script "helloarg.scala" with 1 line println("Hello, " + args(0) + "!")
When i used cmd to run file (not in scala shell), it worked.
C:\Users\Darkntnt>scala D:\Scala\helloarg.scala planet
Hello, planet!
However, It got the error when i load file from scala shell
Error:
scala> :load D:\Scala\helloarg.scala planet
usage: :load -v file
Please help me to fix this problem.
Thank you.
That's a good one.
:load doesn't respect args; there are a few different idioms for "run an app as a script".
I'll add this example to the issue about unifying them.
As a workaround, maybe val args = Array("planet") or similar.
Related
Need to execute the scala script through spark-shell with silent mode. When I am using spark-shell -i "file.scala", after the execution, I am getting into the scala interactive mode. I don't want to get into there.
I have tried to execute the spark-shell -i "file.scala". But I don't know how to execute the script in silent mode.
spark-shell -i "file.scala"
after execution, I get into
scala>
I don't want to get into the scala> mode
Updating (October 2019) for a script that terminates
This question is also about running a script that terminates, that is, a "scala script" that run by spark-shell -i script.scala > output.txt that stopts by yourself (internal instruction System.exit(0) terminates the script). See this question with a good example.
It also needs a "silent mode", it is expected to not pollute the output.txt.
Suppose Spark v2.2+.
PS: there are a lot of cases (typically small tools and module/algorithm tests) where Spark interpreter can be better than compiler... Please, "let's compile!" is not an answer here.
spark-shell -i file.scala keeps the interpreter open
in the end, so System.exit(0) is required to be at the end of your script. The most appropriate solution is to place your code in try {} and put System.exit(0) in finally {} section.
If logging is requiered you can use something like this:
spark-shell < file.scala > test.log 2>&1 &
If you have limitations on editing file and you can't add System.exit(0), use:
echo :quit | scala-shell -i file.scala
UPD
If you want to suppress everything in output except printlns you have to turn off logging for spark-shell. The sample of configs is here. Disabling any kind of logging in $SPARK-HOME/conf/log4j.properties should allow you to see only pritnlns. But I would not follow this approach with printlns. Using general Logging with log4j should be used instead of printlns. You can configure it so obtain the same results as with printlns. It boils down to configuring a pattern. This answer provides an example of a pattern that solves your issue.
The best way is definitively to compile your scala code to a jar and use spark-submit but if you're simply looking for a quick iteration loop, you can simply issue a :quit after parsing your scala code:
echo :quit | scala-shell -i yourfile.scala
Adding onto #rluta's answer. You can place the call to spark-shell command inside a shell script. Say the below in a shell script:
spark-shell < yourfile.scala
But this would require you to keep the lines of code within a line in case a statement is written on different lines.
OR
echo :quit | spark-shell -i yourfile.scala
This should
I am trying to load a spark scala script into a spark shell using load command where the location of the script is passed in a variable. It's not working
val scriptLoc="/abc/spark"
:load ${scriptLoc}/scriptName.scala
Even tried like this which didn't work either
:load scriptLoc/scriptName.scala
Any help would be appreciated.
You can try
spark-shell -i /path/to/file.scala
From the Scala REPL I am running a program thus:
:load foo.scala
I want to pass foo a parameter so I try:
:load foo.scala 1.0
which gives:
usage: :load -v file
Is there any way of running a program from the REPL with parameters like this?
If it is a single source file, I would recommend using the :paste command with which you can copy the whole source file into the REPL and then do a ctrl + c to exit the paste mode.
You could then call your program from the REPL by passing in the parameters that you need!
I'm exploring to use Scala REPL bridge to shell.It is mainly achieved by import sys.process._ package and I can use "ls" ! to execute shell.
Now, I want to use "vi" ! to open a interactive vi editor, it is really crazy but very exciting. After entering the cmd in REPL, the terminal opens a init vi canvas. Unfortunately, the terminal is not reading any input from my keyboard.
Is it possible to open a vi in REPL?
from https://stackoverflow.com/a/29972867/1573825 (java solution):
import java.lang.{Process, ProcessBuilder}
System.out.println("STARTING VI");
val processBuilder = new ProcessBuilder("/usr/bin/vi")
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT)
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT)
processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT)
val p = processBuilder.start()
// wait for termination.
p.waitFor()
System.out.println("Exiting VI")
it doesn't even corrupt readline.
I've been trying to find some sort of a dotfile to put Scala REPL settings and custom function in.
In particular I'm interested in passing it flags like -Dscala.color (enables syntax highlighting), as well as overriding settings like result string truncation:
scala> :power
scala> vals.isettings.maxPrintString = 10000
It would be nice to have these settings apply to both the simple Scala REPL sessions as well as sbt console sessions.
Does such a central configuration place exist for Scala?
Maybe you can use a modernized Scala REPL:
https://lihaoyi.github.io/Ammonite/
Poor man's solution: Set yourself an alias
alias myScala='scala -Dscala.repl.axPrintString = 10000'
As mentioned here ~/.sbt/0.13/global.sbt is the global configuration file for sbt. You can change your global settings here, this probably not going to effect REPL but should do work with SBT Console
You mainly asked about property settings, this goes a little beyond that to consider loading a definitions file as well—and isn't much help for Windows—but I thought I'd share in case it's useful:
I've resorted to using a wrapper script saved as ~/bin/scala, to set config properties and load some utility functions:
#!/bin/sh
# The scala REPL doesn't have any config file, so this wrapper serves to set
# some property values and load an init file of utilities when run without
# arguments to enter REPL mode.
#
# If there are arguments, just assume we're running a .scala file in script
# mode, a class or jar, etc., and execute normally.
SCALA=${SCALA:-/usr/local/bin/scala}
if [ "$#" -eq 0 ] && [ -r ~/.config/scala/replinit.scala ]; then
exec "$SCALA" -i ~/.config/scala/replinit.scala -Dscala.color
else
exec "$SCALA" "$#"
fi
If you sometimes use Ammonite REPL, as another answer suggests, the utility definitions can be shared by loading them from ~/.ammonite/predef.scala:
try load.exec(ammonite.ops.home/".config"/'scala/"replinit.scala")
catch { case _: Exception => println("=== replrc not loaded! ===") }
I'm not sure about a way to load the init file for sbt console automatically, though—Seth Tisue's comment about the initialize setting is helpful for properties, but using a :load command in a value for initialCommands in console doesn't appear to work.