How to Use a Wildcard in the Classpath to Add Multiple Jars

How to use a wildcard in the classpath to add multiple jars?

From: http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

This should work in Java6, not sure about Java5

(If it seems it does not work as expected, try putting quotes. eg: "foo/*")

How to use a wildcard in the classpath to add multiple jars?

From: http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

This should work in Java6, not sure about Java5

(If it seems it does not work as expected, try putting quotes. eg: "foo/*")

Using wildcards in java classpath

The java classpath wildcard expansion is unusual.

From the docs:

Understanding class path wildcards

Class path entries can contain the basename wildcard character *, which is considered equivalent to
specifying a list of all the files in the directory with the extension
.jar or .JAR. For example, the class path entry foo/* specifies all
JAR files in the directory named foo. A classpath entry consisting
simply of * expands to a list of all the jar files in the current
directory.

So, what you need to specify is:

java -classpath /path/to/app/conf/lib/*:/path/to/app/lib/*

If you need only specific jars, you will need to add them individually. The classpath string does not accept generic wildcards like nameJar*, *.jar, spring* etc.

Read Setting multiple jars in java classpath for more information.

How to set up classpath of JavaCompiler to multiple .jar files using wildcard

Wildcards: Since Java 1.6 wildcards are supported when using java/javaw/javac, more information: Windows/Solaris and Linux

example:

javac -cp "lib/*" Test.java

This uses all .jar files (not .class!) in the lib directory as classpath. This should not be confused with the *-expansion of your shell. -cp lib/* gets expanded to -cp lib/a.jar lib/b.jar which is not valid argument syntax. In order to avoid this you have to add quotation marks: -cp "lib/*"

The cause of your Problem: You are trying to call the Java compiler from source directly with its Java API. This source code does not contain the wildcard expansion.
The JDK ships with a wrapper binary (javac,javadoc,javah,javap are all the same binary) which does some things and finally calls the compiler task. This wrapper also expands the wildcards in your classpath and therefore the compiler task doesn't have to do this anymore (and it doesn't). See at Compiler Readme section "build -> Notes -> The launcher". Launcher sourcecode.

Solution:

  1. A very poor solution would be to call javac through a Processbuilder. (This is not recommended since it is a complicated and error prone solution for a simple problem)
  2. Expand the wildcards yourself:

example code:

String classpath = buildClassPath("lib/", "test/", "lib/*");
System.out.println(classpath);
// output: lib/;test/;lib/a.jar;lib/b.jar;

This function takes all classpath entries and builds one classpath. Classpath entries with a wildcard in it will get expanded.

/**
* This function builds a classpath from the passed Strings
*
* @param paths classpath elements
* @return returns the complete classpath with wildcards expanded
*/
private static String buildClassPath(String... paths) {
StringBuilder sb = new StringBuilder();
for (String path : paths) {
if (path.endsWith("*")) {
path = path.substring(0, path.length() - 1);
File pathFile = new File(path);
for (File file : pathFile.listFiles()) {
if (file.isFile() && file.getName().endsWith(".jar")) {
sb.append(path);
sb.append(file.getName());
sb.append(System.getProperty("path.separator"));
}
}
} else {
sb.append(path);
sb.append(System.getProperty("path.separator"));
}
}
return sb.toString();
}

How to specify a wildcard path for jar libraries for a java command line application?

The correct way is to use simply jars/*. Classpath wildcards in the Oracle JRE represent a list of .jar files, and not part of the file name. Here's a quote from the documentation:

Class path entries can contain the base name wildcard character (*),
which is considered equivalent to specifying a list of all of the
files in the directory with the extension .jar or .JAR. For example,
the class path entry mydir/* specifies all JAR files in the directory
named mydir. A class path entry consisting of * expands to a list of
all the jar files in the current directory. Files are considered
regardless of whether they are hidden (have names beginning with '.').

Reference: Class Path Wild Cards at http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

Loading resources from multiple jars with wild card

No, there is no standard/reliable way to do this. Some libraries take advantage of common patterns of ClassLoader.getResources implementations (specifically, that they usually always return "file:" or "jar:file:" URLs) in order to support wildcards in resource lookups. For example, the Wildcards in application context constructor resource paths explains how Spring does this, and it lists several caveats ("Implications on portability", "Classpath*: portability", "notes relating to wildcards").

spark submit add multiple jars in classpath

I was trying to connect to mysql from the python code that was executed using spark-submit.

I was using HDP sandbox that was using Ambari. Tried lot of options such as --jars, --driver-class-path, etc, but none worked.

Solution

Copy the jar in /usr/local/miniconda/lib/python2.7/site-packages/pyspark/jars/

As of now I'm not sure if it's a solution or a quick hack, but since I'm working on POC so it kind of works for me.

Adding multiple jar files into classpath

The * syntax for classpaths is rather limited. Try:

java -classpath "~/twitter4j-2.2.6/lib/*:." GetHashtag


Related Topics



Leave a reply



Submit