getclass

Getting started with the Scala REPL (command-line shell)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.1, “How to get started with the Scala REPL.”

Back to top

Problem

You want to get started using the Scala REPL, including understanding some of its basic features, such as tab completion, starting the REPL with different options, and dealing with errors.

Back to top

Solution

To start the Scala REPL, type scala at your operating system command line:

Table of Contents

  1. Problem
  2. Solution
Back to top

How to determine the class of a Scala object (getClass)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 6.3, “How to determine the Class of an object.”

Problem

Because you don’t have to explicitly declare types with Scala, you may occasionally want to print the class/type of an object to understand how Scala works, or to debug code.

Solution

When you want to learn about the types Scala is automatically assigning on your behalf, call the getClass method on the object.

Scala equivalent of Java .class (classOf)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shorter recipes, Recipe 6.2, “The Scala equivalent of Java’s .class.”

Problem

When an API requires that you pass in a Class, you’d call .class on an object in Java, but that doesn’t work in Scala.

Java Jar file: How to read a file from a Jar file

Java jar file reading FAQ: Can you show me how a Java application can read a file from own of its own Jar files?

Here's an example of some Java code I'm using to read a file (a text file) from a Java Jar file. This is useful any time you pack files and other resources into Jar files to distribute your Java application.

Java - read Jar file example #1

The source code to read a file from a Java Jar file uses the getClass and getResourceAsStream methods: