Universal Robots+

How to use the third-part .jar files?

Hello:
In my URCAPS,I want to use the third-part .jar files. e.g. seroUtils.jar and modbus4j.jar.
When I compile the project , it was compiled successed.But when I add it in POLYSCOPE,before the URCAP,there’s a yellow “!”.
How can I solve this problem. Thank you!:sob:

1 Like

@tonyke

Do you place the 3rd party .jar’s inside the resources folder?

Now ,I meet the same problem,can you give me some tips?Thanks.

@demonwind you need to edit your POM file. In the org.apache.felix plugin, avoid importing the jar file and embed it, something like

< Import-Package>
!your-jar, *
< /Import-Package>
< Embed-Dependency>your-jar< /Embed-Dependency>

If everything goes well, the jar file should be inside the urcap file once you compile it, and Polyscope shouldn’t complain about it.
EDIT: I used spaces in the XML tags to be able to display the message in the forum.

com.ur.urcap.api*;version="[1.0.0,2.0.0)", *

Is that right?

You can leave that, but don’t import your third party jar. You can leave it out by writing !your-jar inside the “Import-package” label.

Is there a concrete example somewhere? I could really use one. I added via maven xstream-1.3.1 where I inserted the dependency i needed shown below in the pom.xml:

   <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.3.1</version>
    </dependency>

	<dependency>
		<groupId>org.osgi</groupId>
		<artifactId>org.osgi.core</artifactId>
		<version>4.3.0</version>
	</dependency>

	<dependency>
		<groupId>com.ur.urcap</groupId>
		<artifactId>api</artifactId>
		<version>1.1.0-69</version>
		<scope>provided</scope>
	</dependency>

My import looks like this:

	<Import-Package>
	     com.ur.urcap.api*;version="[1.1.0,2.0.0)",
         *
	</Import-Package>

Shouldn’t the * asterisk mean that it will add all dependencies?, I tried following the responses above, but to no prevail, so I would really appreciate an example thanks!

The problem with that asterisk (as far as I understand) is that it will tell the OSGi framework that all those dependencies are already installed and available in the classpath, which is not the case. If you take a look at where you installed the simulator, you should find the jars corresponding to urcap.api somewhere (look in GUI/bundle).

You could try two things:

  • Paste those jars (your dependencies) in that directory too, leaving the POM as is and see what happens (I never tried that personally, but maybe it works)
  • Try something like this:
                        < Import-Package>
                            com.ur.urcap.api*;version="[1.1.0,2.0.0)", org.osgi.*, javax.*
                        < /Import-Package>
                        <!-- This is a workaround to create a manifest that is accepted by URSIM -->
                        < Embed-Dependency>com.thoughtworks.xstream.*< /Embed-Dependency>

Then, add the maven-assembly-plugin to the POM:

            < plugin>
                < artifactId>maven-assembly-plugin</artifactId>
                < configuration>
                    < descriptorRefs>
                         < descriptorRef>jar-with-dependencies</descriptorRef>
                    < /descriptorRefs>
                < /configuration>
                < executions>
                    < execution>
                        < phase>package</phase>
                        < goals>
                            < goal>single</goal>
                        < /goals>
                    < /execution>
                < /executions>
            < /plugin>

When you run mvn install, you should see a file with the classes of your dependency packed inside, and the usual .urcap file with the correct manifest. Get the manifest from the .urcap file into the jar with dependencies, change the extension to .urcap, and try using it. I did that and it works (you can use something to automate extracting the manifest from one file to the other and changing the extension).

2 Likes

It works like charm using your instructions, I ended up using Gson instead,
To import I added:

    <Import-Package>
		com.ur.urcap.api*;version="[1.1.0,2.0.0)", !com.google.gson.*,*
	</Import-Package>
    <Embed-Dependency>com.google.gson.*</Embed-Dependency>

To dependencies I added:

<dependency>
		<groupId>com.google.code.gson</groupId>
		<artifactId>gson</artifactId>
		<version>1.7.1</version>
</dependency>

dhv,

were the jar files added automatically into the .urcap file or did you have to add them manually into the recourse folder before building? I tried your suggestion by editing the <Import-Package>, <Embed-Dependency> and <dependency> tag, but the needed classes are still missed in my .urcap file. Can you explain the steps that need to be done in order to use 3rd-party jars?

Thanks a lot!

you need to add maven-assembly-plugin to the pom.xml as jubeira described above.
run mvn install
it will create the folder called target.
inside the folder is now created a new jar file named: {URCapName and version}-SNAPSHOT-jar-with-dependencies.jar

in the target folder copy the file: /classes/META-INF/MANIFEST.MF
and replace it with the META-INF/MANIFEST.MF of the {URCapName and version}-SNAPSHOT-jar-with-dependencies.jar.
To replace you can unzip the jar file and zip it again. remember to rename the extension to .urcap,
then you should be able to load it on the URSim
And yes the things you add to the dependency should be automatically added to the jar file as long as maven knows the library.

I tried this too. After replacing the MANIFEST.MF in the with-dependencies.jar with the MANIFEST.MF from the /target/classes/META_INF folder I changed the .jar extension into .urcap. While installing it in Polyscope a .jar file is being created in the hidden folder ursim/.urcaps and a warning occurs in the terminal of Polyscope with the following information:

Failed to install artifact: /home/ubuntu/Downloads/ursim-3.4.3-77/.urcaps/com.mycompany.myurcap.jar: org.osgi.framework.BundleException: The bundle file:/home/ubuntu/Downloads/ursim-3.4.3-77/.urcaps/com.mycompany.myurcap.jar does not have a META-INF/MANIFEST.MF! Make sure, META-INF and MANIFEST.MF are the first 2 entries in your JAR!.

I checked the .jar file in the /.urcaps folder and it definitely has a MANIFEST.MF (identical to the one from target/classes/META_INF) inside the /META-INF folder.

Do you think the problem might be that these are not the FIRST 2 entries (alphabetically) in the jar file, because there is the /com folder before the the /META-INF folder? Inside the jar file are the folders /com, /META-INF, /org, /OSGI-OPT and two files about.html, LICENSE

Thank you for your help and patience!

No problem CBMIS
Try to see if you can Install the normal URCap file after mvn install, if not then you must fix that first.
I have the same folder structure as you have where com is first:
Selection_002
Pls. post your pom.xml.

I try to import the jersey package com/sun/jersey/api/client/Client. First I added the jersey *.jars into my resource folder, like jbm suggested. The jars got added the my *.urcap file in the /impl folder. I could install the urcap successfully but when using the urcap uses the code with the jersey client I get the error java.lang.NoClassDefFoundError: com/sun/jersey/api/client/Client.

Next I tried the maven-assembly-plugin, like described in my previous post, but this does not work either. The normal urcap installation works fine, but when the code with the jersey client needs to be executed the same error occurs java.lang.NoClassDefFoundError: com/sun/jersey/api/client/Client in the terminal.

In my urcap I also use pictures and the package javax.imageio.ImageIO is imported. I am wondering why this package is not throwing any errors because I did not specify it in the pom and this package is not included in the ursim simulator /ursim/GUI/bundle. So where are the packages hidden, that ursim is using?

Am I doing anything wrong? Here is my pom:

pom.xml.zip (2.1 KB)

Do you think my pom.xml might be the problem?

Hello CBMIS, I tried experimenting a bit

< dependency>
    < groupId>com.sun.jersey</groupId>
    < artifactId>jersey-client</artifactId>
    < version>1.19</version>
< /dependency>

this should build the jar file having com/sun/jersey

but when running it on URsim you will likely get this, error java.lang.NoClassDefFoundError: javax.net.ssl.HttpsUrlConnection, I didn’t look more into it but jersey depends on javax.net.*, so if you find a solution pls post it here.
I couldn’t get xstream working since it also needs some javax deps, so I found another library which didn’t need the deps.
cheers hope it helped a bit.

I got it to work. In order to add and use the package I did that:

  1. Add the package to dependencies:
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.8</version>
    </dependency>

  2. Add the package with an preceded exclamation mark to Import-Package:
    <Import-Package> com.ur.urcap.api*;version="[1.1.0,2.0.0)", !com.sun.jersey.api.*, *</Import-Package>

  3. Add the ArtifactId to Embed-Dependency:
    <Embed-Dependency>jersey-client</Embed-Dependency>

I did not manually add any jars to my resource folder or so. After a successful building I got an error while starting polyscope, that an additional package com.sun.jersey.core is missing. Apparently the jersey-client package has additional dependencies which need to be included as well. So I had to add jersey-core and javax.mail to dependencies and also to Embed-Dependency and then it worked. The required jar files got added automatically to the urcap file.

I hope this helps a bit :wink:

5 Likes