If I place a file called libA.jar in a classpath folder, and rename the old one to:
libA.jar.old
Will the classloader load the classes?
I'm using weblogic over Solaris 8.
Thank you!
Udo
No.
If you're using Java 5 or earlier, you must explicitly name all classes and jar files to be loaded. Obviously, since the old one, libA.jar.old isn't named, it won't be loaded.
It's a bit of a different story if you're using Java 6, since concept of wildcard matching exists there.
Still, non jar files won't be loaded. Info taken from official site. Quote:
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. A class path entry
that contains * will not match class
files. To match both classes and JAR
files in a single directory foo, use
either foo;foo/* or foo/*;foo. The
order chosen determines whether the
classes and resources in foo are
loaded before JAR files in foo, or
vice versa.
Related
I am trying to enumerate classes in the package with
Enumeration<URL> resourceUrls = myObject.getClassLoader().getResources("path/to/my/package/");
while (resourceUrls.hasMoreElements()) {
...
Unfortunately it returns nothing. Why?
Assuming path is correct. Path starts with no slash and ends with slash. There are several public classes under path.to.my.package package.
I took this code from Spring.
You cannot walk a class path like you can walk a file path. Walking a file path is done on the file system, which does not apply to a class path.
While a java class path entries are formed like file paths and usually are folders and files (either on the file system or inside a JAR archive), it does not necessarily have to be that way. In fact, the classes of one single package may originate from various locations of differing nature: one might be loaded from a local JAR file while another one might be loaded from a remote URL.
The method ClassLoader.getResources() exists to provide access to all "occurrences" of a resource if it has the same name in different JAR files (or other locations). For example you can use
ClassLoader.getSystemClassLoader().getResources("META-INF/MANIFEST.MF");
to access the manifest file of each JAR file in your class path.
Try with
Enumeration<URL> urls = ClassLoader.getSystemClassLoader().getResources("path/to/my/package");
while (urls.hasMoreElements()) {
System.out.println(urls.nextElement());
}
I'm writing a command line utility to extract Adobe Form Data from PDF's
This command line fails.
java -classpath jars/*.jar:. extractpdfformdata.ExtractPDFFormData --pdf csmu-asfm.pdf
where as this works.
java -classpath jars/commons-cli-1.3.1.jar:. extractpdfformdata.ExtractPDFFormData --pdf csmu-asfm.pdf
What is the correct way to pass in wildcard paths for jar libraries to java?
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
When I give the -classpath argument to java to start will it pick up only files which have .jar extensions or will it attempt to look into other files in -classpath path as well?
Meaning if I specify -classpath to be /mypath which contains
/mypath/IAmANormalJar.jar
/mypath/IAmAJarWithoutExtension
where IAmAJarWithoutExtension is an actual jar file but without the .jar extension. Will only /mypath/IAmANormalJar.jar be loaded by JVM or will /mypath/IAmAJarWithoutExtension be loaded as well?
http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html
Classpath entries that are neither directories nor archives (.zip or
.jar files) nor * are ignored.
The wildcard symbol explained:
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. Files will be considered
regardless of whether or not they are hidden (that is, have names
beginning with '.').
Java classpath requires jar to be completed specified with extension. Without the extension Java treats that as a directory to which there are class files present to be added to the Java classpath. The Wikipedia article on the Java classpath provides some good information in this regard:
https://en.wikipedia.org/wiki/Classpath_(Java)
It will only pick up files with the extension .class, .jar or .zip. One trick I used to use when I wanted to flip between two jars for testing would be to add another extension to the file I wanted to be "out". If the version I wanted to test was mylib.jar, I would modify the old one as mylib.jar.old and they could co-exist in the directory.
I was working on the Stanford sentiment classifier on windows. I wanted to retrain my own model, and here's how it was specified on the website:
java -mx8g edu.stanford.nlp.sentiment.SentimentTraining -numHid 25 -trainPath train.txt -devPath dev.txt -train -model model.ser.gz
But this gave me the error:
could not find or load main class
But on changing it to java -cp "*" it worked.
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.
From Oracle Docs
-cp < class search path of directories and zip/jar files>
Search all jar and zip files in the current directory for a given class file
The cp flag specifies the classpath, that is, which other archives are to be considered for this program.
Usually, you'd give it a colon-separated list of jar files, but this particular example is a special case according to the documentation:
If -classpath and -cp are not used and CLASSPATH is not set, then the user class path consists of the current directory (.). As a special convenience, a class path element that contains a base name of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. A Java program cannot tell the difference between the two invocations.
Note that the quotes are necessary, beause otherwise the shell would exapand it to all the files in the directory rather than only jar files.
I plan on becoming a certified Java programmer and am studying from the Sierra-Bates book. I had a question about classpaths. Do classpaths need to find only the supporting classes of the class I'm running/compiling, or the supporting classes and the class itself? Also, when I'm getting classes in packages from classpaths, is it legal to just put the adress of the file(the path to it), instead of putting it's root package. Thanks.
1 - a classpath has to give access to each class that needs to run in your program. That would include the main class and any classes it calls and those they call. If there is some code in one of those classes that is never called, in many cases, you don't need to have the classes referenced by the uncalled code.
2 - you have to put the root of the packages in the classpath. So a class "com.bob.myprog.Main" would need to have the class path point to the folder where the "com" package/folder lies. It will need to contain a "bob" folder and "bob" will need to contain a "myprog" folder with "Main.class" in it.
Classpath has to contain both the supporting classes and the class itself.
However, sometimes you can run a single file without specifying classpath (and it will work).
As specified in http://docs.oracle.com/javase/tutorial/essential/environment/paths.html :
The default value of the class path is ".", meaning that only the
current directory is searched. Specifying either the CLASSPATH
variable or the -cp command line switch overrides this value.
Therefore, if you have a class MyClass compiled in the current directory, the following will work:
java MyClass
while pointing classpath to another directory will lead to an error (classpath no longer contains MyClass):
java -cp lib MyClass
When you have a class in a package, it is not enough to put the address to the class file in the classpath. According to SCJP Sun Certified Programmer for Java 5 Study Guide:
In order to find a class in a package, you have to have a directory in
your classpath that has the package's leftmost entry (the package's
"root") as a subdirectory.