@@ -16,6 +16,7 @@ import java.util.concurrent.{ThreadPoolExecutor, ThreadFactory, LinkedBlockingQu
16
16
import akka .dispatch .ExecutionContext
17
17
import java .security .Permission
18
18
import java .io .FilePermission
19
+ import java .net .URLDecoder
19
20
20
21
21
22
object ScalatronImpl
@@ -63,7 +64,7 @@ object ScalatronImpl
63
64
64
65
65
66
val sandboxUntrustedCode = argMap.get(" -sandboxed" ).getOrElse(" no" ) == " yes"
66
- val scalatronJarFilePath = classOf [ScalatronImpl ].getProtectionDomain.getCodeSource.getLocation.getPath
67
+ val scalatronJarFilePath = getClassPath( classOf [ScalatronImpl ])
67
68
implicit val executionContextForUntrustedCode = createExecutionContextForUntrustedCode(
68
69
scalatronJarFilePath,
69
70
scalatronInstallationDirectoryPath + " /out/" ,
@@ -223,6 +224,24 @@ object ScalatronImpl
223
224
sandboxedExecutionContext
224
225
}
225
226
227
+
228
+ /** Given a class, this method determines the file system path of the .jar file the class definition was
229
+ * loaded from. It handles the URLDecoding that is necessary to e.g. deal with paths that contain spaces.
230
+ * Example usage: <code>val scalatronJarFilePathAsURL = getClass(classOf[ScalatronImpl])</code>
231
+ * @param theClass the class whose .jar file path is sought
232
+ * @tparam T the type of the class (not used)
233
+ * @return a string representing the file system path to the .jar file
234
+ */
235
+ def getClassPath [T ](theClass : Class [T ]) : String = {
236
+ // The path returned here is URL encoded, resulting in components like "Documents%20and%20Settings"
237
+ val jarFilePathAsURL = theClass.getProtectionDomain.getCodeSource.getLocation.getPath
238
+
239
+ // so we URL-decode the path before returning it:
240
+ val characterEncoding = " UTF-8" // Eight-bit UCS Transformation Format
241
+ URLDecoder .decode(jarFilePathAsURL, characterEncoding)
242
+ }
243
+
244
+
226
245
// try to locate a base directory for the installation, e.g. '/Scalatron'
227
246
def detectInstallationDirectory (verbose : Boolean ) = {
228
247
// Strategy A: use the Java user directory
@@ -231,7 +250,8 @@ object ScalatronImpl
231
250
// val scalatronInstallationDirectoryPath = userDirectoryPath + "/.."
232
251
233
252
// Strategy B: use the path to the Scalatron.jar file
234
- val scalatronJarFilePath = classOf [ScalatronImpl ].getProtectionDomain.getCodeSource.getLocation.getPath
253
+ val scalatronJarFilePath = getClassPath(classOf [ScalatronImpl ])
254
+
235
255
if (verbose) println(" Detected Scalatron class path to be: " + scalatronJarFilePath)
236
256
237
257
val scalatronInstallationDirectoryPath =
0 commit comments