Import class from external jar into a java snippet

Preface:

I want to iterate over a list of names and build http request from them.

The names contain spaces and language specific vocabulary.

Since i do not know any better, i build strings with the pre-and suffix requirements and the names in the middle.

This string is yet not a valid http-URI. To make such an uri i want to build a String and then use the node String to Uri.

Actual problem:

To incorporate all the special character stuff, i want to use the class URIUtil from the Library Apache commons-httpclient.

However i fail to import the httpclient class properly into a Java Snippet node.

How do i add the http-client jar (latest version) and then import the class in a Java Snippet node?

 

Same problem, I want to use a math library : Apache Commons Math 3.6.1

I added the .jar to the external libraries thumbnail.

However the import fails :

WARN  Java Snippet         0:141      Compile with errors:
Error: The import org.apache.commons.math cannot be resolved

 

Any direction would be appreciated

Cheers, 

Nicolas

+1. Unable to use apache commons stuff or Joda time. Spent countless hours doing stuff the long way just because I haven't figured out how to use libraries that arent already included. Would be a huge timesaver to have this easily done inside knime. Something like pasting a link to the jar and clicking add, would be lovely!

 

Cheers

No issues here. I'm attaching an example using the Apache Commons Math lib, but I've recently used different libraries as well without problems.

My usual steps:

  1. Add the Java Snippet node to the workflow
  2. Open configuration
  3. Go to "Additional Libraries", add the jar file (in case you want the workflow to be portable, add the JAR file into the workflow and specify a "KNIME URL")
  4. Go to "Java Snippet" tab, import the classes in the "Your custom import" section, e.g. write "import org.apache.commons.math3.fraction.Fraction;" (the editor should provide you with auto completion on ctrl+space)
  5. Write code

In case your imported classes are not available, I would probably suggest inspecting the added JAR file first by unzipping it and verifying that it contains .class files and a META-INF directory with a MANIFEST.MF file:

$ unzip -d commons-math3-3.6.1 commons-math3-3.6.1.jar
$ tree commons-math3-3.6.1 
commons-math3-3.6.1
├── META-INF
│   …
│   ├── MANIFEST.MF
│   …
├── …
└── org
    └── apache
        └── commons
            └── math3
                ├── Field.class
                ├── …

-- Philipp

2 Likes