org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class

Tags: #<Tag:0x00007fa3ea17cb00>

I am upgrading a module to depend on 1.11.6 and it is not able to load annotated beans. I have read that a possible cause is java 8 and I have downgraded to java 7 in vain. The code however compiles well with version 1.9.9. The snipped below shows the code that runs into error which can be found at https://pastebin.com/wV4814fa. I will appreciate any help.

public synchronized void refresh() {
		calculationClasses.clear();
		flagCalculationClasses.clear();

		ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
		scanner.addIncludeFilter(new AssignableTypeFilter(PatientCalculation.class));

		for (BeanDefinition bd : scanner.findCandidateComponents("org.openmrs.module")) {
			try {
				Class<? extends PatientCalculation> clazz = (Class<? extends PatientCalculation>) Context.loadClass(bd.getBeanClassName());
				calculationClasses.put(bd.getBeanClassName(), clazz);

				if (PatientFlagCalculation.class.isAssignableFrom(clazz)) {
					flagCalculationClasses.add((Class<? extends PatientFlagCalculation>) clazz);
				}

			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

			log.debug("Registered calculation class " + bd.getBeanClassName());
		}
	}

@dkayiwa @darius @k.joseph @wyclif

What does java -version give you?

I run java 7.

java version “1.7.0_95” OpenJDK Runtime Environment (IcedTea 2.6.4) (7u95-2.6.4-3) OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)

I am noting something very interesting. Even though my java version is 1.7, the module is kind of compiled using oracle java 8. Compiling the module with -X switch produces the following:

[DEBUG] properties used {uiframeworkVersion=3.3.1, env.DESKTOP_SESSION=ubuntu, env.OLDPWD=/home/antony/workspace/KenyaEMR/Kenya_EMR/kenyacore, file.encoding.pkg=sun.io, java.home=/usr/lib/jvm/java-8-oracle/jre, env.QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1, env.GDM_LANG=en_US, env.DISPLAY=:0, 

I am still looking at it and will share once I get a solution

Finding: I needed to change javac version as well when I switched between java versions. The compiler still pointed to java 8 even though java version was 7. Switching the compiler version sorted me.

THanks

Would be great if you sent the finding as a reply because edits do not send notifications.

Explaining how you switched the compiler version will also help those who may not know how.

The solution to the problem was to change javac version as well when I switched between java versions. The compiler still pointed to java 8 even though java version was 7. Switching the compiler version sorted me.

To update javac use:

`sudo update-alternatives --config javac` 

and specify the version of interest, in my case java 7. I hope this helps.

1 Like

Hi @dkayiwa

It seems I’m encountering an error similar to the one reported in this thread. At-least I know that this error is related to the Java compiler version but failed to figure out how to get over it.

Like I reported in Patient Conditions and Encounters, we need this feature in HFE.

So I created a new sub module(api-2.3) and this depends on core 2.3.1-SNAPSHOT. I’m aware that java 8 support is available when you depend on core 2.x and above,

Adding the compiler plugin pointing to 1.8 source to api-2.3 makes spring fail to build the context in the omod sub module:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.1</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
  </configuration>
</plugin>

Error:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
  Failed to read candidate component class: file [~/openmrs-module-htmlformentry/api-2.3/target/classes/org/openmrs/module/htmlformentry/ConditionElement.class];
  nested exception is java.lang.ArrayIndexOutOfBoundsException: 32364

Here is the full error log: https://pastebin.com/N0Yuamf0


NB: Here is the commit that has changes you want to look at - https://github.com/samuelmale/openmrs-module-htmlformentry/commit/edf722fe4f77fe6c65e8354e5f5e5ca2212ad953

It’s true that if I forget about using Java 8 compatible code, the error is gone

cc: @mksd, @ibacher