- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Object XXX is not a member of package YYY
Tue, 2011-11-22, 18:36
I tried to write first lines in Scala and failed.
import org.apache.tools.ant.Project
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
I've got "object apache is not a member of package org" error when I
tried to run this script using following command:
java -cp D:\tools\apache-ant-1.7.0\lib\ant.jar;D:\tools
\scala-2.9.1.final\lib\scala-compiler.jar;D:\tools\scala-2.9.1.final
\lib\scala-library.jar -Dscala.usejavacp=true
scala.tools.nsc.MainGenericRunner D:\test\scala\ant.scala
The same for "import javax.xml.crypto.Data" - "object crypto is not a
member of package javax.xml".
I asked about this problem here -
http://stackoverflow.com/questions/8211619/object-apache-is-not-a-member...
But did not get the answer yet.
Wed, 2011-11-23, 15:37
#2
Re: Object XXX is not a member of package YYY
Hi Vladimir,
this works for me:
java -cp
D:\tools\scala-2.9.1.final\lib\scala-compiler.jar;D:\tools\scala-2.9.1.final\lib\scala-library.jar
-Dscala.usejavacp=true scala.tools.nsc.MainGenericRunner -cp D:\tools\apache-ant-1.7.0\lib\ant.jar
D:\test\scala\ant.scala
Don't know whether it's a bug or expected behavior, that the MainGenericRunner doesn't use the java
classpath in your original example.
--
Eugen
Wed, 2011-11-23, 15:57
#3
creating instances from methods relying on abstract classes
Hi all,
I wonder whether the following is a well known pattern or not so smart as I think it was during creating it (and by the way due to the for and back linking between classes and companions I was surprised that it is firstly compiling and secondly working ;-). Is there a more transparent way of doing it?
Thanks and Regards,
Volker.
+------------
I have a lot of classes coming from one abstract one. Because all of them need a popper companion with certain methods I added also a blueprint for the companion:
trait PetCompanion[A] {
def apply(name: String): A
def unapply(a: Pet[A]) = Some(a.kind)
}
abstract class Pet[A](val kind: String, val name: String) {
def companion: PetCompanion[A]
def sound: String
def bear(name: String) = companion("son of " + name)
override def toString = name + ": " + sound
}
Now I can create Dog
object Dog extends PetCompanion[Dog] {def apply(name: String) = new Dog(name)}
class Dog(name: String) extends Pet[Dog]("Dog", name) {
override val companion = Dog
override val sound = "woof"
}
and Cat classes/companions
object Cat extends PetCompanion[Cat] {def apply(name: String) = new Cat(name)}
class Cat(name: String) extends Pet[Cat]("Cat", name) {
override val companion = Cat
override val sound = "miaow"
}
The benefit is that I can write functions returning instances of pets depending on the input like:
def create[A <: Pet[A]](companion: PetCompanion[A])(name: String) = companion(name)
Or in action:
scala> create(Dog)("Spike")
res0: Dog = Spike: woof
scala> create(Cat)("Tom")
res1: Cat = Tom: miaow
Wed, 2011-11-23, 17:07
#4
Re: Object XXX is not a member of package YYY
On Wed, Nov 23, 2011 at 4:27 PM, Eugen Labun <labun@gmx.net> wrote:
Thank you very much!It helps.
Does anyone can explain this feature?
this works for me:
java -cp D:\tools\scala-2.9.1.final\lib\scala-compiler.jar;D:\tools\scala-2.9.1.final\lib\scala-library.jar -Dscala.usejavacp=true scala.tools.nsc.MainGenericRunner -cp D:\tools\apache-ant-1.7.0\lib\ant.jar D:\test\scala\ant.scala
Thank you very much!It helps.
Does anyone can explain this feature?
Wed, 2011-11-23, 17:17
#5
Re: Object XXX is not a member of package YYY
Scala has a standard library that includes java's standard library. Ant is not part of this. If you want to use ant classes, you have to tell the compiler/runtime where ant is.
On Wed, Nov 23, 2011 at 10:53 AM, Vladimir Bezugliy <vladimirbezugliy@gmail.com> wrote:
On Wed, Nov 23, 2011 at 10:53 AM, Vladimir Bezugliy <vladimirbezugliy@gmail.com> wrote:
On Wed, Nov 23, 2011 at 4:27 PM, Eugen Labun <labun@gmx.net> wrote:this works for me:
java -cp D:\tools\scala-2.9.1.final\lib\scala-compiler.jar;D:\tools\scala-2.9.1.final\lib\scala-library.jar -Dscala.usejavacp=true scala.tools.nsc.MainGenericRunner -cp D:\tools\apache-ant-1.7.0\lib\ant.jar D:\test\scala\ant.scala
Thank you very much!It helps.
Does anyone can explain this feature?
Wed, 2011-11-23, 17:37
#6
Re: Object XXX is not a member of package YYY
But ant.jar *was* in the java classpath, and the environment variable "scala.usejavacp" was set to
true (see the original post). So, why Scala's MainGenericRunner couldn't use classes from the java
classpath and needed specifying its own classpath additionally to the java classpath? Or am I
missing something?
On 2011-11-23 16:57, Josh Suereth wrote:
> Scala has a standard library that includes java's standard library. Ant is not part of this. If
> you want to use ant classes, you have to tell the compiler/runtime where ant is.
Though I'm not a script user, I'd try to run bin\scala.bat with the
syntax described at http://www.scala-lang.org/docu/files/tools/scala.html.
Peter