简体   繁体   中英

Convert a java string classpath into a wildcard where more than `N` jars from the same directory show up in the classpath?

I launch java from an agent launcher. The class path gets so large I need to use the manifest.mf file or Windows will run out of cmd line space (bash has a limit too but it's much higher so we don't see it there).

I'd rather just use wildcard on the classpath.

How can I write a java method that can convert the a string classpath (example: /my/dir/myjar.jar /my/dir/myjar2.jar ... ) into a wildcard where more than N jars from the same directory show up in the classpath?

Here is what i'm working out:

public static String convertClasspathToWildcard(String cp) {
    Set<String> parentCountMap = new HashSet<>();
    for (String nextJar : cp.split(SystemUtils.IS_OS_WINDOWS && cp.contains(";") ? ";" : ":")) {
      String substring = nextJar.substring(0, nextJar.lastIndexOf(File.separator));
      parentCountMap.add(substring);
    }
    StringBuilder newCp = new StringBuilder();
    for (String parent : parentCountMap) {
      if (newCp.length() > 0) {
        newCp.append(SystemUtils.IS_OS_WINDOWS ? ";" : ":");
      }
      newCp.append(parent + File.separator + "*");
    }
    return newCp.toString();
  }

Doesn't quite work but it's the idea I'm going for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM