I am getting the following error when compiling a Hello World Program in Netbeans.
C:\Users\Norman\Documents\NetBeansProjects\LambdaCollectionExamples\ScalaApplication2\nbproject\build-impl.xml:238: bad option: '-make:transitive'.
This is the xml file:-
<scalac addparams="-make:transitive -dependencyfile "${basedir}/${build.dir}/.scala_dependencies" #{addparams}"
deprecation="${scalac.deprecation}"
destdir="#{destdir}"
encoding="${source.encoding}" excludes="#{excludes}"
extdirs="#{extdirs}"
force="yes"
fork="true"
includes="#{includes}"
sourcepath="#{sourcepath}"
srcdir="#{srcdir}"
target="jvm-${javac.target}"
unchecked="${scalac.unchecked}">
This is the program
package scalaapplication2
object Main {
/**
* #param args the command line arguments
*/
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
Scala 2.11.2 was giving me such errors. Download Scala 2.10.4 binaries for your system should resolve it.
ant -f C:\\Users\\wayne\\Documents\\NetBeansProjects\\ScalaApplication2 run
init:
deps-jar:
Compiling 1 source file to C:\Users\wayne\Documents\NetBeansProjects\ScalaApplication2\build\classes
Recompiling 1 files
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
one warning found
compile:
run:
Hello, world!
BUILD SUCCESSFUL (total time: 3 seconds)
Related
I was reading this question and saw:
scalac includes an 8-bit simulator of a fully armed and operational battle station, viewable using the magic key combination CTRL-ALT-F12 during the GenICode compilation phase.
To show what compilation phases scalac has, I used scalac -Xshow-phases.
phase name id description
---------- -- -----------
< ... >
icode 23 generate portable intermediate code
Seems to be the relevant phase.
I compile a Hello-world program
object Hello {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
by scalac Hello.scala and pressed CTRL-ALT-F12 all the way. Nothing happened. And tried scalac -Xprint:icode Hello.scala with CTRL-ALT-F12, nothing special as well.
How can I trigger the simulator? And what does that do?
On scala 2.11.8 and OS X 10.11.5
I'm using Intellij-idea for scala programming (with sbt plugin).
I want to know what is the difference between scala classes, scala scripts and scala worksheets. When we use each of them?
This will be very nice if you can explain it by a simple example.
Thanks
You have different ways of running scala code:
First create a Program with your classes, this is as in java, I use object because it works well without instantianing, like static, just compile with the SBT and run it you can also use the scala Interpreter REPL
We can use this object in the REPL
scala> object Hello {
| def main(args:Array[String]) {
| println("Hello, Scala !!")
| }
| }
defined object Hello
scala> Hello.main(Array("onlyforwork"))
Hello, Scala !!
compiling and running it using activator/SBT
> compile
[info] Compiling 1 Scala source to /home/anquegi/Dev/StackOverFlow/scalaStack/target/scala-2.11/classes...
[success] Total time: 2 s, completed 13/04/2015 11:29:42
> run
[warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list
Multiple main classes detected, select one to run:
[1] org.example.Hello
[2] org.example.ScheduledTaskScala
[3] question1.Ques
[4] scriptworksheet.Hello
Enter number: 4
[info] Running scriptworksheet.Hello
Hello, Scala !!
[success] Total time: 19 s, completed 13/04/2015 11:30:04
The second is that if we add the scala code as a script or file Hello.scala, You can save your scala code in the file with .scala extension (basically with any file extension but prefered .scala extension) and to run, provide file name with extension as parameter to scala interpreter
/**
* Created by anquegi on 13/04/15.
*/
println("Hello, Scala !!")
if we call the scala interpreter this file is executed, you do not need to instanciate objects or clases, just executing like a shell script, you can also execute directlyy from Intellij, but I use the console with scala installed on the system
[anquegi#localhost scalaStack]$ scala src/main/scala/scriptworksheet/HelloScript.scala
Hello, Scala !!
And finally the worksheet is the most powerfull, I recommend this for increasing your prodductivity at work bacause it is easy to test things is like the REPL, ant it evluates the scala exprssions and shows you back the result
Following is excerpt from official github repo wiki about the scala worksheet
A worksheet is a Scala file that is evaluated on save, and the result of each expression is shown in a column to the right of your program. Worksheets are like a REPL session on steroids, and enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, auto-format, etc.
// We can define objects or classes and the worksheet
//will print the sesult of every expression
object Hello {
def main(args:Array[String]) {
println("Hello, Scala !!")
}
}
println("Hello Scala")
val a = 4 + 5
the result
defined module Hello
Hello Scala
res0: Unit = ()
a: Int = 9
then a capture that shows you working with classe the work sheet and the console for scriptsin the Intellij
Scala Worksheet
It's the same as Scala Interpreter (REPL) but runs inside IntelliJ. Where you may easily and quickly evaluate some expressions. Check IntelliJ confluence page for more information.
Scala Script
If you don't want write script on Bash you can do it with Scala. It's just sequence of Scala statements.
Example:
import java.nio.file.{Paths, Files}
val ScalaSource = "*.scala"
val Path = "path/to/necessary/folder"
val stream = Files.newDirectoryStream(Paths.get(Path), ScalaSource)
val paths = stream.iterator
while (paths.hasNext) {
println(paths.next.getFileName)
}
Running it:
$ scala scala_script_name.scala
To getting started pick up this guide.
Classes & Object
Short answer for Scala classes it's similar to POJO and Scala Objects it's a Java Singleton class.
I'm a newbie in scala. I recently started using netbeans for writing scala programs.I have installed scala version 2.11.0.400 and its plugins for netbeans.When I try to compile my scala code:-
package scalaapplication2
object Main {
def main(args: Array[String]): Unit=
{
println("Hello, world!")
}
}
I get the following compilation error even after setting the path variables:-
Compiling 1 source file to C:\Users\Nitin\Documents\NetBeansProject\ScalaApplication2\build\classes
C:\Users\Nitin\Documents\NetBeansProjects\ScalaApplication2\nbproject\build- impl.xml:423:
The following error occurred while executing this line:
C:\Users\Nitin\Documents\NetBeansProjects\ScalaApplication2\nbproject\build-impl.xml:238: bad option: '-make:transitive'
Why is it so?
Removing '-make:transitive' from the scalac addparams option (Files view > ScalaApplication > nbproject > build-impl.xml) worked for me.
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.
I installed the scala plugin on my netbeans and followed the instruction of this page:
http://wiki.netbeans.org/Scala68v1#Scala_Plugins_for_NetBeans_6.8_v1.x_.28RC2.29
but after it completed correctly step by step, when I make an empty project (Hello world!), the project has an error!
The empty project is here:
package scalaapplication1
object Main {
/**
* #param args the command line arguments
*/
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
and the console error report is:
...\NetBeansProjects\ScalaApplication2\nbproject\build-impl.xml:403: The following error occurred while executing this line:
...\NetBeansProjects\ScalaApplication2\nbproject\build-impl.xml:236: scalac doesn't support the "fork" attribute
Is there any suggestion about it?!
I had the same problem with Scala 2.7.7. After installing 2.8.0.RC1 it worked fine.
Henri