Ray
Ray

Reputation: 547

No measurement ServiceProvider found

I'm researching the unit of measure open source library, and the maven dependency I use is:

<dependency>
  <groupId>tec.units</groupId>
  <artifactId>unit-ri</artifactId>
  <version>1.0.2</version>
</dependency>  

which implements the JSR-363. When I try to use it as below:

ServiceProvider provider = ServiceProvider.current();

The result is:

Exception in thread "main" java.lang.IllegalStateException: No measurement ServiceProvider found.

Could anybody tell me what is wrong?

Upvotes: 0

Views: 326

Answers (3)

ibiG
ibiG

Reputation: 11

I faced with the same problem using the measure library in the java bean forms while opening beans within the Netbeans IDE. This trick works for me:

import javax.measure.spi.ServiceProvider;
import tec.units.ri.spi.DefaultServiceProvider;        

private ServiceProvider serviceProvider;    
try {
   serviceProvider = ServiceProvider.current();
} catch ( IllegalStateException e ) {
   serviceProvider = new DefaultServiceProvider();
}

Upvotes: 1

Ray
Ray

Reputation: 547

For everyone who may need to use this library. It is strange but after i change the maven dependency version from 1.02 to 1.01,no other change,it works fine. So , this should be a bug of this version...

Upvotes: 0

Rens Groenveld
Rens Groenveld

Reputation: 982

So, I have looked into the class ServiceProvider to see what the current() method does:

https://github.com/unitsofmeasurement/unit-api/blob/master/src/main/java/javax/measure/spi/ServiceProvider.java

You can see it uses the ServiceLoader to return a value. If you look at the documentation of the ServiceLoader you will see that you need a config file:

https://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html

A service provider is identified by placing a provider-configuration file in the resource directory META-INF/services. The file's name is the fully-qualified binary name of the service's type. The file contains a list of fully-qualified binary names of concrete provider classes, one per line. Space and tab characters surrounding each name, as well as blank lines, are ignored. The comment character is '#' ('\u0023', NUMBER SIGN); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8.

Upvotes: 0

Related Questions