I have a jar that uses some of the apache tomcat library jars and thus has the following manifest.
Manifest-Version: 1.0
Main-Class: foo
Class-Path: foo.jar foo_utils.jar . /opt/apache-tomcat/
lib/websocket-api.jar /opt/apache-tomcat/lib/tomcat7-websocket.jar /o
pt/apache-tomcat/lib/tomcat-util.jar /opt/apache-tomcat/lib/tomcat-co
yote.jar /opt/apache-tomcat/bin/tomcat-juli.jar
The jar is able to find foo.jar and foo_utils.jar without a problem.
It can't find any of the jars in the /opt/apache-tomcat/lib folder. I have checked the path and made sure that it is a valid path to the jars I want and is.due to the ant build puts a new line at the end of the classpath last line and thus messes with the line being parsed properly.
How may I have ant not put a new line at the end of my manifest class path file?
The JAR File Specification says (emphasis mine):
Class-Path :
The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs.
The presence of a newline at the end of the MANIFEST.MF is not relevant.
Can you check if your manifest file is ending with a new line or carriage return? As per https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html - the last line will not be parsed properly if it does not end with a new line or carriage return.
I'm not sure but that dot between foo_utils.jar and /opt/apache-tomcat/ seems strange to me. Is there a reason for this dot?
Related
I have one .jar file with only MANIFEST/MANIFEST.MF file inside it. I want to extend classpath in this file. Already googled on how to do this and found this:
Extract MANIFEST/MANIFEST.MF - jar xf myfile.jar
MANIFEST/MANIFEST.MF
Edit manifest file
Update it inside .jar - jar umf myfile.jar MANIFEST/MANIFEST.MF
The problem is that I get this error:
java.io.IOException: line too long
at java.util.jar.Attributes.read(Attributes.java:379)
at java.util.jar.Manifest.read(Manifest.java:199)
at java.util.jar.Manifest.<init>(Manifest.java:69)
at sun.tools.jar.Main.update(Main.java:652)
at sun.tools.jar.Main.run(Main.java:271)
at sun.tools.jar.Main.main(Main.java:1288)
Google says that maximum line length of manifest is 72 - OK. Tried not to edit extracted manifest file. When I try to import the same file I just extracted I get the same error. Maybe there's a tool that formats manifest files in order to make them "valid"?
Thank you in advance.
The update command should be:
jar umf MANIFEST/MANIFEST.MF myfile.jar
Use the below steps to avoid the issue
(1) use separate lines, to avoid too long a line for java package name lists
(2) (Space at the end of each line and one at the beginning) . Type a preceding space before each following lines, for example: Class-Path: ...jar ...jar ...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?
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
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.
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.
When I type jar cvfm file_name.jar manifest.txt *.class in command prompt I get this error:
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:410)
at java.util.jar.Manifest.read(Manifest.java:199)
at java.util.jar.Manifest.<init>(Manifest.java:69)
at sun.tools.jar.Main.run(Main.java:172)
at sun.tools.jar.Main.main(Main.java:1177)
I've never gotten this error before and I can't find anything on it, what does it mean?
Be careful about the order of the parameters:
1) jar cvmf manifest.txt some.jar package/*class
2) jar cvfm some.jar manifest.txt package/*class
Check the name of a header variable in the MANIFEST file. MANIFEST file is not correct.
This tutorial will help to identify the MANIFEST file format and related things, http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
In case you land here and tried everything and still isn't getting rid of the problem, take a look if there are any accidental tabs instead of 4 spaces for indentation in the MANIFEST file.
I was using a maven pom.xml file to auto-generate the MANIFEST and the property line was too big, spanning multiple lines like this:
<Extension-List> item1 item2 item3 item4
item5 item6 item7 item8 <--- these lines are idented with tabs
item9 item10 item11 item12 </Extension-List>
this was corrupting the MANIFEST file in a very hard to see way.
Your Manifest file has to follow the required format. If you're generating the file yourself, then you should be able to figure out where you got the syntax wrong. If the manifest file was generated by some tool or process, however, you'll have to check the tool's documentation to see whether you ran into a bug in the tool.
Read carefully the first line of your manifest, for example :
Main-Class: main.HelloWorld
I had this error because I added a space like this "Main-Class :"
Maybe it is something similar.
My problem was that I had loads of dependencies on one line so I split it up using a simple Find and Replace where each JAR was on a separate line. It turns out that if you have something like:
Class-Path: dependencies/org.apache.commons.cli_1.4.jar (note the space at the end)
dependencies/org.apache.log4j_1.2.15.v201012070815.jar (note the space at the end)
You need to make sure that the line doesn't end with a space, and that the new line begins with a space, like so:
Class-Path: dependencies/org.apache.commons.cli_1.4.jar
dependencies/org.apache.log4j_1.2.15.v201012070815.jar
This problem can also occur if your MANIFEST.MF file begins with some non-printable characters. Tools like notepad and wordpad don't let you see them, so you'll swear you're file looks right and that all your headers are valid.
In my case I got the message:
java.io.IOException: invalid header field name: Built-By
The clue if you are having this situation are those characters occurring before the valid Built-By header. Those are non-printable characters that somehow got into your MANIFEST.MF file. The other clue is to open a command line, navigate to your MANIFEST.MF file and do
>cat MANIFEST.MF
□Build-BY: TSRUT
Bundle-Description: Fragment Controller.
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Notice that box character before your 1st header at the beginning of the file. If that's the case, this is the solution to your problem:
Open up your favorite editor, manual select the entire file (do not use the select-all command, as that will also select those hidden characters). Close the file. Open a new instance of your editor, paste your content, and save the new file back to your original MANIFEST.MF file. Select yes to overwrite. Then verify that those hidden character were removed from the beginning of the file.
if your manifest file is having some additional empty lines will also cause this error . Make sure to remove the additional lines.
I get: Could not find the main class: org.dav.kin.Tester. Program will exit. when I attempt to run my jar file via java -jar tester.jar or java -classpath tester.jar org.dav.kin.Tester Does anyone know what is wrong and how to fix it? Below are additional details. Thanks.
Manifest File:
Manifest-Version: 1.0
Created-By: DKin
Class-Path: .
Main-Class: org.dav.kin.Tester
jar tf tester.jar
org/
org/dav/
org/dav/kin/
org/dav/kin/Tester.class
org/dav/kin/TesterCellRenderer.class
...
...
META-INF/
META-INF/MANIFEST.MF
UPDATE:
Jar file runs if I specify the system classpath, which contains the groovy-all-{version}.jar, like so: java -classpath tester.jar;"%CLASSPATH%" org.dav.kin.Tester Anyone know why I have to explicitly re-state the classpath (or more precisely, the groovy jar)?
Your jar file lacks a file with this name
/org/dav/kin/Tester.class
or you have special characters in your MANIFEST.MF file
MANIFEST.MF files have a particular syntax. It's best to use other tools to generate them; however some of the details I've encountered which increases the success of hand written files include:
Always make sure the lines are less than 72 characters long.
Always use \r\n (windows newline), even on non-windows systems.
Verify that all whitespace characters are spaces.
Verify that there are no nonprintable characters (htab, etc).
Sometimes a blank line at the end of the file helps.
Is Tester.class' package declaration org.dav.kin?
You have indicated that the you are using Groovy. Groovy does compile down to Java class files but it still requires the groovy runtime libraries. You need to make sure groovy is on the classpath as well as your classes. Try this:
java -classpath tester.jar;groovy-all-1.8.0.jar org.dav.kin.Tester
Just in case. I just solve exactly the same problem.
Instead of
Class-Path: .
in MANIFEST.MF
one should enumerate (with space) jars which are required in runtime, so it should be something like this:
Class-Path: groovy-all-2.4.5.jar relative/my-dependent-project-artifact.jar
When i use this cmd line :
jar cmf arshad.mf ars.jar *.class
i get this error :
invalid header field name:Manifest-version
This is my manifest file :
Manifest-Version: 1.0
Main-Class:t
i made the manifest file with notepad in UTF-8 encoding - is there any problem with the manifest ?
Add a space after the colons:
Manifest-Version: 1.0
Main-Class: t
Yes,it is true.
A common mistake people make when writing their manifest files for jar's is that they don't put spaces after their colons. I don't know, based on what you wrote here, if that's it or not but give it a try.
Example:
Main-Class:someClass //wrong
Main-Class: someClass //correct
Also, your manifest file must be saved as UTF-8. You're not necessarily safe writing it on MS Notepad and saving as UTF-8 encoding. This post has some good details:
Invalid Header Field Name when adding manifest to JAR using Eclipse
Notepad adds some bytes onto the front to broadcast the endian-ness of the data. This will break the jar command. A decent solution is to not use Notepad and download Notepad++. In Notepad++ you select 'encoding' from the top bar, and select 'UTF-8 Without BOM'. Saving your manifest file with this setting applied should solve the problem. If there are no others.