how to call python script in Scala ide - scala

I want to run python script in Scala program, I ran the python script in spark-shell, but same code I tried in Scala IDE it throws "No such file or directory".
My code in spark-shell:-
python file : a.py
scala script:-
file="a.py"
Seq("python",file) !
Above code ran successfully in spark-shell and I tried same code in Scala IDE
object newtest {
def runpython(file:String)={
Seq("python",file) !
}
def main(args: Array[String]):Unit={
val file="/sample/a.py"
runpython(file)
}
}
note:- "/sample" is project folder in scala IDE
Please help me on this.
Thanks,
Prasad.

I would guess that the missing file is python.
try setting the full path of the python executable

Related

Why does scala not see my hello.class as a program in command prompt?

I am trying to run scala program in cmd prompt. And I get the message.
"No, such file or class on class path exists."
Things that may be relevant:
I had several warnings from Windows about downloading and running the scala executable from the site. (I ignored them all )
I had to enter a Admin Command Line to use scala, and it did not show up in my program files after install.
The installer never actually popped up for me. So I ran the executable as administration, now
it appears in my program X86 folder. and I can enter scala mode without being in Admin mode. 4. The .scala file does compile. with scalac hello.scala
The code given to me by my instructor, in a file I named hello.scala object hello extends App{ println("Hello") }
Finally, I have looked at 2 Youtube videos and 2 Stack posts before asking this question. Thank you all for your time.
Here's a complete transcript of a successful session compiling and running on this code:
% echo 'object hello extends App{ println("Hello") }' > hello.scala
% scalac hello.scala
% scala hello
Hello
Are you doing something different? If so, what is it? Show a transcript of your entire effort, including all commands you entered and all error messages you got.

Error: Could not find or load main class Main Scala

I've recently installed Scala as a part of my functional programming course and I've encountered a problem: IntelliJ IDEA 2017.2.1 (Java version 9, build 9+181) doesn't run any of my scala code, exitting with
Error: Could not find or load main class Main
This code is an example.
object Main {
def length[A](list:List[A]):Int = {
if (list == Nil) 0
else 1 + length(list.tail)
}
def main(args: Array[String]): Unit = {
length(List[Int](1, 4, 5, 12, -1))
}
}
It's really simple, yet IntelliJ refuses to run it. Windows CMD doesn't even react to a scala command, resulting into
'scala' is not recognized as an internal or external command,
operable program or batch file.
even though I have it installed on my computer. If I call Scala Console inside of IntelliJ everything works fine and compiles as expected. I've tried switching to JDK 1.8 inside of IntelliJ, yet it led to no result.
What could be the problem?
For me it turns out that the src/main was not marked as Sources Root
which causes the following error
...
One of the two will be used. Which one is undefined.
Error: Could not find or load main class Main
Process finished with exit code 1
So of course after I mark the src/main as Sources Root, the Scala Hello World example runs happy again.
Notice the blue color of directory src/main when it's marked as Sources Root
Are you using the little green arrow to run the program from inside of your Main object?
How did you create the program? It could be that your build file SBT configuration of the project is a different Scala version than what's installed on your computer.
It's really simple, yet IntelliJ refuses to run it. Windows CMD
doesn't even react to a scala command, resulting into
'scala' is not recognized as an internal or external command, operable program or batch file.
This means that Scala is not added to your class path in your terminal. Look up how to do that and see if that doesn't help out your IntelliJ problem too.

Object pythagorean is not a member of package com - Scala programming error

I am working through Atomic Scala 2.0, learning Scala. I am trying to create a package myself and trying to use it in my programs. This is the code for creating my package:
package com.atomicscala.pythagorean
class RightTriangle {
def hypotenuse(a:Double, b:Double):Double={
math.sqrt(a*a + b*b)
}
def area(a:Double, b:Double):Double={
a*b/2
}
}
And this is my program:
import com.atomicscala.pythagorean._
object test{
def main(args:Array[String])={
val rt = new RightTriangle
println(rt.hypotenuse(3,4))
}
}
When I try to run my program after compiling the package, I get an error like this:
error: object pythagorean is not a member of package com.atomicscala
Instead, if I name the package as just pythogorean, the code works fine. what is causing this?
Try it with the package spelled correctly:
You have:
package com.atmoicscala.pythagorean
probably should be
package com.atomicscala.pythagorean
I was able to solve it by mentioning the Classpath in the Scala command.
scala filename -classpath . (If the package is in the current working
directory)
scala filename -classpath PackageLocation
The root cause is on the "fsc", fsc will reset when classpath change.
You may reset it explicitly by execute the following
fsc -reset
then you don't need to include -classpath in your scala command.
note: I am not sure whether this is a good practice.
Another way is to run the scala command without using the fsc offline compiler
scala -nc filename

Is it possible to run rascal programs from the command line?

I know how to run rascal code from within eclipse and how to use the REPL, but I don't know how I can run a rascal file (or group of rascal files) as a program from the command line.
When I try the following, I get a parse error:
$ java -Xmx1G -Xss32m -jar rascal-shell-stable.jar mymodule.rsc
Version: 0.7.2.201501130937
Parse error in cwd:///mymodule.rsc from <1,11> to <1,12>
The contents of mymodule.rsc:
module mymodule
println("hello world");
What am I doing wrong?
Well, your mymodule.rsc is actually syntactically incorrect and will also give parse errors in the Eclipse IDE. Here is an improved version:
module mymodule
import IO;
value main(list[value] args) {
println("hello world");
}
Bonus: you should also add import IO; to make the println function available.

Why the "hello, world" is not output to the console?

I'm just learning scala, and I wrote the "hello,world" program like this:
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
I saved it to a file named "helloworld.scala"
Now I run it in the console:
scala helloworld.scala
But nothing outputed. I thought it will output "Hello, world". Why?
PS
If I modify the content to:
println("Hello, world")
and run again, it will output "hello,world".
if you want to run your code as a script (by using scala helloworld.scala) you have to tell scala where your main method is by adding the line
HelloWorld.main(args)
to your code
the second option you have is compiling the script by using scalac helloworld.scala
and then calling the compiled version of your class using scala HelloWorld
You have two options.
Compile and Run:
As in Java you should have a main-method as a starting point of you application. This needs to be compiled with scalac. After that the compiled class file can be started with scala ClassName
scalac helloworld.scala
scala HelloWorld
Script:
If you have a small script only, you can directly write code into a file and run it with the scala command. Then the content of that file will be wrapped automagically in a main-method.
// hello.scala, only containing this:
println("Hello, world!")
then run it:
scala hello.scala
Anyway I would recomment to compile and run. There are some things, that are not possible in a "Scalascript", which I do not remember right now.