7/21/2023 0 Comments Scala codeThe examples shown in this recipe are shown for the “one off ” cases where you might want to compile and run a small application or test code.įor other useful command-line options, see the manpages for the scalac and scala commands. $ scala -classpath classes:lib/DateUtils.jar Īs you can imagine, this process gets more and more difficult as you add new classes and libraries, and it’s strongly recommended that you use a tool like SBT, Maven, or Ant to manage your application’s build process. Once the files have been compiled in this manner, you can run the application like this: classes/com/alvinalexander/pizza/Topping.class classes/com/alvinalexander/pizza/Topping$.class classes/com/alvinalexander/pizza/Pizza.class We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely. classes/com/alvinalexander/pizza/Main.class Our mission: to help people learn to code for free. Scaladoc is a documentation generation system that reads specially formatted comments in Scala source code and generates compiled documentation. classes/com/alvinalexander/pizza/Main$delayedInit$body.class classes/com/alvinalexander/pizza/Main$.class Scala is a type-safe JVM language that incorporates both object oriented and functional programming into an extremely concise, logical, and extraordinarily powerful language. $ scalac -classpath lib/DateUtils.jar -d classes " src/com/alvinalexander/pizza/*Īssuming Main.scala is an object that extends App, Pizza.scala is a regular class file, and Topping.scala is a case class, your classes directory will contain these files after your scalac command: Given this configuration, to compile your source code files to the classes directory, use the following command: The Main.scala, Pizza.scala, and Topping.scala files will also have package declarations corresponding to the directories they are located in, i.e.: src/com/alvinalexander/pizza/Topping.scala src/com/alvinalexander/pizza/Pizza.scala src/com/alvinalexander/pizza/Main.scala In this case, your files and directories will look like this: In a more complicated example, you may have your source code in subdirectories under a src folder, one or more JAR files in a lib directory, and you want to compile your output class files to a classes folder. Override def toString = toppings.toStringĪssuming that the Topping class is compiled to a file named Topping.class in a subdirectory named classes, compile Pizza.scala like this: For instance, if you have a class named Pizza in a file named Pizza.scala, it may depend on a Topping class: For example, given a Scala source code file named Hello.scala:Ĭompile it from the command line with scalac:Ĭompiling and executing classes is basically the same as Java, including concepts like the classpath. SolutionĬompile programs with scalac, and run them with scala. Though you normally use the Simple Build Tool (SBT) to build Scala applications, you may want to use more basic tools to compile and run small test programs, in the same way you might use javac and java with small Java applications. This is Recipe 14.5, “How to compile Scala code with scalac and run it with scala.” Problem This is an excerpt from the Scala Cookbook. Run the command sbt new scala/scala3.g8 to create a Scala 3 project, or sbt new scala/hello-world.g8 to create a Scala 2 project. (It can also publish libraries and do many other tasks.) To create a new Scala project with sbt: cd to an empty folder.
0 Comments
Leave a Reply. |