Java Manifest Files & Classpath issues
The other day I had an issue with a very very very long environment variable on Windows, and it turns out that cmd.exe drops anyting beyond 2K or 8K chars (say Enviroment Variables, Command line arguments, etc)
Even though the Win32 limitation for environment variables is 32,767 characters, Command Prompt ignores any environment variables that are inherited from the parent process and are longer than its own limitations of either 2047 or 8191 characters (as appropriate to the operating system). For more information about the SetEnvironmentVariable function, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/ms686206.aspx
So, this very long env. variable was a CLASSPATH to be used by java to run some testing, thus I was forced to look for a different way to specify a classpath that supported a whole bunch of paths (~330 in this specific case).
I found that one can use a Manifest packaged into a jar file, this manifest will only contain a list of paths to be used as CLASSPATH, in other words a Classpath manifest jar. This is how it works:
(you can learn more about java Manifests here)
Create a text file Manifest.txt
In that Manifest.txt file add a Class-Path header followed by a list of jar files.
Class-Path: file1.jar dir1/file2.jar dirx/fileb.jar dirZ/dirL/dirF/file3.jar ...
However, note that according to the jar file specification (here) there is a limitation:
No line may be longer than 72 bytes (not characters), in its UTF8-encoded form. If a value would make the initial line longer than this, it should be continued on extra lines (each starting with a single SPACE).
So just type one path per line.
Then all you need to do is to make a jar file containing that Manifest.txt, use the jar tool for that purpose: (I'm using $> to denote my prompt)
$> jar cfm mynewjar.jar Manifest.txt
c - create a new file
f - specify a file name
m - include manifest information from the specified file.
If you want to look what is inside the jar file, just use:
And if you want to extract the contents:
Now all you need to do is use this mynewjar.jar in your java commandline:
$> java -classpath /path/to/mynew.jar ......
And the most important note (I spent so much time trying to figure this out):
If you are doing this on Windows and using absolute paths to your jar files, using:
c:/dirA/fileA.jar
As a path to a file wont work!.
If you use absolute paths make sure you add a leading slash '/' to all your paths:
/c:/dirA/fileA.jar
That one will work! :)
The leading slash (/) is used to prevent "c:" being parsed as a protocol specification. And this is because according to the jar file specification the Class-Path attribute:
The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs.
This means you could also use:
file://c:/dirA/fileA.jar
And will work too.