I posted this directly to David B. but I thought that this could be of
interest for the rest of you too.
I just created some JUnit tests (well, some specs which can be
executed as JUnit tests). Those tests are using the Scala Interpreter
class and the code runs ok if I run it from the command line (as
regular specs Specifications).
But when I run "mvn test" I get exceptions like:
object scala not found.
at scala.tools.nsc.symtab.Definitions$definitions$.getModuleOrClass
(Definitions.scala:355)
at scala.tools.nsc.symtab.Definitions$definitions$.getModule
(Definitions.scala:326)
at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackage
(Definitions.scala:34)
at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackageClass
(Definitions.scala:35)
at scala.tools.nsc.symtab.Definitions$definitions$.init
(Definitions.scala:663)
at scala.tools.nsc.Global$Run.<init>(Global.scala:476)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:477)
at org.specs.util.ScalaInterpreter$$anonfun$interpret$1.apply
(ScalaInterpreter.scala:28)
I tried to add this to my pom.xml in the surefire plugin
configuration:
On Wed, Mar 18, 2009 at 10:37, etorreborre <etorrebo...@gmail.com> wrote:
> Hi,
> I posted this directly to David B. but I thought that this could be of > interest for the rest of you too.
> I just created some JUnit tests (well, some specs which can be > executed as JUnit tests). Those tests are using the Scala Interpreter > class and the code runs ok if I run it from the command line (as > regular specs Specifications).
> But when I run "mvn test" I get exceptions like:
> object scala not found. > at scala.tools.nsc.symtab.Definitions$definitions$.getModuleOrClass > (Definitions.scala:355) > at scala.tools.nsc.symtab.Definitions$definitions$.getModule > (Definitions.scala:326) > at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackage > (Definitions.scala:34) > at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackageClass > (Definitions.scala:35) > at scala.tools.nsc.symtab.Definitions$definitions$.init > (Definitions.scala:663) > at scala.tools.nsc.Global$Run.<init>(Global.scala:476) > at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:477) > at org.specs.util.ScalaInterpreter$$anonfun$interpret$1.apply > (ScalaInterpreter.scala:28)
> I tried to add this to my pom.xml in the surefire plugin > configuration:
--------------------------------------------------------------------------- ----
Test set: org.specs.util.interpreterSpec
--------------------------------------------------------------------------- ----
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.016
sec <<< FAILURE!
initializationError0(org.specs.util.interpreterSpec) Time elapsed: 0
sec <<< ERROR!
scala.tools.nsc.FatalError: object scala not found.
at scala.tools.nsc.symtab.Definitions$definitions$.getModuleOrClass
(Definitions.scala:355)
at scala.tools.nsc.symtab.Definitions$definitions$.getModule
(Definitions.scala:326)
at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackage
(Definitions.scala:34)
at scala.tools.nsc.symtab.Definitions$definitions$.ScalaPackageClass
(Definitions.scala:35)
at scala.tools.nsc.symtab.Definitions$definitions$.init
(Definitions.scala:663)
at scala.tools.nsc.Global$Run.<init>(Global.scala:476)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:477)
at org.specs.util.interpreterSpec.<init>(interpreterSpec.scala:11)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance
(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance
(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at java.lang.Class.newInstance0(Class.java:350)
at java.lang.Class.newInstance(Class.java:303)
at org.specs.runner.JUnitSuiteRunner.<init>(JUnitSuiteRunner.scala:
20)
at sun.reflect.GeneratedConstructorAccessor3.newInstance(Unknown
Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance
(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at org.junit.internal.requests.ClassRequest.buildRunner
(ClassRequest.java:33)
at org.junit.internal.requests.ClassRequest.getRunner
(ClassRequest.java:28)
at org.apache.maven.surefire.junit4.JUnit4TestSet.<init>
(JUnit4TestSet.java:45)
at
org.apache.maven.surefire.junit4.JUnit4DirectoryTestSuite.createTestSet
(JUnit4DirectoryTestSuite.java:56)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.locateTestSets
(AbstractDirectoryTestSuite.java:96)
at org.apache.maven.surefire.Surefire.createSuiteFromDefinition
(Surefire.java:209)
at org.apache.maven.surefire.Surefire.run(Surefire.java:156)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess
(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main
(SurefireBooter.java:997)
When I wrote the scala macro interpreter for Jedit I need to provide a custom classpath to the interpreter to allow it to find scala-library and scala-compiler : import _root_.scala.tools.nsc.{Interpreter, Settings} ...
val settings = new Settings(null) settings.classpath.value = List( settings.classpath.value, new File(dir, "scala-library.jar").getAbsolutePath, new File(dir, "scala-compiler.jar").getAbsolutePath ).mkString(File.pathSeparator)
val out = new StringWriter() val interp = new Interpreter(settings, new PrintWriter(out)){ override def parentClassLoader = classOf[MacroHandler4Scala].getClassLoader } try { interp.beQuietDuring({ interp.bind("view", classOf[View].getName, view) interp.bind("buffer", classOf[Buffer].getName, view.getBuffer) interp.bind("editPane", classOf[EditPane].getName, view.getEditPane) interp.bind("textArea", classOf[JEditTextArea].getName, view.getTextArea) }) Source.fromFile(macro.getPath).getLines.foreach(interp.interpret(_)) val output = out.toString if (output.length > 0) { new TextAreaDialog(view, "scala-eval", "output", UIManager.getIcon("OptionPane.errorIcon"), output) }
I think you have the same issue. And I suppose it work as standalone because in this case scala is part of the system classloader.
> When I wrote the scala macro interpreter for Jedit I need to provide a
> custom classpath to the interpreter to allow it to find scala-library
> and scala-compiler :
> import _root_.scala.tools.nsc.{Interpreter, Settings}
> ...
> val settings = new Settings(null)
> settings.classpath.value = List(
> settings.classpath.value,
> new File(dir, "scala-library.jar").getAbsolutePath,
> new File(dir, "scala-compiler.jar").getAbsolutePath
> ).mkString(File.pathSeparator)
My question here is: how do I get those values when I'm running this
code inside JUnit?
In the code above where do you get the "dir" variable from and how
would that be possible to write code which could work using both JUnit
and scala (on the command line)?
First try to keep the setting.classpath empty and defile the classloader of the interpreter to the current one something like
val cl = Thread.currentThread.getContextClassLoader val interp = new Interpreter(settings, new PrintWriter(out)){ override def parentClassLoader = cl }
If it not work I'll search in my notes (older how you could retreive scala-library.jar location from classpath)
On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> wrote:
> Hi David,
>> When I wrote the scala macro interpreter for Jedit I need to provide a >> custom classpath to the interpreter to allow it to find scala-library >> and scala-compiler : >> import _root_.scala.tools.nsc.{Interpreter, Settings} >> ...
>> val settings = new Settings(null) >> settings.classpath.value = List( >> settings.classpath.value, >> new File(dir, "scala-library.jar").getAbsolutePath, >> new File(dir, "scala-compiler.jar").getAbsolutePath >> ).mkString(File.pathSeparator)
> My question here is: how do I get those values when I'm running this > code inside JUnit?
> In the code above where do you get the "dir" variable from and how > would that be possible to write code which could work using both JUnit > and scala (on the command line)?
> First try to keep the setting.classpath empty and defile the
> classloader of the interpreter to the current one something like
> val cl = Thread.currentThread.getContextClassLoader
> val interp = new Interpreter(settings, new PrintWriter(out)){
> override def parentClassLoader = cl
> }
> If it not work I'll search in my notes (older how you could retreive
> scala-library.jar location from classpath)
> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> wrote:
> > Hi David,
> >> When I wrote the scala macro interpreter for Jedit I need to provide a
> >> custom classpath to the interpreter to allow it to find scala-library
> >> and scala-compiler :
> >> import _root_.scala.tools.nsc.{Interpreter, Settings}
> >> ...
> >> val settings = new Settings(null)
> >> settings.classpath.value = List(
> >> settings.classpath.value,
> >> new File(dir, "scala-library.jar").getAbsolutePath,
> >> new File(dir, "scala-compiler.jar").getAbsolutePath
> >> ).mkString(File.pathSeparator)
> > My question here is: how do I get those values when I'm running this
> > code inside JUnit?
> > In the code above where do you get the "dir" variable from and how
> > would that be possible to write code which could work using both JUnit
> > and scala (on the command line)?
a code fragment that you could use to find a jar in classpath :
var cl = classLoader.getOrElse(Thread.currentThread.getContextClassLoader()) while (cl != null) { if (cl.isInstanceOf[URLClassLoader]) { val ucl = cl.asInstanceOf[URLClassLoader] ucl.getURLs().foreach {url => if (url.getProtocol().equals("file")) { val file = new File(URLDecoder.decode(url.getPath(), "UTF-8")).getCanonicalFile() ...
On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com> wrote:
> Hmmm, this doesn't work. I think I need some classpath surgery to > retrieve the scala-library from the classpath!
> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote: >> First try to keep the setting.classpath empty and defile the >> classloader of the interpreter to the current one something like
>> val cl = Thread.currentThread.getContextClassLoader >> val interp = new Interpreter(settings, new PrintWriter(out)){ >> override def parentClassLoader = cl >> }
>> If it not work I'll search in my notes (older how you could retreive >> scala-library.jar location from classpath)
>> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> wrote:
>> > Hi David,
>> >> When I wrote the scala macro interpreter for Jedit I need to provide a >> >> custom classpath to the interpreter to allow it to find scala-library >> >> and scala-compiler : >> >> import _root_.scala.tools.nsc.{Interpreter, Settings} >> >> ...
>> >> val settings = new Settings(null) >> >> settings.classpath.value = List( >> >> settings.classpath.value, >> >> new File(dir, "scala-library.jar").getAbsolutePath, >> >> new File(dir, "scala-compiler.jar").getAbsolutePath >> >> ).mkString(File.pathSeparator)
>> > My question here is: how do I get those values when I'm running this >> > code inside JUnit?
>> > In the code above where do you get the "dir" variable from and how >> > would that be possible to write code which could work using both JUnit >> > and scala (on the command line)?
> a code fragment that you could use to find a jar in classpath :
> var cl = classLoader.getOrElse(Thread.currentThread.getContextClassLoader())
> while (cl != null) {
> if (cl.isInstanceOf[URLClassLoader]) {
> val ucl = cl.asInstanceOf[URLClassLoader]
> ucl.getURLs().foreach {url =>
> if (url.getProtocol().equals("file")) {
> val file = new File(URLDecoder.decode(url.getPath(),
> "UTF-8")).getCanonicalFile()
> ...
> On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com> wrote:
> > Hmmm, this doesn't work. I think I need some classpath surgery to
> > retrieve the scala-library from the classpath!
> > On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote:
> >> First try to keep the setting.classpath empty and defile the
> >> classloader of the interpreter to the current one something like
> >> val cl = Thread.currentThread.getContextClassLoader
> >> val interp = new Interpreter(settings, new PrintWriter(out)){
> >> override def parentClassLoader = cl
> >> }
> >> If it not work I'll search in my notes (older how you could retreive
> >> scala-library.jar location from classpath)
> >> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> wrote:
> >> > Hi David,
> >> >> When I wrote the scala macro interpreter for Jedit I need to provide a
> >> >> custom classpath to the interpreter to allow it to find scala-library
> >> >> and scala-compiler :
> >> >> import _root_.scala.tools.nsc.{Interpreter, Settings}
> >> >> ...
> >> >> val settings = new Settings(null)
> >> >> settings.classpath.value = List(
> >> >> settings.classpath.value,
> >> >> new File(dir, "scala-library.jar").getAbsolutePath,
> >> >> new File(dir, "scala-compiler.jar").getAbsolutePath
> >> >> ).mkString(File.pathSeparator)
> >> > My question here is: how do I get those values when I'm running this
> >> > code inside JUnit?
> >> > In the code above where do you get the "dir" variable from and how
> >> > would that be possible to write code which could work using both JUnit
> >> > and scala (on the command line)?
Yes, when embedding the interpreter you either need to pass in the classpath, or override one of the classloader methods(although I don't think the later is supported)
Also, feel free to upgrade to maven-scala-plugin 2.10.1
Sent from my iPhone
On Mar 19, 2009, at 2:56 AM, David Bernard <david.bernard.
> When I wrote the scala macro interpreter for Jedit I need to provide a > custom classpath to the interpreter to allow it to find scala-library > and scala-compiler : > import _root_.scala.tools.nsc.{Interpreter, Settings} > ...
> val settings = new Settings(null) > settings.classpath.value = List( > settings.classpath.value, > new File(dir, "scala-library.jar").getAbsolutePath, > new File(dir, "scala-compiler.jar").getAbsolutePath > ).mkString(File.pathSeparator)
> val out = new StringWriter() > val interp = new Interpreter(settings, new PrintWriter(out)){ > override def parentClassLoader = > classOf[MacroHandler4Scala].getClassLoader > } > try { > interp.beQuietDuring({ > interp.bind("view", classOf[View].getName, view) > interp.bind("buffer", classOf[Buffer].getName, view.getBuffer) > interp.bind("editPane", classOf[EditPane].getName, > view.getEditPane) > interp.bind("textArea", classOf[JEditTextArea].getName, > view.getTextArea) > })
> Source.fromFile(macro.getPath).getLines.foreach(interp.interpret(_)) > val output = out.toString > if (output.length > 0) { > new TextAreaDialog(view, "scala-eval", "output", > UIManager.getIcon("OptionPane.errorIcon"), output) > }
> I think you have the same issue. And I suppose it work as standalone > because in this case scala is part of the system classloader.
> On Thu, Mar 19, 2009 at 07:46, etorreborre <etorrebo...@gmail.com> > wrote:
>> Here is a full stack trace:
>> --- >> --- >> --- >> --- >> ------------------------------------------------------------------- >> Test set: org.specs.util.interpreterSpec >> --- >> --- >> --- >> --- >> ------------------------------------------------------------------- >> Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.016 >> sec <<< FAILURE! >> initializationError0(org.specs.util.interpreterSpec) Time elapsed: 0 >> sec <<< ERROR! >> scala.tools.nsc.FatalError: object scala not found. >> at scala.tools.nsc.symtab.Definitions$definitions >> $.getModuleOrClass >> (Definitions.scala:355) >> at scala.tools.nsc.symtab.Definitions$definitions$.getModule >> (Definitions.scala:326) >> at scala.tools.nsc.symtab.Definitions$definitions >> $.ScalaPackage >> (Definitions.scala:34) >> at scala.tools.nsc.symtab.Definitions$definitions >> $.ScalaPackageClass >> (Definitions.scala:35) >> at scala.tools.nsc.symtab.Definitions$definitions$.init >> (Definitions.scala:663) >> at scala.tools.nsc.Global$Run.<init>(Global.scala:476) >> at scala.tools.nsc.Interpreter.interpret(Interpreter.scala: >> 477) >> at >> org.specs.util.interpreterSpec.<init>(interpreterSpec.scala:11) >> at >> sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native >> Method) >> at sun.reflect.NativeConstructorAccessorImpl.newInstance >> (NativeConstructorAccessorImpl.java:39) >> at sun.reflect.DelegatingConstructorAccessorImpl.newInstance >> (DelegatingConstructorAccessorImpl.java:27) >> at >> java.lang.reflect.Constructor.newInstance(Constructor.java:494) >> at java.lang.Class.newInstance0(Class.java:350) >> at java.lang.Class.newInstance(Class.java:303) >> at >> org.specs.runner.JUnitSuiteRunner.<init>(JUnitSuiteRunner.scala: >> 20) >> at >> sun.reflect.GeneratedConstructorAccessor3.newInstance(Unknown >> Source) >> at sun.reflect.DelegatingConstructorAccessorImpl.newInstance >> (DelegatingConstructorAccessorImpl.java:27) >> at >> java.lang.reflect.Constructor.newInstance(Constructor.java:494) >> at org.junit.internal.requests.ClassRequest.buildRunner >> (ClassRequest.java:33) >> at org.junit.internal.requests.ClassRequest.getRunner >> (ClassRequest.java:28) >> at org.apache.maven.surefire.junit4.JUnit4TestSet.<init> >> (JUnit4TestSet.java:45) >> at >> org. >> apache.maven.surefire.junit4.JUnit4DirectoryTestSuite.createTestSet >> (JUnit4DirectoryTestSuite.java:56) >> at >> org. >> apache.maven.surefire.suite.AbstractDirectoryTestSuite.locateTestSets >> (AbstractDirectoryTestSuite.java:96) >> at >> org.apache.maven.surefire.Surefire.createSuiteFromDefinition >> (Surefire.java:209) >> at org.apache.maven.surefire.Surefire.run(Surefire.java:156) >> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) >> at sun.reflect.NativeMethodAccessorImpl.invoke >> (NativeMethodAccessorImpl.java:39) >> at sun.reflect.DelegatingMethodAccessorImpl.invoke >> (DelegatingMethodAccessorImpl.java:25) >> at java.lang.reflect.Method.invoke(Method.java:585) >> at >> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess >> (SurefireBooter.java:338) >> at org.apache.maven.surefire.booter.SurefireBooter.main >> (SurefireBooter.java:997)
> a code fragment that you could use to find a jar in classpath :
> var cl = > classLoader.getOrElse(Thread.currentThread.getContextClassLoader()) > while (cl != null) { > if (cl.isInstanceOf[URLClassLoader]) { > val ucl = cl.asInstanceOf[URLClassLoader] > ucl.getURLs().foreach {url => > if (url.getProtocol().equals("file")) { > val file = new File(URLDecoder.decode(url.getPath(), > "UTF-8")).getCanonicalFile() > ...
> On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com> wrote:
>> Hmmm, this doesn't work. I think I need some classpath surgery to >> retrieve the scala-library from the classpath!
>> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote: >>> First try to keep the setting.classpath empty and defile the >>> classloader of the interpreter to the current one something like
>>> val cl = Thread.currentThread.getContextClassLoader >>> val interp = new Interpreter(settings, new PrintWriter(out)){ >>> override def parentClassLoader = cl >>> }
>>> If it not work I'll search in my notes (older how you could retreive >>> scala-library.jar location from classpath)
>>> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> wrote:
>>> > Hi David,
>>> >> When I wrote the scala macro interpreter for Jedit I need to provide a >>> >> custom classpath to the interpreter to allow it to find scala-library >>> >> and scala-compiler : >>> >> import _root_.scala.tools.nsc.{Interpreter, Settings} >>> >> ...
>>> >> val settings = new Settings(null) >>> >> settings.classpath.value = List( >>> >> settings.classpath.value, >>> >> new File(dir, "scala-library.jar").getAbsolutePath, >>> >> new File(dir, "scala-compiler.jar").getAbsolutePath >>> >> ).mkString(File.pathSeparator)
>>> > My question here is: how do I get those values when I'm running this >>> > code inside JUnit?
>>> > In the code above where do you get the "dir" variable from and how >>> > would that be possible to write code which could work using both JUnit >>> > and scala (on the command line)?
> If I understand the problem correctly, the following should also work:
> new > File( > classOf[ > ScalaObject].getProtectionDomain.getCodeSource.getLocation.toURI)
> It gets the jar file containing ScalaObject.
> -Mark
> On 3/19/09, David Bernard <david.bernard...@gmail.com> wrote:
>> a code fragment that you could use to find a jar in classpath :
>> var cl = >> classLoader.getOrElse(Thread.currentThread.getContextClassLoader()) >> while (cl != null) { >> if (cl.isInstanceOf[URLClassLoader]) { >> val ucl = cl.asInstanceOf[URLClassLoader] >> ucl.getURLs().foreach {url => >> if (url.getProtocol().equals("file")) { >> val file = new File(URLDecoder.decode(url.getPath(), >> "UTF-8")).getCanonicalFile() >> ...
>> On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com> >> wrote:
>>> Hmmm, this doesn't work. I think I need some classpath surgery to >>> retrieve the scala-library from the classpath!
>>> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> >>> wrote: >>>> First try to keep the setting.classpath empty and defile the >>>> classloader of the interpreter to the current one something like
>>>> val cl = Thread.currentThread.getContextClassLoader >>>> val interp = new Interpreter(settings, new PrintWriter(out)){ >>>> override def parentClassLoader = cl >>>> }
>>>> If it not work I'll search in my notes (older how you could >>>> retreive >>>> scala-library.jar location from classpath)
>>>> On Thu, Mar 19, 2009 at 09:52, etorreborre >>>> <etorrebo...@gmail.com> wrote:
>>>>> Hi David,
>>>>>> When I wrote the scala macro interpreter for Jedit I need to >>>>>> provide a >>>>>> custom classpath to the interpreter to allow it to find scala- >>>>>> library >>>>>> and scala-compiler : >>>>>> import _root_.scala.tools.nsc.{Interpreter, Settings} >>>>>> ...
>>>>>> val settings = new Settings(null) >>>>>> settings.classpath.value = List( >>>>>> settings.classpath.value, >>>>>> new File(dir, "scala-library.jar").getAbsolutePath, >>>>>> new File(dir, "scala-compiler.jar").getAbsolutePath >>>>>> ).mkString(File.pathSeparator)
>>>>> My question here is: how do I get those values when I'm running >>>>> this >>>>> code inside JUnit?
>>>>> In the code above where do you get the "dir" variable from and how >>>>> would that be possible to write code which could work using both >>>>> JUnit >>>>> and scala (on the command line)?
On Thu, Mar 19, 2009 at 7:12 AM, Mark Harrah <dmhar...@gmail.com> wrote:
> If I understand the problem correctly, the following should also work:
> new > File(classOf[ScalaObject].getProtectionDomain.getCodeSource.getLocation.toU RI)
> It gets the jar file containing ScalaObject.
> -Mark
> On 3/19/09, David Bernard <david.bernard...@gmail.com> wrote:
> > a code fragment that you could use to find a jar in classpath :
> > var cl = > > classLoader.getOrElse(Thread.currentThread.getContextClassLoader()) > > while (cl != null) { > > if (cl.isInstanceOf[URLClassLoader]) { > > val ucl = cl.asInstanceOf[URLClassLoader] > > ucl.getURLs().foreach {url => > > if (url.getProtocol().equals("file")) { > > val file = new File(URLDecoder.decode(url.getPath(), > > "UTF-8")).getCanonicalFile() > > ...
> > On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com> > wrote:
> >> Hmmm, this doesn't work. I think I need some classpath surgery to > >> retrieve the scala-library from the classpath!
> >> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote: > >>> First try to keep the setting.classpath empty and defile the > >>> classloader of the interpreter to the current one something like
> >>> val cl = Thread.currentThread.getContextClassLoader > >>> val interp = new Interpreter(settings, new PrintWriter(out)){ > >>> override def parentClassLoader = cl > >>> }
> >>> If it not work I'll search in my notes (older how you could retreive > >>> scala-library.jar location from classpath)
> >>> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> > wrote:
> >>> > Hi David,
> >>> >> When I wrote the scala macro interpreter for Jedit I need to provide > a > >>> >> custom classpath to the interpreter to allow it to find > scala-library > >>> >> and scala-compiler : > >>> >> import _root_.scala.tools.nsc.{Interpreter, Settings} > >>> >> ...
> >>> >> val settings = new Settings(null) > >>> >> settings.classpath.value = List( > >>> >> settings.classpath.value, > >>> >> new File(dir, "scala-library.jar").getAbsolutePath, > >>> >> new File(dir, "scala-compiler.jar").getAbsolutePath > >>> >> ).mkString(File.pathSeparator)
> >>> > My question here is: how do I get those values when I'm running this > >>> > code inside JUnit?
> >>> > In the code above where do you get the "dir" variable from and how > >>> > would that be possible to write code which could work using both > JUnit > >>> > and scala (on the command line)?
I've not used OSGi, so I'll have to give a general answer. It depends on how OSGi loads classes. If OSGi uses a SecureClassLoader and provides a CodeSource instance for the original jar to SecureClassLoader.defineClass, then I think it should work. If OSGi masks the true source and provides a null CodeSource to defineClass, it won't work.
I'd just try it and see if it works.
-Mark
On 3/19/09, Josh Suereth <joshua.suer...@gmail.com> wrote:
> Mark, does that trick work within an OSGi container?
> On Thu, Mar 19, 2009 at 7:12 AM, Mark Harrah <dmhar...@gmail.com> wrote:
>> If I understand the problem correctly, the following should also work:
>> new >> File(classOf[ScalaObject].getProtectionDomain.getCodeSource.getLocation.toU RI)
>> It gets the jar file containing ScalaObject.
>> -Mark
>> On 3/19/09, David Bernard <david.bernard...@gmail.com> wrote:
>> > a code fragment that you could use to find a jar in classpath :
>> > var cl = >> > classLoader.getOrElse(Thread.currentThread.getContextClassLoader()) >> > while (cl != null) { >> > if (cl.isInstanceOf[URLClassLoader]) { >> > val ucl = cl.asInstanceOf[URLClassLoader] >> > ucl.getURLs().foreach {url => >> > if (url.getProtocol().equals("file")) { >> > val file = new File(URLDecoder.decode(url.getPath(), >> > "UTF-8")).getCanonicalFile() >> > ...
>> > On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com> >> wrote:
>> >> Hmmm, this doesn't work. I think I need some classpath surgery to >> >> retrieve the scala-library from the classpath!
>> >> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote: >> >>> First try to keep the setting.classpath empty and defile the >> >>> classloader of the interpreter to the current one something like
>> >>> val cl = Thread.currentThread.getContextClassLoader >> >>> val interp = new Interpreter(settings, new PrintWriter(out)){ >> >>> override def parentClassLoader = cl >> >>> }
>> >>> If it not work I'll search in my notes (older how you could retreive >> >>> scala-library.jar location from classpath)
>> >>> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> >> wrote:
>> >>> > Hi David,
>> >>> >> When I wrote the scala macro interpreter for Jedit I need to >> >>> >> provide >> a >> >>> >> custom classpath to the interpreter to allow it to find >> scala-library >> >>> >> and scala-compiler : >> >>> >> import _root_.scala.tools.nsc.{Interpreter, Settings} >> >>> >> ...
>> >>> >> val settings = new Settings(null) >> >>> >> settings.classpath.value = List( >> >>> >> settings.classpath.value, >> >>> >> new File(dir, "scala-library.jar").getAbsolutePath, >> >>> >> new File(dir, "scala-compiler.jar").getAbsolutePath >> >>> >> ).mkString(File.pathSeparator)
>> >>> > My question here is: how do I get those values when I'm running this >> >>> > code inside JUnit?
>> >>> > In the code above where do you get the "dir" variable from and how >> >>> > would that be possible to write code which could work using both >> JUnit >> >>> > and scala (on the command line)?
This is a problem with the custom class loader, AbstractFileClassLoader, used in the interpeter. It does not specify the CodeSource/ProtectionDomain of the classes loaded, although this could probably be addressed. Therefore, I would not expect the technique to work in the interpreter even outside an OSGi container. The scala runner uses a URLClassLoader and doesn't have a problem.
For what it's worth, note that because of this all libraries loaded by the interpreter will go into the default protection domain. A security policy using codeBase or signedBy will not work properly.
> Mark, does that trick work within an OSGi container?
> On Thu, Mar 19, 2009 at 7:12 AM, Mark Harrah <dmhar...@gmail.com> wrote:
>> If I understand the problem correctly, the following should also work:
>> new >> File(classOf[ScalaObject].getProtectionDomain.getCodeSource.getLocation.toU RI)
>> It gets the jar file containing ScalaObject.
>> -Mark
>> On 3/19/09, David Bernard <david.bernard...@gmail.com> wrote:
>> > a code fragment that you could use to find a jar in classpath :
>> > var cl = >> > classLoader.getOrElse(Thread.currentThread.getContextClassLoader()) >> > while (cl != null) { >> > if (cl.isInstanceOf[URLClassLoader]) { >> > val ucl = cl.asInstanceOf[URLClassLoader] >> > ucl.getURLs().foreach {url => >> > if (url.getProtocol().equals("file")) { >> > val file = new File(URLDecoder.decode(url.getPath(), >> > "UTF-8")).getCanonicalFile() >> > ...
>> > On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com> >> wrote:
>> >> Hmmm, this doesn't work. I think I need some classpath surgery to >> >> retrieve the scala-library from the classpath!
>> >> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote: >> >>> First try to keep the setting.classpath empty and defile the >> >>> classloader of the interpreter to the current one something like
>> >>> val cl = Thread.currentThread.getContextClassLoader >> >>> val interp = new Interpreter(settings, new PrintWriter(out)){ >> >>> override def parentClassLoader = cl >> >>> }
>> >>> If it not work I'll search in my notes (older how you could retreive >> >>> scala-library.jar location from classpath)
>> >>> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com> >> wrote:
>> >>> > Hi David,
>> >>> >> When I wrote the scala macro interpreter for Jedit I need to >> >>> >> provide >> a >> >>> >> custom classpath to the interpreter to allow it to find >> scala-library >> >>> >> and scala-compiler : >> >>> >> import _root_.scala.tools.nsc.{Interpreter, Settings} >> >>> >> ...
>> >>> >> val settings = new Settings(null) >> >>> >> settings.classpath.value = List( >> >>> >> settings.classpath.value, >> >>> >> new File(dir, "scala-library.jar").getAbsolutePath, >> >>> >> new File(dir, "scala-compiler.jar").getAbsolutePath >> >>> >> ).mkString(File.pathSeparator)
>> >>> > My question here is: how do I get those values when I'm running this >> >>> > code inside JUnit?
>> >>> > In the code above where do you get the "dir" variable from and how >> >>> > would that be possible to write code which could work using both >> JUnit >> >>> > and scala (on the command line)?
> On 3/19/09, Josh Suereth <joshua.suer...@gmail.com> wrote:
> > Mark, does that trick work within an OSGi container?
> > On Thu, Mar 19, 2009 at 7:12 AM, Mark Harrah <dmhar...@gmail.com> wrote:
> >> If I understand the problem correctly, the following should also work:
> >> new
> >> File(classOf[ScalaObject].getProtectionDomain.getCodeSource.getLocation.toU RI)
> >> It gets the jar file containing ScalaObject.
> >> -Mark
> >> On 3/19/09, David Bernard <david.bernard...@gmail.com> wrote:
> >> > a code fragment that you could use to find a jar in classpath :
> >> > var cl =
> >> > classLoader.getOrElse(Thread.currentThread.getContextClassLoader())
> >> > while (cl != null) {
> >> > if (cl.isInstanceOf[URLClassLoader]) {
> >> > val ucl = cl.asInstanceOf[URLClassLoader]
> >> > ucl.getURLs().foreach {url =>
> >> > if (url.getProtocol().equals("file")) {
> >> > val file = new File(URLDecoder.decode(url.getPath(),
> >> > "UTF-8")).getCanonicalFile()
> >> > ...
> >> > On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com>
> >> wrote:
> >> >> Hmmm, this doesn't work. I think I need some classpath surgery to
> >> >> retrieve the scala-library from the classpath!
> >> >> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote:
> >> >>> First try to keep the setting.classpath empty and defile the
> >> >>> classloader of the interpreter to the current one something like
> >> >>> val cl = Thread.currentThread.getContextClassLoader
> >> >>> val interp = new Interpreter(settings, new PrintWriter(out)){
> >> >>> override def parentClassLoader = cl
> >> >>> }
> >> >>> If it not work I'll search in my notes (older how you could retreive
> >> >>> scala-library.jar location from classpath)
> >> >>> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com>
> >> wrote:
> >> >>> > Hi David,
> >> >>> >> When I wrote the scala macro interpreter for Jedit I need to
> >> >>> >> provide
> >> a
> >> >>> >> custom classpath to the interpreter to allow it to find
> >> scala-library
> >> >>> >> and scala-compiler :
> >> >>> >> import _root_.scala.tools.nsc.{Interpreter, Settings}
> >> >>> >> ...
> >> >>> >> val settings = new Settings(null)
> >> >>> >> settings.classpath.value = List(
> >> >>> >> settings.classpath.value,
> >> >>> >> new File(dir, "scala-library.jar").getAbsolutePath,
> >> >>> >> new File(dir, "scala-compiler.jar").getAbsolutePath
> >> >>> >> ).mkString(File.pathSeparator)
> >> >>> > My question here is: how do I get those values when I'm running this
> >> >>> > code inside JUnit?
> >> >>> > In the code above where do you get the "dir" variable from and how
> >> >>> > would that be possible to write code which could work using both
> >> JUnit
> >> >>> > and scala (on the command line)?
I tried David B. suggestion of get the full classpath with the code
snippet he sent me and it worked ok when I also added to my Maven
surefire configuration:
> > On 3/19/09, Josh Suereth <joshua.suer...@gmail.com> wrote:
> > > Mark, does that trick work within an OSGi container?
> > > On Thu, Mar 19, 2009 at 7:12 AM, Mark Harrah <dmhar...@gmail.com> wrote:
> > >> If I understand the problem correctly, the following should also work:
> > >> new
> > >> File(classOf[ScalaObject].getProtectionDomain.getCodeSource.getLocation.toU RI)
> > >> It gets the jar file containing ScalaObject.
> > >> -Mark
> > >> On 3/19/09, David Bernard <david.bernard...@gmail.com> wrote:
> > >> > a code fragment that you could use to find a jar in classpath :
> > >> > var cl =
> > >> > classLoader.getOrElse(Thread.currentThread.getContextClassLoader())
> > >> > while (cl != null) {
> > >> > if (cl.isInstanceOf[URLClassLoader]) {
> > >> > val ucl = cl.asInstanceOf[URLClassLoader]
> > >> > ucl.getURLs().foreach {url =>
> > >> > if (url.getProtocol().equals("file")) {
> > >> > val file = new File(URLDecoder.decode(url.getPath(),
> > >> > "UTF-8")).getCanonicalFile()
> > >> > ...
> > >> > On Thu, Mar 19, 2009 at 10:30, etorreborre <etorrebo...@gmail.com>
> > >> wrote:
> > >> >> Hmmm, this doesn't work. I think I need some classpath surgery to
> > >> >> retrieve the scala-library from the classpath!
> > >> >> On Mar 19, 6:13 pm, David Bernard <david.bernard...@gmail.com> wrote:
> > >> >>> First try to keep the setting.classpath empty and defile the
> > >> >>> classloader of the interpreter to the current one something like
> > >> >>> val cl = Thread.currentThread.getContextClassLoader
> > >> >>> val interp = new Interpreter(settings, new PrintWriter(out)){
> > >> >>> override def parentClassLoader = cl
> > >> >>> }
> > >> >>> If it not work I'll search in my notes (older how you could retreive
> > >> >>> scala-library.jar location from classpath)
> > >> >>> On Thu, Mar 19, 2009 at 09:52, etorreborre <etorrebo...@gmail.com>
> > >> wrote:
> > >> >>> > Hi David,
> > >> >>> >> When I wrote the scala macro interpreter for Jedit I need to
> > >> >>> >> provide
> > >> a
> > >> >>> >> custom classpath to the interpreter to allow it to find
> > >> scala-library
> > >> >>> >> and scala-compiler :
> > >> >>> >> import _root_.scala.tools.nsc.{Interpreter, Settings}
> > >> >>> >> ...
> > >> >>> > My question here is: how do I get those values when I'm running this
> > >> >>> > code inside JUnit?
> > >> >>> > In the code above where do you get the "dir" variable from and how
> > >> >>> > would that be possible to write code which could work using both
> > >> JUnit
> > >> >>> > and scala (on the command line)?