|
1 | 1 | /******************************************************************************* |
2 | | -* Copyright (c) 2018-2021 Microsoft Corporation and others. |
| 2 | +* Copyright (c) 2018-2022 Microsoft Corporation and others. |
3 | 3 | * All rights reserved. This program and the accompanying materials |
4 | 4 | * are made available under the terms of the Eclipse Public License v1.0 |
5 | 5 | * which accompanies this distribution, and is available at |
|
16 | 16 | import java.net.MalformedURLException; |
17 | 17 | import java.net.URISyntaxException; |
18 | 18 | import java.nio.charset.Charset; |
| 19 | +import java.nio.charset.CharsetEncoder; |
19 | 20 | import java.nio.file.Path; |
20 | 21 | import java.nio.file.Paths; |
21 | 22 | import java.util.ArrayList; |
@@ -134,11 +135,50 @@ protected CompletableFuture<Response> handleLaunchCommand(Arguments arguments, R |
134 | 135 | } |
135 | 136 | } else if (launchArguments.shortenCommandLine == ShortenApproach.ARGFILE) { |
136 | 137 | try { |
137 | | - Path tempfile = LaunchUtils.generateArgfile(launchArguments.vmArgs, launchArguments.classPaths, launchArguments.modulePaths); |
138 | | - launchArguments.vmArgs = " \"@" + tempfile.toAbsolutePath().toString() + "\""; |
139 | | - launchArguments.classPaths = new String[0]; |
140 | | - launchArguments.modulePaths = new String[0]; |
141 | | - context.setArgsfile(tempfile); |
| 138 | + /** |
| 139 | + * See the JDK spec https://docs.oracle.com/en/java/javase/18/docs/specs/man/java.html#java-command-line-argument-files. |
| 140 | + * The argument file must contain only ASCII characters or characters in system default encoding that's ASCII friendly. |
| 141 | + */ |
| 142 | + Charset systemCharset = LaunchUtils.getSystemCharset(); |
| 143 | + CharsetEncoder encoder = systemCharset.newEncoder(); |
| 144 | + String vmArgsForShorten = null; |
| 145 | + String[] classPathsForShorten = null; |
| 146 | + String[] modulePathsForShorten = null; |
| 147 | + if (StringUtils.isNotBlank(launchArguments.vmArgs)) { |
| 148 | + if (!encoder.canEncode(launchArguments.vmArgs)) { |
| 149 | + logger.warning(String.format("Cannot generate the 'vmArgs' argument into the argfile because it contains characters " |
| 150 | + + "that cannot be encoded in the system charset '%s'.", systemCharset.displayName())); |
| 151 | + } else { |
| 152 | + vmArgsForShorten = launchArguments.vmArgs; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (ArrayUtils.isNotEmpty(launchArguments.classPaths)) { |
| 157 | + if (!encoder.canEncode(String.join(File.pathSeparator, launchArguments.classPaths))) { |
| 158 | + logger.warning(String.format("Cannot generate the '-cp' argument into the argfile because it contains characters " |
| 159 | + + "that cannot be encoded in the system charset '%s'.", systemCharset.displayName())); |
| 160 | + } else { |
| 161 | + classPathsForShorten = launchArguments.classPaths; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (ArrayUtils.isNotEmpty(launchArguments.modulePaths)) { |
| 166 | + if (!encoder.canEncode(String.join(File.pathSeparator, launchArguments.modulePaths))) { |
| 167 | + logger.warning(String.format("Cannot generate the '--module-path' argument into the argfile because it contains characters " |
| 168 | + + "that cannot be encoded in the system charset '%s'.", systemCharset.displayName())); |
| 169 | + } else { |
| 170 | + modulePathsForShorten = launchArguments.modulePaths; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if (vmArgsForShorten != null || classPathsForShorten != null || modulePathsForShorten != null) { |
| 175 | + Path tempfile = LaunchUtils.generateArgfile(vmArgsForShorten, classPathsForShorten, modulePathsForShorten, systemCharset); |
| 176 | + launchArguments.vmArgs = (vmArgsForShorten == null ? launchArguments.vmArgs : "") |
| 177 | + + " \"@" + tempfile.toAbsolutePath().toString() + "\""; |
| 178 | + launchArguments.classPaths = (classPathsForShorten == null ? launchArguments.classPaths : new String[0]); |
| 179 | + launchArguments.modulePaths = (modulePathsForShorten == null ? launchArguments.modulePaths : new String[0]); |
| 180 | + context.setArgsfile(tempfile); |
| 181 | + } |
142 | 182 | } catch (IOException e) { |
143 | 183 | logger.log(Level.SEVERE, String.format("Failed to create a temp argfile: %s", e.toString()), e); |
144 | 184 | } |
|
0 commit comments