0

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?

Micho
  • 3,929
  • 13
  • 37
  • 40
Ray
  • 477
  • 1
  • 6
  • 18
  • I don't know the framework, but shouldn't you set the ServiceProvider yourself? What does ServiceProvider.available() return? Reading from http://unitsofmeasurement.github.io/unit-api/site/apidocs/javax/measure/spi/ServiceProvider.html#available() the current() will throw the IllegalStateException when there is no current Provider. – Rens Groenveld Apr 24 '17 at 08:39
  • As i saw in :[link](http://stackoverflow.com/questions/43308544/jsr-363-formating-a-volume-unit-in-decilitre), it just calls ServiceProvider.current().getUnitFormatService().getUnitFormat() and the code works fine, and as i debug the code, it seems that it will use ServiceLoader.load(ServiceProvider.class) to load a ServiceProvider itself. – Ray Apr 24 '17 at 08:45
  • Can you please share the error stacktrace – Saurabh Jhunjhunwala Apr 24 '17 at 08:56
  • @Saurabh, It is like this:Exception in thread "main" java.lang.IllegalStateException: No measurement ServiceProvider found. at javax.measure.spi.ServiceProvider.current(ServiceProvider.java:157) at UnitOfMeasure.com.sap.kfp.App.main(App.java:18) – Ray Apr 24 '17 at 09:07
  • is that the beginning of the error... or is that the only error – Saurabh Jhunjhunwala Apr 24 '17 at 09:27
  • its the only error – Ray Apr 24 '17 at 09:37

3 Answers3

1

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();
}
ibiG
  • 11
  • 2
0

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.

Rens Groenveld
  • 960
  • 11
  • 28
  • yes i can tell that i didnt put ServiceProvider to the right use. Thanks for your guidance. – Ray Apr 24 '17 at 09:13
  • No problem. See http://stackoverflow.com/questions/3420715/where-to-put-serviceloader-config-file-in-a-web-app for an example of the config file. – Rens Groenveld Apr 24 '17 at 09:15
  • By the way, I think you are using ServiceProvider fine, it's just the ServiceLoader that needs configuration. – Rens Groenveld Apr 24 '17 at 09:16
0

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...

Ray
  • 477
  • 1
  • 6
  • 18