Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:24:54+00:00 2026-06-09T19:24:54+00:00

This question may sound a bit stupid, but I couldn’t figure out, how to

  • 0

This question may sound a bit stupid, but I couldn’t figure out, how to start a Scala method from the command line.

I compiled the following file Test.scala :

package example

object Test {
  def print() {
    println("Hello World")
  }

}

with scalac Test.scala.

Then, I can run the method print with scala in two steps:

C:\Users\John\Scala\Examples>scala
Welcome to Scala version 2.9.2 (Java HotSpot(TM) Client VM, Java 1.6.0_32).
Type in expressions to have them evaluated.
Type :help for more information.

scala> example.Test.print
Hello World

But what I really like to do is, to run the method directly from the command line with one command like scala example.Test.print.

How can I achieve this goal ?

UPDATE:
Suggested solution by ArikG does not work for me – What I am missing ?

C:\Users\John\Scala\Examples>scala -e 'example.Test.print'
C:\Users\John\AppData\Local\Temp\scalacmd1874056752498579477.scala:1: error: u
nclosed character literal
'example.Test.print'
         ^
one error found

C:\Users\John\Scala\Examples>scala -e "example.Test.print"
C:\Users\John\AppData\Local\Temp\scalacmd1889443681948722298.scala:1: error: o
bject Test in package example cannot be accessed in package example
example.Test.print
        ^
one error found

where

C:\Users\John\Scala\Examples>dir example
 Volume in drive C has no label.
 Volume Serial Number is 4C49-8C7F 

 Directory of C:\Users\John\Scala\Examples\example

14.08.2012  12:14    <DIR>          .
14.08.2012  12:14    <DIR>          ..
14.08.2012  12:14               493 Test$.class
14.08.2012  12:14               530 Test.class
               2 File(s)          1.023 bytes
               2 Dir(s)  107.935.760.384 bytes free

UPDATE 2 – Possible SOLUTIONs:

  • As ArikG correctly suggested, with scala -e "import example.Test._; print" works well with Windows 7.
  • See answer of Daniel to get it work without the import statement
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:24:55+00:00Added an answer on June 9, 2026 at 7:24 pm

    Let me expand on this solution a bit:

    scala -e 'example.Test.print'
    

    Instead, try:

    scala -cp path-to-the-target-directory -e 'example.Test.print'
    

    Where the target directory is the directory where scala used as destination for whatever it compiled. In your example, it is not C:\Users\John\Scala\Examples\example, but C:\Users\John\Scala\Examples. The directory example is where Scala will look for classes belonging to the package example.

    This is why things did not work: it expected to find the package example under a directory example, but there were no such directory under the current directory in which you ran scala, and the classfiles that were present on the current directory were expected to be on the default package.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a bit of a rhetorical question, but I ask it
This may sound like a stupid question but I can't seem to find an
This may sound like a stupid question, perhaps it is. But I'm only trying
Ok this question may sound a bit convoluted, or at least esoteric, but I'll
This may sound like a strange question, but hopefully there is an answer out
The question may sound a bit... stupid. But I'm kinda stuck. I'm using Twitter
This question may sound stupid but I wana know When do we put activity
This may sound a bit provocative but it actually is a real question. Feel
This may sound like a bit of a dumb question but how do I
This question may sound a bit weird/novice/stupid. Please bear with me. The below code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.