praveen
praveen

Reputation: 81

bean validation- hibernate error

I am getting following exception when trying to run my command line application:

java.lang.ExceptionInInitializerError
        at org.hibernate.validator.engine.ConfigurationImpl.<clinit>(ConfigurationImpl.java:52)
        at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:43)
        at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -2
        at java.lang.String.substring(String.java:1937)
        at org.hibernate.validator.util.Version.<clinit>(Version.java:39)
        ... 34 more

Am I doing anything wrong? Please suggest.

Upvotes: 1

Views: 2690

Answers (2)

Pascal Thivent
Pascal Thivent

Reputation: 570615

This is strange. I pasted the relevant parts of the static initialization block of o.h.v.u.Version in a class with a main and added some poor man's logging traces:

public class VersionTest {
    public static void main(String[] args) {
        Class clazz = org.hibernate.validator.util.Version.class;

        String classFileName = clazz.getSimpleName() + ".class";
        System.out.println(String.format("%-16s: %s", "classFileName", classFileName));

        String classFilePath = clazz.getCanonicalName().replace('.', '/') + ".class";
        System.out.println(String.format("%-16s: %s", "classFilePath", classFilePath));

        String pathToThisClass = clazz.getResource(classFileName).toString();
        System.out.println(String.format("%-16s: %s", "pathToThisClass", pathToThisClass));

        // This is line 39 of `org.hibernate.validator.util.Version`
        String pathToManifest = pathToThisClass.substring(0, pathToThisClass.indexOf(classFilePath) - 1)
            + "/META-INF/MANIFEST.MF";
        System.out.println(String.format("%-16s: %s", "pathToManifest", pathToManifest));
    }
}

And here the output I get when running it:

classFileName   : Version.class
classFilePath   : org/hibernate/validator/util/Version.class
pathToThisClass : jar:file:/home/pascal/.m2/repository/org/hibernate/hibernate-validator/4.0.2.GA/hibernate-validator-4.0.2.GA.jar!/org/hibernate/validator/util/Version.class
pathToManifest  : jar:file:/home/pascal/.m2/repository/org/hibernate/hibernate-validator/4.0.2.GA/hibernate-validator-4.0.2.GA.jar!/META-INF/MANIFEST.MF

In your case, the StringIndexOutOfBoundsException: String index out of range: -2 suggests that:

pathToThisClass.indexOf( classFilePath )

is returning -1, making the pathToThisClass.substring(0, -2) call indeed erroneous.

And this means that org/hibernate/validator/util/Version.class is somehow not part of the pathToThisClass that you get. I don't have a full explanation but this must be related to the fact that you're using One-Jar.

Could you run the above test class and update your question with the output?

Upvotes: 1

axtavt
axtavt

Reputation: 242786

So, as you use One-JAR, the problem probably is in incompatibility between One-JAR and Hibernate Validator. However, in the latest version of One-JAR (0.97) it works fine, therefore use the latest version.

Upvotes: 0

Related Questions