Import class from external jar into a java snippet

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