问题描述
我有以下几个罐子/Users/joe/.scala/lib下:
I have the following jars under /Users/joe/.scala/lib:
commons-codec-1.4.jar
httpclient-4.1.1.jar
httpcore-4.1.jar
commons-logging-1.1.1.jar
httpclient-cache-4.1.1.jar
httpmime-4.1.1.jar
下面是我test.sh Scala编写的。
Below is my test.sh written in scala.
#!/bin/sh -v
L=`cd /Users/joe/.scala/lib;pwd`
cp=`echo $L/*.jar|sed 's/ /:/g'`
echo $cp
exec scala -classpath $cp $0 $@
!#
println(new org.apache.commons.httpclient.HttpClient())
下面是我得到的错误:
$ ./test.sh
#!/bin/sh -v
L=`cd /Users/joe/.scala/lib;pwd`
cd /Users/joe/.scala/lib;pwd
cp=`echo $L/*.jar|sed 's/ /:/g'`
echo $L/*.jar|sed 's/ /:/g'
echo $cp
/Users/joe/.scala/lib/commons-codec-1.4.jar:/Users/joe/.scala/lib/commons-logging-1.1.1.jar:/Users/joe/.scala/lib/httpclient-4.1.1.jar:/Users/joe/.scala/lib/httpclient-cache-4.1.1.jar:/Users/joe/.scala/lib/httpcore-4.1.jar:/Users/joe/.scala/lib/httpmime-4.1.1.jar
exec scala -classpath $cp $0 $@
/Users/joe/Desktop/scala/./test.sh:7: error: object httpclient is not a member of package org.apache.commons
println(new org.apache.commons.httpclient.HttpClient())
^
one error found
然而,简单的人没有任何类路径依赖关系的工作,虽然:
hello.sh
However, simple ones without any classpath dependencies work though:hello.sh
#!/bin/sh
exec scala "$0" "$@"
!#
println(new java.util.Date())
任何想法,我在第一个例子做错了什么?另外,什么是使用Scala脚本时设置类路径依赖关系的最佳方式?
Any idea what I am doing wrong in the first example? Alternatively, what is the best way to set classpath dependencies when working with scala scripts?
推荐答案
我觉得跟4.1.1类是org.apache.http.client.HttpClient代替org.apache.commons.httpclient,而且它的接口。所以,你可能想
I think with 4.1.1 the class is org.apache.http.client.HttpClient instead of org.apache.commons.httpclient, and it's an interface. So you might want
new org.apache.http.client.DefaultHttpClient()
而不是
new org.apache.commons.httpclient.HttpClient()
这很可能已经在早期版本的不同。
It might well have been different in an earlier version.
这篇关于运行斯卡拉脚本外部依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!