import org.apache.tools.ant.Project
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
I tried to run this code 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
There is following error:
D:\test\scala\ant.scala:1: error: object apache is not a member of package org
import org.apache.tools.ant.Project
^
one error found
What is wrong?
UPDATE:
As I can see it is impossible to import any org.xxx package.
The same problem with javax.xml.xxx package.
D:\test\test2.scala:2: error: object crypto is not a member of package javax.xml
import javax.xml.crypto.Data
^
one error found
Actually I cannot import anything!
D:\test\test3.scala:3: error: object test is not a member of package com
import com.test.utils.ant.taskdefs.SqlExt
^
one error found
You haven't included the ant jar file in your classpath.
The compiler effectively builds objects representing the nested package structure. There is already a top-level package named org from the JDK (org.xml for example) but without additional jars org.apache is not there.
I found that in general when an error of type Object XXX is not a member of package YYY occurs, you should:
Check that all your files are in a package, ie. not in the top-level src/ directory
I experimented with scala.bat (uncommenting the echo of the final command line, see the line starting with echo "%_JAVACMD%" ...) and found that this should work:
java -Dscala.usejavacp=true -cp d:\Dev\scala-2.9.1.final\lib\scala-compiler.jar;d:\Dev\scala-2.9.1.final\lib\scala-library.jar scala.tools.nsc.MainGenericRunner -cp d:\Dev\apache-ant-1.8.2\lib\ant.jar D:\test\scala\ant.scala
I would check that 'D:\tools\apache-ant-1.7.0\lib\ant.jar' exist and also check that it is not corrupt (length 0 or unable to open with 'jar tf D:\tools\apache-ant-1.7.0\lib\ant.jar').
Run with scala command instead:
scala -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 D:\test\scala\ant.scala
Related
I am working through Atomic Scala 2.0, learning Scala. I am trying to create a package myself and trying to use it in my programs. This is the code for creating my package:
package com.atomicscala.pythagorean
class RightTriangle {
def hypotenuse(a:Double, b:Double):Double={
math.sqrt(a*a + b*b)
}
def area(a:Double, b:Double):Double={
a*b/2
}
}
And this is my program:
import com.atomicscala.pythagorean._
object test{
def main(args:Array[String])={
val rt = new RightTriangle
println(rt.hypotenuse(3,4))
}
}
When I try to run my program after compiling the package, I get an error like this:
error: object pythagorean is not a member of package com.atomicscala
Instead, if I name the package as just pythogorean, the code works fine. what is causing this?
Try it with the package spelled correctly:
You have:
package com.atmoicscala.pythagorean
probably should be
package com.atomicscala.pythagorean
I was able to solve it by mentioning the Classpath in the Scala command.
scala filename -classpath . (If the package is in the current working
directory)
scala filename -classpath PackageLocation
The root cause is on the "fsc", fsc will reset when classpath change.
You may reset it explicitly by execute the following
fsc -reset
then you don't need to include -classpath in your scala command.
note: I am not sure whether this is a good practice.
Another way is to run the scala command without using the fsc offline compiler
scala -nc filename
I know how to run rascal code from within eclipse and how to use the REPL, but I don't know how I can run a rascal file (or group of rascal files) as a program from the command line.
When I try the following, I get a parse error:
$ java -Xmx1G -Xss32m -jar rascal-shell-stable.jar mymodule.rsc
Version: 0.7.2.201501130937
Parse error in cwd:///mymodule.rsc from <1,11> to <1,12>
The contents of mymodule.rsc:
module mymodule
println("hello world");
What am I doing wrong?
Well, your mymodule.rsc is actually syntactically incorrect and will also give parse errors in the Eclipse IDE. Here is an improved version:
module mymodule
import IO;
value main(list[value] args) {
println("hello world");
}
Bonus: you should also add import IO; to make the println function available.
In scala it is common practice to stack package statements to allow shorter imports, but when I load a file using stacked packages into the scala ide and I attempt to use an import starting with the same organization I get a compiler error from what appears to be the presentation compiler. The code compiles fine in sbt outside of the IDE.
An example code snippet is as follows:
package com.coltfred
package util
package time
import com.github.nscala_time.time.Imports._
On the import I get the error object github is not a member of package com.coltfred.util.com.
If I move the import to a single line the error will go away, but we've used this practice frequently in our code base so changing them all to be single line package statements would be a pain.
Why is this happening and is there anything I can do to fix it?
Edit:
I used the eclipse-sbt plugin to generate the eclipse project file for this. The directory structure is what it should be and all of the dependencies are in the classpath.
Edit 2:
It turns out there was a file in the test tree of the util package (which should have been in the same package), but had a duplicate package statement at the top. I didn't check the test tree because it shouldn't affect the compilation of the main tree, but apparently I was wrong.
Not sure why the Scala IDE is not liking this, but you can force the import to start at the top level using _root_:
import _root_.com.github.nscala_time.time.Imports._
See if that avoids irritating the IDE.
This is a common annoyance that annoyed paulp into an attempt to fix it. His idea was that a dir that doesn't contribute class files shouldn't be taken as a package. If you can take util as scala.util, you should do so in preference to foo.util where that util is empty.
The util dir is the usual suspect, because who doesn't have a util dir lying around, and in particular, ./util?
apm#mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/time
apm#mara:~/tmp/coltfred$ mkdir -p com/coltfred/util/com
apm#mara:~/tmp/coltfred$ vi com/coltfred/util/time/test.scala
apm#mara:~/tmp/coltfred$ scalac com/coltfred/util/time/test.scala
./com/coltfred/util/time/test.scala:5: error: object github is not a member of package com.coltfred.util.com
import com.github.nscala_time.time._
^
one error found
apm#mara:~/tmp/coltfred$ cat com/coltfred/util/time/test.scala
package com.coltfred
package util
package time
import com.github.nscala_time.time._
class Test
apm#mara:~/tmp/coltfred$
To debug, find out where the offending package is getting loaded from.
I'm using:
% scalac -version
Scala compiler version 2.9.1 -- Copyright 2002-2011, LAMP/EPFL
on Ubuntu 12.04.
This code is saved in HelloGui.scala:
import scala.swing._
object HelloGui extends SimpleSwingApplication {
def top = new MainFrame {
title = "Hello World GUI"
contents = new Button {
text = "Click me"
}
}
}
When I try to compile this I get:
% scalac HelloGui.scala
HelloGui.scala:1: error: object swing is not a member of package scala
import scala.swing._
^
one error found
I've tried using import swing._ (it isn't clear from the tutorials which import path I need to use with this version of scala), and I get:
% scalac HelloGui.scala
HelloGui.scala:1: error: not found: object swing
import swing._
^
one error found
When I look in /usr/share/java, I see scala-swing-2.9.1.jar and scala-swing.jar as a symlink to it, so it seems like the libraries are present?
Am I missing a compiler flag or is there another package I need to install?
The compiler needs to have the path to the swing jar passed explicitly. This works:
% scalac -classpath /usr/share/java/scala-swing.jar HelloGui.scala
I get the following warning whenever I start my Scala application:
WARN - imported `SVNProperties' is permanently hidden by definition of object SVNProperties in package core, at line 4 of app/core/SVNResource.scala
What could this mean?
You probably have code that looks something like this:
object Hidden {
import scala.collection.immutable
object immutable { def x = 7 }
}
except in a less obvious way. You're importing something--in my example, the package immutable--and then you go and define something else with the same name that prevents you from using what you imported.
In particular, it looks like you tried to import SVNProperties into SVNResource.scala, except that SVNResource.scala defines its own SVNProperties which hides the import.
I encountered this warning after moving some classes from one package to another. I guess there was some conflict between the new location and binaries from the old location. In my case this helped:
sbt clean
I got this warning when my class was importing classes in the same package.
Once I removed the unnecessary import, the warnings were removed.
This happened to me after moving a class from one package to another, like in astasiak's case. I ran sbt clean with no luck. For the life of me, I couldn't find the class in the old location.
However, I had other errors preventing me from building. When I fixed those, this error disappeared. My guess is that until you can build cleanly, sbt still thinks you have the class is in the old package, and includes this error with any other build errors that are preventing you from building.
My advice? Check for other compilation errors and fix those -- you might be erroneously getting this error due to sbt having an outdated view of your package structure since its last successful build.
Just to further expand on a comment posted by Nick, as this was the case for me:
Another common cause is that SVNProperties is in the same package and so is already in scope. Trying to import it explicitly results in this warning.
More concretely, you may have:
package app.core
// No need to import the following - it is already visible as
// you are in the same package
import app.core.SVNProperties
object SVNResource {
Use instead:
package app.core
object SVNResource {
(Opinion) As a matter of coding style you may want to camel case your variables differently like for eg. SvnProperties, SvnResource. It reads easier for most people, see also this question.
I had a main class with name Server and I was creating a jetty server in the main class in the following way.
import org.eclipse.jetty.server.Server
var server:Server=new Server()
I got the below warn on running sbt run
> [warn] /home/xxx/xxx/xxx/src/main/scala/com/xxx/xxx/main/Server.scala:3:
> imported `Server' is permanently hidden by definition of object Server in package main
[warn] import org.eclipse.jetty.server.Server
[warn] ^
[warn] one warning found
I renamed my main class and the warning disappeared.
If you are using Scala Eclipse IDE you can do :
Project > Clean...
After that all the warning will be removed.
Also, make sure the name of the package you import =/= object name.
I got this by having circular dependencies. Both my classes were using each other on accident.
If the warning comes from importing a class with the same name, you can always use import renaming.
package domain
case class Company (...
package models
import domain.{Company => _Company}
object Company extends SkinnyCRUDMapper[_Company] {
Issue is related to dependency conflict, When you have same class in multiple Jars compiler found one of class is hidden and gives an error.
check for your Jar version is same across project or try to change name/package for one of conflicted class