Skip to content

Commit 268c419

Browse files
tsudukimsrowen
authored andcommitted
[SPARK-6435] spark-shell --jars option does not add all jars to classpath
Modified to accept double-quotated args properly in spark-shell.cmd. Author: Masayoshi TSUZUKI <[email protected]> Closes #5227 from tsudukim/feature/SPARK-6435-2 and squashes the following commits: ac55787 [Masayoshi TSUZUKI] removed unnecessary argument. 60789a7 [Masayoshi TSUZUKI] Merge branch 'master' of https://github.com/apache/spark into feature/SPARK-6435-2 1fee420 [Masayoshi TSUZUKI] fixed test code for escaping '='. 0d4dc41 [Masayoshi TSUZUKI] - escaped comman and semicolon in CommandBuilderUtils.java - added random string to the temporary filename - double-quotation followed by `cmd /c` did not worked properly - no need to escape `=` by `^` - if double-quoted string ended with `\` like classpath, the last `\` is parsed as the escape charactor and the closing `"` didn't work properly 2a332e5 [Masayoshi TSUZUKI] Merge branch 'master' into feature/SPARK-6435-2 04f4291 [Masayoshi TSUZUKI] [SPARK-6435] spark-shell --jars option does not add all jars to classpath
1 parent 75905c5 commit 268c419

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

bin/spark-class2.cmd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ if not "x%JAVA_HOME%"=="x" set RUNNER=%JAVA_HOME%\bin\java
6161

6262
rem The launcher library prints the command to be executed in a single line suitable for being
6363
rem executed by the batch interpreter. So read all the output of the launcher into a variable.
64-
for /f "tokens=*" %%i in ('cmd /C ""%RUNNER%" -cp %LAUNCH_CLASSPATH% org.apache.spark.launcher.Main %*"') do (
64+
set LAUNCHER_OUTPUT=%temp%\spark-class-launcher-output-%RANDOM%.txt
65+
"%RUNNER%" -cp %LAUNCH_CLASSPATH% org.apache.spark.launcher.Main %* > %LAUNCHER_OUTPUT%
66+
for /f "tokens=*" %%i in (%LAUNCHER_OUTPUT%) do (
6567
set SPARK_CMD=%%i
6668
)
69+
del %LAUNCHER_OUTPUT%
6770
%SPARK_CMD%

launcher/src/main/java/org/apache/spark/launcher/CommandBuilderUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ static String quoteForBatchScript(String arg) {
244244
boolean needsQuotes = false;
245245
for (int i = 0; i < arg.length(); i++) {
246246
int c = arg.codePointAt(i);
247-
if (Character.isWhitespace(c) || c == '"' || c == '=') {
247+
if (Character.isWhitespace(c) || c == '"' || c == '=' || c == ',' || c == ';') {
248248
needsQuotes = true;
249249
break;
250250
}
@@ -261,15 +261,14 @@ static String quoteForBatchScript(String arg) {
261261
quoted.append('"');
262262
break;
263263

264-
case '=':
265-
quoted.append('^');
266-
break;
267-
268264
default:
269265
break;
270266
}
271267
quoted.appendCodePoint(cp);
272268
}
269+
if (arg.codePointAt(arg.length() - 1) == '\\') {
270+
quoted.append("\\");
271+
}
273272
quoted.append("\"");
274273
return quoted.toString();
275274
}

launcher/src/main/java/org/apache/spark/launcher/Main.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,9 @@ public static void main(String[] argsArray) throws Exception {
101101
* The method quotes all arguments so that spaces are handled as expected. Quotes within arguments
102102
* are "double quoted" (which is batch for escaping a quote). This page has more details about
103103
* quoting and other batch script fun stuff: http://ss64.com/nt/syntax-esc.html
104-
*
105-
* The command is executed using "cmd /c" and formatted in single line, since that's the
106-
* easiest way to consume this from a batch script (see spark-class2.cmd).
107104
*/
108105
private static String prepareWindowsCommand(List<String> cmd, Map<String, String> childEnv) {
109-
StringBuilder cmdline = new StringBuilder("cmd /c \"");
106+
StringBuilder cmdline = new StringBuilder();
110107
for (Map.Entry<String, String> e : childEnv.entrySet()) {
111108
cmdline.append(String.format("set %s=%s", e.getKey(), e.getValue()));
112109
cmdline.append(" && ");
@@ -115,7 +112,6 @@ private static String prepareWindowsCommand(List<String> cmd, Map<String, String
115112
cmdline.append(quoteForBatchScript(arg));
116113
cmdline.append(" ");
117114
}
118-
cmdline.append("\"");
119115
return cmdline.toString();
120116
}
121117

launcher/src/test/java/org/apache/spark/launcher/CommandBuilderUtilsSuite.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public void testWindowsBatchQuoting() {
7474
assertEquals("\"a b c\"", quoteForBatchScript("a b c"));
7575
assertEquals("\"a \"\"b\"\" c\"", quoteForBatchScript("a \"b\" c"));
7676
assertEquals("\"a\"\"b\"\"c\"", quoteForBatchScript("a\"b\"c"));
77-
assertEquals("\"ab^=\"\"cd\"\"\"", quoteForBatchScript("ab=\"cd\""));
77+
assertEquals("\"ab=\"\"cd\"\"\"", quoteForBatchScript("ab=\"cd\""));
78+
assertEquals("\"a,b,c\"", quoteForBatchScript("a,b,c"));
79+
assertEquals("\"a;b;c\"", quoteForBatchScript("a;b;c"));
80+
assertEquals("\"a,b,c\\\\\"", quoteForBatchScript("a,b,c\\"));
7881
}
7982

8083
@Test

0 commit comments

Comments
 (0)