Skip to content

Polish ClasspathBuilder #44330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,30 @@ protected ClasspathBuilder(List<URL> urls) {
}

/**
* Builds a classpath string or an argument file representing the classpath, depending
* on the operating system.
* @param urls an array of {@link URL} representing the elements of the classpath
* @return the classpath; on Windows, the path to an argument file is returned,
* prefixed with '@'
* Creates a ClasspathBuilder instance using the specified list of URLs.
* @param urls a list of {@link URL} objects representing the elements of the
* classpath
* @return a new instance of {@code ClasspathBuilder}
*/
static ClasspathBuilder forURLs(List<URL> urls) {
return new ClasspathBuilder(new ArrayList<>(urls));
}

/**
* Builds a classpath string or an argument file representing the classpath, depending
* on the operating system.
* @param urls an array of {@link URL} representing the elements of the classpath
* @return the classpath; on Windows, the path to an argument file is returned,
* prefixed with '@'
* Creates a ClasspathBuilder instance using the specified array of URLs.
* @param urls an array of {@link URL} objects representing the elements of the
* classpath
* @return a new instance of {@code ClasspathBuilder}
*/
static ClasspathBuilder forURLs(URL... 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() {
if (ObjectUtils.isEmpty(this.urls)) {
return new Classpath("", Collections.emptyList());
Expand All @@ -88,6 +91,13 @@ Classpath build() {
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() {
String os = System.getProperty("os.name");
if (!StringUtils.hasText(os)) {
Expand Down Expand Up @@ -145,6 +155,9 @@ private static Path toFile(URL url) {
}
}

/**
* Classpath consisting of a {@code -cp} argument and its associated elements.
*/
static final class Classpath {

private final String argument;
Expand All @@ -157,7 +170,8 @@ private Classpath(String argument, List<Path> elements) {
}

/**
* 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
*/
String argument() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ void buildWithClassPathOnWindows(@TempDir Path tempDir) throws Exception {

@Test
void buildAndRunWithLongClassPath() throws IOException, InterruptedException {
URL[] urls = Arrays.stream(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
.map(this::toURL)
.toArray(URL[]::new);
StringBuilder classPath = new StringBuilder(ManagementFactory.getRuntimeMXBean().getClassPath());
// Simulates [CreateProcess error=206, The filename or extension is too long]
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())
.withClasspath(urls)
.build();
Expand Down