tags:

views:

45

answers:

2

Under Windows, I can run a Scala script from a different directory using a batch script like:

Hello.bat:

@scala "%~dp0Hello.scala" %*

(%~dp0 will be translated to the path where the batch file is)

So I can call it like this:

c:\somedir>path\to\scala\script\Hello
Hello World!
c:\somedir>path\to\scala\script\Hello Moon
Hello Moon!

Or, if the directory where the script is is already in the path, I could simply use:

c:\somedir>Hello
Hello World!
c:\somedir>Hello Moon
Hello Moon!

I can't do the same thing for compiled classes:

@scala "%~dp0Hello.class" %*

won't work, and

@scala -howtorun:object "%~dp0Hello.class" %*

wont't work either, as well as

@scala -howtorun:object "%~dp0Hello" %*

This one:

@scala -howtorun:object "Hello" %*

only works if I am at the same directory, same as:

@scala Hello %*

And:

@cd %~dp0
@scala Hello %*

works, but it exits in the directory of the script, not where I was when I called it!

How can I tell scala where to find a class that I am trying to run?

+2  A: 

(Let me know if I've misunderstood your question, as I suspect you know this already ... )

Classes to be executed must be on the classpath. Simply put you can either:

set CLASSPATH=/path/to/where/your/base/package/is;%CLASSPATH%

or you can put it explicitly in your scala invocation

scala -classpath /path/to/where/your/base/package/is;%CLASSPATH%
Synesso
The first one worked, the second one did not...
Sebastián Grignoli
My apologies. The flag should be -classpath, not -cp. Also the path separator is ';', not ':'. Edited. :)
Synesso
both -cp and -classpath were accepted, but either way I got this error:__'-classpath' does not accept multiple arguments__(but I was passing one argument only)
Sebastián Grignoli
@Grignoli The most probable reason for the error message is that there are spaces inside `CLASSPATH`. You could try putting double quotes around the the parameter.
Daniel
A: 

Just for documentation purposes:

Thanks to Synesso's answer I was able to achieve it with this:

@echo off
set CLASSPATH_tmp=%CLASSPATH%
set CLASSPATH=%~dp0;%CLASSPATH%
call scala Hello %*
set CLASSPATH=%CLASSPATH_tmp%
set CLASSPATH_tmp=

the -cp modifier was not accepted by scala (under Windows), so this bat file adds the application directory to the CLASSPATH environment variable temporarily.

Sebastián Grignoli