'An unexpected error occurred while trying to open jar

I have a spring batch project exported as a runable Jar file, it has 4 main methods, only one main is working, the other main needs referenced jar jai-codec-1.1.3.jar, but when I put the jar in the classpath, the first main also could not working and it gives error message "An unexpected error occurred while trying to open jar"

here is my mainfest file:

Manifest-Version: 1.0 Class-Path: JARS/spring-jdbc-3.1.1.RELEASE.jar JARS/spring-context-3.2.3.RELEASE.jar JARS/log4j-1.2.14.jar JARS/spring-tx-3.2.3.RELEASE.jar JARS/spring-core-3.2.3.RELEASE.jar JARS/spring-beans-3.2.3.RELEASE.jar JARS/commons-logging-1.1.1.jar JARS/spring-aop-3.2.3.RELEASE.jar JARS/aopalliance-1.0.jar JARS/spring-expression-3.2.3.RELEASE.jar JARS/commons-dbcp-1.4.jar JARS/commons-pool-1.5.4.jar JARS/ojdbc6-11.2.0.3.jar JARS/commons-lang3-3.0.jar JARS/itextpdf-5.5.1.jar JARS/jdom2-2.0.5.jar JARS/jai-codec-1.1.3.jar

I found out that it is not because the specific jar file, it is because the length of the classpath is exceeded the limit, is there a way to put more jars in the MAINFEST file classpath?

also I have defined all @autowired service and component classes but it still not working in jar, it only works when I defined those as bean in spring-config.

Anyone can Help me to figure out why?



Solution 1:[1]

Problem solved. I am not sure if it is the limitation of classpath size in MAINFEST file, but it really matters when it was too long, I changed all jar name as 1, 2 , 3... and be able to put all jars in the classpath, and it works. Please leave your comments if you have different opinion on this, Thanks.

Solution 2:[2]

This error may indicate (in a very unclear way though) an error in MANIFEST.MF. This can be emulated by an invalid empty line in the middle of the file or by lines too long.

MANIFEST.MF is extremely manual-edit-unfriendly:

  • It must have a final empty line (or final line terminator in other words),
  • its lines must be 72 bytes (not chars, see the comment) long at most (I bet this was your true problem, you just fixed symptoms)
  • and continuation line must start with space...
  • and on top of it, the classpath entries must be URLs, not file paths.

More about its specification can be found here: https://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html

Solution 3:[3]

I will add that Properties in Manifest MUST NOT be dot separated, either dash or no separator: "myProperty" will work, "my-property" will work too, but "my.property" will fail with "Error: An unexpected error occurred while trying to open file ..." with no explanation at all. Although "my.property" would work as a Java property in some standalone application, it just breaks the launch of the embedded application inside a JAR when the Manifest is read.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3666635
Solution 2
Solution 3 Eric Lemaitre