Xero-Java
Xero-Java copied to clipboard
java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl in IdentityApi.getConnections()
SDK you're using (please complete the following information): 4.21.0
Describe the bug While trying to authenticate a Custom Connection, using the sample code, I get the following exception:
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:154)
at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:121)
at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:96)
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:120)
at com.xero.api.client.IdentityApi.getConnectionsForHttpResponse(IdentityApi.java:245)
at com.xero.api.client.IdentityApi.getConnections(IdentityApi.java:207)
at nz.co.xxx.yyy.XeroUtils.authenticateWithXero(XeroUtils.java:60)
at nz.co.xxx.yyy.BalanceFetcher.start(BalanceFetcher.java:72)
at nz.co.xxx.yyy.Main.main(Main.java:39)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:390)
at java.base/java.lang.Class.forName(Class.java:381)
at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:111)
at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:209)
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:136)
... 8 more
To Reproduce Code I used:
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String TOKEN_REQUEST_URL = "https://identity.xero.com/connect/token";
public static final TokenStorage XERO_ERROR = null;
public static TokenStorage authenticateWithXero(XeroCredentials credentials, boolean verbose)
{
if (verbose)
System.out.println("Authenticating with Xero... ");
// The scopes that we will need
ArrayList<String> scopeList = new ArrayList<String>();
scopeList.add("accounting.transactions.read");
new NetHttpTransport();
try {
ClientCredentialsTokenRequest tokReq = new ClientCredentialsTokenRequest(HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl(TOKEN_REQUEST_URL));
BasicAuthentication basicAuth = new BasicAuthentication(credentials.getClientId(), credentials.getClientSecret());
TokenResponse tokenResponse = tokReq.setScopes(scopeList)
.setClientAuthentication(basicAuth)
.execute();
if (Build.isDebug)
System.out.println(" Access token: " + tokenResponse.getAccessToken());
ApiClient defaultIdentityClient = new ApiClient("https://api.xero.com", null, null, null, null);
IdentityApi idApi = new IdentityApi(defaultIdentityClient);
List<Connection> connection = idApi.getConnections(tokenResponse.getAccessToken(),null);
// ... snip...
}
catch (TokenResponseException e) {
if (e.getDetails() != null) {
System.err.println("Error: " + e.getDetails().getError());
if (e.getDetails().getErrorDescription() != null) {
System.err.println(e.getDetails().getErrorDescription());
}
if (e.getDetails().getErrorUri() != null) {
System.err.println(e.getDetails().getErrorUri());
}
} else {
System.err.println(e.getMessage());
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
return XERO_ERROR;
}
After scratching my head for a while, I found a workaround by including the following dependency:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
My POM file before the workaround was as follows (client details removed for privacy):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nz.co.xxx</groupId>
<artifactId>Yyy</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>nz.co.xxx.yyy.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>com.oracle.database.jdbc:ojdbc8</artifact>
<includes>
<include>**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.auth/google-auth-library-oauth2-http -->
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.xeroapi/xero-java -->
<dependency>
<groupId>com.github.xeroapi</groupId>
<artifactId>xero-java</artifactId>
<version>4.21.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.7.0.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>```
Ok
O