Sebastián Grignoli
Sebastián Grignoli

Reputation: 33432

How to run Scala compiled classes from a different directory

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?

Upvotes: 0

Views: 1193

Answers (2)

Synesso
Synesso

Reputation: 38978

(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%

Upvotes: 2

Sebastián Grignoli
Sebastián Grignoli

Reputation: 33432

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.

Upvotes: 0

Related Questions