This page is no longer maintained — Please continue to the home page at www.scala-lang.org

classpath for #! script

8 replies
Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Can someone tell me how to specify the classpath or sourcepath for a #! script (on Linux)? Thanks.

--Russ P.

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: classpath for #! script
You can do one of these, assuming you use bash:

1)

export CLASSPATH=whatever
./thescript

2)

CLASSPATH=whatever ./thescript

3)

Alter the script so that wherever it passes a classpath into java or scala, it does so via their -classpath switch.


2009/2/6 Russ Paielli <russ.paielli@gmail.com>
Can someone tell me how to specify the classpath or sourcepath for a #! script (on Linux)? Thanks.

--Russ P.


Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: classpath for #! script
Thanks for the reply, but I must be missing something, because your methods are not working for me. (As I said before, I've never used Java.)

Here's the situation. I wrote a few .scala files, including a couple of #! scripts that I am using as test drivers that import some of the other files. When the files are all in one directory, I can run the scripts by just typing their names. However, I decided to create a subdirectory called "test" to keep the test scripts separate from the other files. When I go to the test directory and type the name of a script, the imports no longer work.

I tried your suggestion of typing

> CLASSPATH=.. myscript.scala

but that didn't work. I also tried the absolute path instead of "..", but that didn't work either.

I also set the CLASSPATH in my .bashrc file and sourced it, but that didn't help either.

As for your alternative 3, I do not understand what it means.

Sorry for being so dense, but I cannot find answers to these basic questions online or in the Odersky book. Any additional help will certainly be appreciated.


On Fri, Feb 6, 2009 at 1:28 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
You can do one of these, assuming you use bash:

1)

export CLASSPATH=whatever
./thescript

2)

CLASSPATH=whatever ./thescript

3)

Alter the script so that wherever it passes a classpath into java or scala, it does so via their -classpath switch.


2009/2/6 Russ Paielli <russ.paielli@gmail.com>
Can someone tell me how to specify the classpath or sourcepath for a #! script (on Linux)? Thanks.

--Russ P.





--
http://RussP.us
Jens Alfke
Joined: 2009-01-30,
User offline. Last seen 42 years 45 weeks ago.
Re: classpath for #! script

Try starting the script with

#! scala -classpath /path/to/script/dir/

It's better to use an absolute path for this than '..', because it
doesn't make assumptions about what the current directory is when you
invoke the script. (The really bulletproof way is to get the directory
by using a shell script to get the parent directory of $0, but you
can't fit that into a #! line.)

—Jens

Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: classpath for #! script
Thanks, but I can't get that to work either. My script starts with this:

#!/bin/sh
exec scala "$0" "$@"
!#

Where should I put the line you suggested?


On Fri, Feb 6, 2009 at 12:49 PM, Jens Alfke <jens@mooseyard.com> wrote:
Try starting the script with

#! scala -classpath /path/to/script/dir/

It's better to use an absolute path for this than '..', because it doesn't make assumptions about what the current directory is when you invoke the script. (The really bulletproof way is to get the directory by using a shell script to get the parent directory of $0, but you can't fit that into a #! line.)

—Jens



--
http://RussP.us
Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: classpath for #! script
#!/bin/sh
exec scala -classpath /path/to/classes "$0" "$@"
!#

On Fri, Feb 6, 2009 at 1:03 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
Thanks, but I can't get that to work either. My script starts with this:

#!/bin/sh
exec scala "$0" "$@"
!#

Where should I put the line you suggested?


On Fri, Feb 6, 2009 at 12:49 PM, Jens Alfke <jens@mooseyard.com> wrote:
Try starting the script with

#! scala -classpath /path/to/script/dir/

It's better to use an absolute path for this than '..', because it doesn't make assumptions about what the current directory is when you invoke the script. (The really bulletproof way is to get the directory by using a shell script to get the parent directory of $0, but you can't fit that into a #! line.)

—Jens



--
http://RussP.us

Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: classpath for #! script
Actually I needed -sourcepath rather than -classpath, but that did the job. Thanks.

On Fri, Feb 6, 2009 at 1:09 PM, Jorge Ortiz <jorge.ortiz@gmail.com> wrote:
#!/bin/sh
exec scala -classpath /path/to/classes "$0" "$@"
!#

On Fri, Feb 6, 2009 at 1:03 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
Thanks, but I can't get that to work either. My script starts with this:

#!/bin/sh
exec scala "$0" "$@"
!#

Where should I put the line you suggested?


On Fri, Feb 6, 2009 at 12:49 PM, Jens Alfke <jens@mooseyard.com> wrote:
Try starting the script with

#! scala -classpath /path/to/script/dir/

It's better to use an absolute path for this than '..', because it doesn't make assumptions about what the current directory is when you invoke the script. (The really bulletproof way is to get the directory by using a shell script to get the parent directory of $0, but you can't fit that into a #! line.)

—Jens



--
http://RussP.us




--
http://RussP.us
Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: classpath for #! script
I apologise for answering the question you asked instead of the one you meant to ask. :)

2009/2/6 Russ Paielli <russ.paielli@gmail.com>
Actually I needed -sourcepath rather than -classpath, but that did the job. Thanks.

On Fri, Feb 6, 2009 at 1:09 PM, Jorge Ortiz <jorge.ortiz@gmail.com> wrote:
#!/bin/sh
exec scala -classpath /path/to/classes "$0" "$@"
!#

On Fri, Feb 6, 2009 at 1:03 PM, Russ Paielli <russ.paielli@gmail.com> wrote:
Thanks, but I can't get that to work either. My script starts with this:

#!/bin/sh
exec scala "$0" "$@"
!#

Where should I put the line you suggested?


On Fri, Feb 6, 2009 at 12:49 PM, Jens Alfke <jens@mooseyard.com> wrote:
Try starting the script with

#! scala -classpath /path/to/script/dir/

It's better to use an absolute path for this than '..', because it doesn't make assumptions about what the current directory is when you invoke the script. (The really bulletproof way is to get the directory by using a shell script to get the parent directory of $0, but you can't fit that into a #! line.)

—Jens



--
http://RussP.us




--
http://RussP.us

Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: classpath for #! script
For those of you interested, I had written a small Ruby script to kickstart a Scala script with a decent classpath which I cleaned up a little today for the occasion.

It's called "scalarun" and it allows you to write scripts such as,

#!/usr/bin/env scalarun
# REQUIRE files("lib/*.jar")
# REQUIRE artifact("org.slf4j:slf4j-api:jar:1.4.3")
# REQUIRE artifact("org.slf4j:slf4j-log4j12:jar:1.4.3")
# REQUIRE artifact("log4j:log4j:jar:1.2.15")
!#

import org.slf4j.LoggerFactory
val logger = LoggerFactory.getLogger("test")
logger.info("Hello, world!")

I'm attaching here in the hope that others might find it useful.   The script is self-documenting and works only on *NIX systems I presume.

cheers,
alex

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland