6
6
package scala
7
7
package tools .nsc
8
8
9
- import io .{ Directory , File , Path }
9
+ import io .{ AbstractFile , Directory , File , Path }
10
10
import java .io .IOException
11
11
import scala .tools .nsc .reporters .{Reporter ,ConsoleReporter }
12
12
import util .Exceptional .unwrap
@@ -111,6 +111,12 @@ class ScriptRunner extends HasCompileSocket {
111
111
else None
112
112
}
113
113
114
+ def hasClassToRun (d : Directory ): Boolean = {
115
+ import util .ClassPath .{ DefaultJavaContext => ctx }
116
+ val cp = ctx.newClassPath(AbstractFile .getDirectory(d))
117
+ cp.findClass(mainClass).isDefined
118
+ }
119
+
114
120
/* The script runner calls sys.exit to communicate a return value, but this must
115
121
* not take place until there are no non-daemon threads running. Tickets #1955, #2006.
116
122
*/
@@ -124,24 +130,30 @@ class ScriptRunner extends HasCompileSocket {
124
130
125
131
compile match {
126
132
case Some (compiledPath) =>
127
- try io.Jar .create(jarFile, compiledPath, mainClass)
128
- catch { case _ : Exception => jarFile.delete() }
129
-
130
- if (jarOK) {
131
- compiledPath.deleteRecursively()
132
- handler(jarFile.toAbsolute.path)
133
+ if (! hasClassToRun(compiledPath)) {
134
+ // it compiled ok, but there is nothing to run;
135
+ // running an empty script should succeed
136
+ true
137
+ } else {
138
+ try io.Jar .create(jarFile, compiledPath, mainClass)
139
+ catch { case _ : Exception => jarFile.delete() }
140
+
141
+ if (jarOK) {
142
+ compiledPath.deleteRecursively()
143
+ handler(jarFile.toAbsolute.path)
144
+ }
145
+ // jar failed; run directly from the class files
146
+ else handler(compiledPath.path)
133
147
}
134
- // jar failed; run directly from the class files
135
- else handler(compiledPath.path)
136
148
case _ => false
137
149
}
138
150
}
139
151
140
152
if (jarOK) handler(jarFile.toAbsolute.path) // pre-compiled jar is current
141
153
else recompile() // jar old - recompile the script.
142
154
}
143
- // don't use a cache jar at all--just use the class files
144
- else compile exists (cp => handler(cp.path))
155
+ // don't use a cache jar at all--just use the class files, if they exist
156
+ else compile exists (cp => ! hasClassToRun(cp) || handler(cp.path))
145
157
}
146
158
}
147
159
0 commit comments