Calling A Java Library From Pentaho Kettle - java

I have developed a java class exported into a .jar library that will be called by a Pentaho Kettle 'modified java script'. The .jar is compiled in Eclipse with JDC Compliance level 1.7.
When I try to use this class inside a 'modified java script', I get the error: ReferenceError: “xeroCallPackage” is not defined. I have tried lots of things without much luck so far.
My file xeroCallPackage.jar is in the path with the other *.jar files in Pentaho (..\data-integration\lib)
For info:
The stripped down (for simplicity) java library code is here:
package xeroCallPackage;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Map;
public class xeroURLCall {
public String getResponse(String CONSUMER_KEY, String CONSUMER_PRIVATE_KEY, String URL) throws IOException, OAuthException, URISyntaxException {
// stripped out code here
return response.readBodyAsString();
}
}
The stripped down Pentaho 'modified java script' is here:
var CONSUMER_KEY = "ffffff";
var CONSUMER_PRIVATE_KEY = "aaaaa";
var URL = "https://gggggggg.rrrrr.wwww";
var ResponseAsString;
ResponseAsString = new xeroCallPackage.xeroURLCall.getResponse(CONSUMER_KEY,CONSUMER_PRIVATE_KEY,URL);

You will either have to have org as top-most package or prefix the fully qualified name of your class with Packages.
So in your case the fully qualified name of your class that can be used for calling from Spoon will be Packages.xeroCallPackage.xeroURLCall
Apart from that the supplied JavaScript code won't work (but maybe that's just because of the stripped down code). You'd have to create a xeroURLCall object first and then call the getResponse method on that object:
var call = new Packages.xeroCallPackage.xeroURLCall(...);
var responseAsString = call.getResponse(CONSUMER_KEY,CONSUMER_PRIVATE_KEY,URL);

Related

GraalVM - embedding python multi-file project in java

I couldn't find a solution create a polyglot source out of multiple files in GraalVM.
What exactly I want to achieve:
I have a python project:
my-project:
.venv/
...libs
__main__.py
src/
__init__.py
Service.py
Example sourcecode:
# __main__.py
from src.Service import Service
lambda url: Service(url)
# src/Service.py
import requests
class Service:
def __init__(self, url):
self.url = url
def invoke(self):
return requests.get(self.url)
This is very simple example, where we've got an entry-point script, project is structured in packages and there is one external library (requests).
It works, when I run it from command-line with python3 __main__.py, but I can't get it work, when embedding it in Java (it can't resolve imports).
Example usage in java:
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import java.io.File;
import java.io.IOException;
public class Runner {
public static void main(String[] args) throws IOException {
Context context = Context.newBuilder("python")
.allowExperimentalOptions(true)
.allowAllAccess(true)
.allowIO(true)
.build();
try (context) {
// load lambda reference:
Value reference = context.eval(Source.newBuilder("python", new File("/path/to/my-project/__main__.py")).build());
// invoke lambda with `url` argument (returns `Service` object)
Value service = reference.execute("http://google.com");
// invoke `invoke` method of `Service` object and print response
System.out.println("Response: " + service.getMember("invoke").execute());
}
}
}
It fails with Exception in thread "main" ModuleNotFoundError: No module named 'src'.
The solution works for javascript project (having similar index.js to __main__.py, its able to resolve imports - GraalVM "sees" other project's files, but somehow it doesn't, when using python.
I found out, that python is able to run zip package with project inside, but this also doesn't work with GraalVM.
Is there any chance to accomplish it? If not, maybe there is a similar tool to webpack for python (if I could create a single-file bundle, it should also work).
Btw, I don't know python at all, so I may missing something.
Thanks for any help!

Using VC++ dll using JNA

I am using a BTICARD.DLL, which is the dll of Arinc429 card. I need to write wrapper class in Java for the functions like BTICard_CardOpen for example.
I Had written an interface below BTICardAPI.java:
package NLIPjt;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.Native;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public interface BTICardAPI extends StdCallLibrary {
BTICardAPI INSTANCE = (BTICardAPI) Native.loadLibrary("BTICARD", BTICardAPI.class);
int BTICard_CardOpen(Pointer LPHCARD, int cardnum);
}
and my Java implementation prog
BTICardTest.java:
package NLIPjt;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public class BTICardTest {
public static void main(String args[]) {
BTICardAPI BTI1 = BTICardAPI.INSTANCE;
int iErr;
int CardNo = 0;
Pointer CardHandle = null;
iErr = BTI1.BTICard_CardOpen(CardHandle, CardNo);
System.out.println("Error Value: " + iErr);
}
}
i get the following error in netbeans IDE:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'BTICard_CardOpen': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:245)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:566)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:542)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:528)
at com.sun.jna.Library$Handler.invoke(Library.java:228)
at com.sun.proxy.$Proxy0.BTICard_CardOpen(Unknown Source)
at NLIPjt.BTICardTest.main(BTICardTest.java:14)
Looking for a solution!!
According to the documentation you need to make the library available. There are three ways to do this.
Make your target library available to your Java program. There are
several ways to do this:
The preferred method is to set the jna.library.path system property to
the path to your target library. This property is similar to
java.library.path, but only applies to libraries loaded by JNA.
Change the appropriate library access environment variable before
launching the VM. This is PATH on Windows, LD_LIBRARY_PATH on Linux,
and DYLD_LIBRARY_PATH on OSX.
Make your native library available on your classpath, under the path
{OS}-{ARCH}/{LIBRARY}, where {OS}-{ARCH} is JNA's canonical prefix for
native libraries (e.g. win32-x86, linux-amd64, or darwin). If the
resource is within a jar file it will be automatically extracted when
loaded.

How to import packages in java [duplicate]

This question already has answers here:
How to use 3rd party packages in Java
(3 answers)
Closed 6 years ago.
I am new in Java programming language and i want to use a library by importing their packages . Can anyone tell me how can i import packages in Java using text editor?
I found this library in github and i wanted to use their packages for my java code i am developing by using import. I tried just to call these packages on my code by using import but in compiler there was an error which states: packages not found.
import com.tiemens.secretshare.main.cli.*;
import com.tiemens.secretshare.main.cli.*;
import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.min;
import static java.util.Arrays.copyOfRange;
public class Shamir {
//The encoding that will be used when splitting and combining files.
static String encoding = "ISO-8859-1";
//The number of bytes per piece (except maybe the last one)!
static int pieceSize = 128;
//Mode 0 for strings, 1 for ints.
public static ArrayList<String> shamirSplit(String inputString, int numPieces, int minPieces, int mode) {
String type = "-sS";
if (mode == 1) {
type = "-sN";
}
ArrayList<String> parts = new ArrayList<>();
String[] splitArgs = {"-n", Integer.toString(numPieces), "-k", Integer.toString(minPieces), type, inputString, "-primeNone"};
MainSplit.SplitInput splitInput = MainSplit.SplitInput.parse(splitArgs);
MainSplit.SplitOutput splitOutput = splitInput.output();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
splitOutput.print(ps);
String content = baos.toString(); // e.g. ISO-8859-1
BufferedReader reader = new BufferedReader(new StringReader(content));
String line;
int i = 0;
try {
while ((line = reader.readLine()) != null && i < numPieces) {
if (line.startsWith("Share (x")) {
i++;
parts.add(line.trim());
}
}
} catch (Exception e)
So my class i want to implement is Shamir class but i need to import com.tiemens.secretshare.main.cli.*;
Can anyone tell me how to make this package work for my Shamir class?
I am guessing you aren't using maven. Download the jar files for packages you want to import and put then in your build path
If I am not mistaken, when I do what you did:
import com.tiemens.secretshare.main.cli.*;
public class Foo {
}
and then try to compile using javac Foo.java, I get:
Error:(2, 1) java: package com.tiemens.secretshare.main.cli does not exist
This means that when the compiler javac is trying to compile your class (Shamir.java) it needs either the source files or the bytecode (class files) for the classes in the package com.tiemens.secretshare.main.cli. Since you seem to have neither, the compilation fails.
Thus, you need the jar file that contains the classes they you want to compile against. There are two ways to achieve this:
Use Maven. But that means you need to learn Maven. That's life. Use mvn repo to compile against.
If you think it is too much of work to learn Maven, you will need to build secretshare code on GitHub yourself. This means you will need to learn gradle. Again, that's life.
Too bad you couldn't download the JAR file as a "release download" for this project.
Download the jar for your library and include it in your project classpath. Then you can import it in your class.
For setting the classpath use this link

Java: Illegal start of expression

I'm just learning Java and wish to use the Path object:
Path file = ...;
And it's giving me: "Illegal start of expression"
I have the following imports:
import java.nio.file.*;
import java.nio.file.attribute.*;
I am running JDK 1.7 platform (JDK 7) according to NetBeans. Have googled to the end of earth and cannot find squat on this error.
I'm assuming path file = ...; is some new syntax or feature that my current JDK isn't recognizing???
EDIT |
import javax.swing.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class MainWindow extends JFrame {
public MainWindow()
{
initComponents();
}
private void cboModelFocusGained(java.awt.event.FocusEvent evt)
{
Path file = ...;
}
}
Path file = ...;
is not a valid statement, in any version of Java. My guess is that you copied and pasted this from some web site, but the three dots were just meant as an ellipsis meaning "the initialization code must go here".
What do you think these three dots mean?
The ... are place holders, these should be replaced with the actual path to the file on your computer. For example if the file exists in C:\Directory\file.txt, then the code should be:
Path file = "C:\\Directory\\file.txt";

Discovering jni4net samples

I'm discovering the jni4net. This is the technology used to provide the bridge between Java and .NET. So, I created new Eclipse Java project and copied the sample code from jni4net-0.8.6.0-bin/samples/myCSharpDemoCalc->MyCalcUsageInJava.java into this project. However the code cannot be compiled because two imports "mycsharpdemocalc.DemoCalc" and "mycsharpdemocalc.ICalc" cannot be found. I don't understand how to integrate/import mycsharpdemocalc.c into the Java project so that the code could be compiled.
import net.sf.jni4net.Bridge;
import java.io.IOException;
import mycsharpdemocalc.DemoCalc;
import mycsharpdemocalc.ICalc;
public class MyCalcUsageInJava {
public static void main(String arsg[]) throws IOException {
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("MyCSharpDemoCalc.j4n.dll"));
ICalc calc = new DemoCalc();
final int result = calc.MySuperSmartFunctionIDontHaveInJava("Answer to the Ultimate Question of Life, the Universe, and Everything");
System.out.printf("Answer to the Ultimate Question is : " + result);
}
}
There is ReadMe in each sample directory.
You have to use proxygen tool to generate the proxies (which are used in the java code).
There is generateProxies.cmd batch to do that.
More complex things may need config file for proxygen.
Also there is community Wiki

Categories

Resources