Be aware that the sun.security.tools.JarSigner
class was written to be used as a command-line utility and wasn't designed to be called from Java code. As a result, the error handling is pretty abrupt: the code will simply print an error message to standard out and then call System.exit()
1.
This means that if you call the class from within your Java code and an error occurs when you try to sign a jar, the JVM running your code will simply stop. This may be fine depending on your situation, but if your code is long running or acting as a service, it's not so good.
It's therefore better to call the jarsigner tool using the ProcessBuilder as per clamp's comment. You can then call waitFor()
on the resulting Process object and check exitValue()
to see if the command was successful. getInputStream()
will let you read any error messages that were written to standard out if the operation fails.