Polish ClasspathBuilder
See gh-44330 Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
This commit is contained in:
parent
b6abb42ec9
commit
616d4cb149
|
@ -51,27 +51,30 @@ class ClasspathBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a classpath string or an argument file representing the classpath, depending
|
* Creates a ClasspathBuilder instance using the specified list of URLs.
|
||||||
* on the operating system.
|
* @param urls a list of {@link URL} objects representing the elements of the
|
||||||
* @param urls an array of {@link URL} representing the elements of the classpath
|
* classpath
|
||||||
* @return the classpath; on Windows, the path to an argument file is returned,
|
* @return a new instance of {@code ClasspathBuilder}
|
||||||
* prefixed with '@'
|
|
||||||
*/
|
*/
|
||||||
static ClasspathBuilder forURLs(List<URL> urls) {
|
static ClasspathBuilder forURLs(List<URL> urls) {
|
||||||
return new ClasspathBuilder(new ArrayList<>(urls));
|
return new ClasspathBuilder(new ArrayList<>(urls));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a classpath string or an argument file representing the classpath, depending
|
* Creates a ClasspathBuilder instance using the specified array of URLs.
|
||||||
* on the operating system.
|
* @param urls an array of {@link URL} objects representing the elements of the
|
||||||
* @param urls an array of {@link URL} representing the elements of the classpath
|
* classpath
|
||||||
* @return the classpath; on Windows, the path to an argument file is returned,
|
* @return a new instance of {@code ClasspathBuilder}
|
||||||
* prefixed with '@'
|
|
||||||
*/
|
*/
|
||||||
static ClasspathBuilder forURLs(URL... urls) {
|
static ClasspathBuilder forURLs(URL... urls) {
|
||||||
return new ClasspathBuilder(Arrays.asList(urls));
|
return new ClasspathBuilder(Arrays.asList(urls));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds {@link Classpath} that containing a classpath argument and its corresponding
|
||||||
|
* classpath elements.
|
||||||
|
* @return a {@code Classpath}
|
||||||
|
*/
|
||||||
Classpath build() {
|
Classpath build() {
|
||||||
if (ObjectUtils.isEmpty(this.urls)) {
|
if (ObjectUtils.isEmpty(this.urls)) {
|
||||||
return new Classpath("", Collections.emptyList());
|
return new Classpath("", Collections.emptyList());
|
||||||
|
@ -88,6 +91,13 @@ class ClasspathBuilder {
|
||||||
return new Classpath(argument, files);
|
return new Classpath(argument, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if an argument file should be used for the classpath based on the
|
||||||
|
* operating system. On Windows, argument files are used due to the command length
|
||||||
|
* limitation.
|
||||||
|
* @return {@code true} if an argument file is required for the classpath,
|
||||||
|
* {@code false} otherwise
|
||||||
|
*/
|
||||||
protected boolean needsClasspathArgFile() {
|
protected boolean needsClasspathArgFile() {
|
||||||
String os = System.getProperty("os.name");
|
String os = System.getProperty("os.name");
|
||||||
if (!StringUtils.hasText(os)) {
|
if (!StringUtils.hasText(os)) {
|
||||||
|
@ -145,6 +155,9 @@ class ClasspathBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classpath consisting of a {@code -cp} argument and its associated elements.
|
||||||
|
*/
|
||||||
static final class Classpath {
|
static final class Classpath {
|
||||||
|
|
||||||
private final String argument;
|
private final String argument;
|
||||||
|
@ -157,7 +170,8 @@ class ClasspathBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the {@code -cp} argument value.
|
* Return the {@code -cp} argument value; on Windows, the path to an argument file
|
||||||
|
* is returned, prefixed with '@'.
|
||||||
* @return the argument to use
|
* @return the argument to use
|
||||||
*/
|
*/
|
||||||
String argument() {
|
String argument() {
|
||||||
|
|
|
@ -119,9 +119,12 @@ class CommandLineBuilderTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void buildAndRunWithLongClassPath() throws IOException, InterruptedException {
|
void buildAndRunWithLongClassPath() throws IOException, InterruptedException {
|
||||||
URL[] urls = Arrays.stream(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
|
StringBuilder classPath = new StringBuilder(ManagementFactory.getRuntimeMXBean().getClassPath());
|
||||||
.map(this::toURL)
|
// Simulates [CreateProcess error=206, The filename or extension is too long]
|
||||||
.toArray(URL[]::new);
|
while (classPath.length() < 35000) {
|
||||||
|
classPath.append(File.pathSeparator).append(classPath);
|
||||||
|
}
|
||||||
|
URL[] urls = Arrays.stream(classPath.toString().split(File.pathSeparator)).map(this::toURL).toArray(URL[]::new);
|
||||||
List<String> command = CommandLineBuilder.forMainClass(ClassWithMainMethod.class.getName())
|
List<String> command = CommandLineBuilder.forMainClass(ClassWithMainMethod.class.getName())
|
||||||
.withClasspath(urls)
|
.withClasspath(urls)
|
||||||
.build();
|
.build();
|
||||||
|
|
Loading…
Reference in New Issue