14

I try to build JAR with a Class-Path element in the MANIFEST.MF. I get the elements of the class path from an outside source (Maven in my case). Since the paths are absolute and beyond my control, they can contain weird characters like spaces.

Since spaces are used to separate items in the class path, this path doesn't work:

Class-Path: C:\User\Some Odd Name\project\target\project-1.0.0.jar

How can I escape / encode odd characters / whitespace in items of the classpath in the JAR Manifest?

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820

2 Answers2

18

Elements in the Class-Path element are URLs, so the usual escaping rules apply (and you should use forward slashes as well):

Class-Path: /C:/User/Some%20Odd%20Name/project/target/project-1.0.0.jar

Note: The initial slash is necessary since C is not a valid network protocol (like http or ftp). If you were pedantic, it should be file:///C:/...

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I didn't know they are URLs. This is very helpful to me, as I need to split the list of URLs. I was wondering what to do about spaces in paths / JAR file names... now I know I can ignore. They will be represented using URL entities, e.g., (space) -> `%20` – kevinarpe May 29 '15 at 07:03
  • Good, however I found that for this kind of absolute path, also I had to include a '/' before the drive letter: `Class-Path: /C:/User/Some%20Odd%20Name/project/target/project-1.0.0.jar` – R.D. Alkire Mar 22 '17 at 19:31
  • @R.D.Alkire Correct since `C` is not a valid network protocol unlike, say `http` or `ftp`. To be very correct, it should be `file:/C:/...` but one could argue that the context (the manifest file) is a file, so the base URL is alreay `file://...` Fixed. – Aaron Digulla May 30 '17 at 13:30
0

For me the caret character was the only way to successfully escape the withespace in a Windows classpath:

Class-Path: C:\User\Some^ Odd^ Name\project\target\project-1.0.0.jar
marc
  • 977
  • 9
  • 14