0

I would like to use a 3rd party java library in my xpage application. I found some instructions on the Web, how jars can be used. Can anyone tell me what am I doing with Java code, which comes in a simple folder?

I would like to use JsonPath.

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
Reiner
  • 33
  • 7

1 Answers1

0

I'd like to preface my answer with "I'm sorry". This sort of thing gets more complicated than it should.

In the simple case of your question - how do I add a source-form Java project to my NSF - it's not too bad. I'd take the tack of creating a new source folder in Project Explorer, say "src-jsonpath" and importing the source from the filesystem. However, in this case, that won't work too well.

The trouble is that JsonPath has a good number of dependencies, which you would ALSO need to track down. It's available in Maven, but, since Designer doesn't know anything about Maven, there are some hoops.

The simplest variant of this technique I found was via https://stackoverflow.com/a/15484858/858171, which suggests a two-step process for downloading the library and its various dependencies without having to create an actual Maven project. It (and I will now) assumes you have Maven set up on your system - if you don't, you can find sort-of-okay instructions on maven.apache.org.

With the other major assumption that you're on or can use a Mac or other Unix-alike system, these two lines should download the libraries to the current directory:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=com.jayway.jsonpath:json-path:2.0.0:jar -DoutputDirectory=.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=com.jayway.jsonpath:json-path:2.0.0:pom -DoutputDirectory=.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy-dependencies -f json-path-2.0.0.pom -DoutputDirectory=.

The first line actually retrieves the jar for the JsonPath library. The second then retrieves its pom file (project descriptor) - this file isn't useful for your needs directory, but is needed for the next step. The third line then uses this pom file to download the project's dependencies (and its dependencies' dependencies) to the current directory.

Once you've done this, you'll end up with a pile of jar files that you can then either put into the NSF or (probably better, with this many) onto the filesystem. The best route would be to turn them into an XSP OSGi library, but that's a larger topic that I should blog about eventually.

Community
  • 1
  • 1
Jesse Gallagher
  • 4,461
  • 13
  • 11
  • Thanks for the comprehensive answer. In the meantime, I have also found the corresponding [Maven-repository](http://mvnrepository.com/artifact/com.jayway.jsonpath/json-path) that provides the jar files and the references to the corresponding dependencies. I have also tried to manually take over the jars in my NSF. But my tests triggers a 403 error. So I have something forgot ... I need to take a closer look at Maven. Since it will probably take some time until I have done that, I decide for your answer, because it pushed me in the right direction. Looking forward to your next blog post! – Reiner Aug 21 '15 at 13:08
  • If you want to wrap the jars in an OSGi plugin then I have written a couple of articles about how to do that :-) http://www.dalsgaard-data.eu/blog/wrap-an-existing-jar-file-into-a-plug-in/. Agree with Jesse that that is a much better approach. – John Dalsgaard Aug 23 '15 at 08:44
  • I had overcome the first hurdle: Maven installed, and set the required settings. Proxy configured. With the supplied commands I get the dependent jars! Great! And there are of course more than the ones I had even found. Unfortunately, I have still the 403 error. Do I have to set some security settings to work with external jars? – Reiner Aug 24 '15 at 14:24
  • Not inherently, but it's possible that a given library may do something that requires security changes (often reflection). If you don't have the XPages Log Reader from OpenNTF on your server, I recommend it highly for getting info about errors. – Jesse Gallagher Aug 24 '15 at 20:24
  • I've got it. As expected, after I changed the java.policy file (permission java.security.AllPermission;), my test code runs fine. Now I'll try to pack the jars in a OSGI plugin to work around the security problem. Thanks to everyone for the tips! – Reiner Aug 25 '15 at 13:44