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.
Related
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!
I am trying to use graphviz native library from java.I am able to compile the program in Eclipse. But getting exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no gv in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1681)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at org.graphviz.test.Test.<clinit>(Test.java:12)
Could not find the main class: org.graphviz.test.Test. Program will exit.
Here is my code(copied from somewhere of course):
package org.graphviz.test;
import org.graphviz.internal.SWIGTYPE_p_Agedge_t;
import org.graphviz.internal.SWIGTYPE_p_Agnode_t;
import org.graphviz.internal.SWIGTYPE_p_Agraph_t;
import org.graphviz.internal.gv;
public class Test {
static {
System.loadLibrary("gv");
}
public static void main(String[] args) {
SWIGTYPE_p_Agraph_t g, sg;
SWIGTYPE_p_Agnode_t n, m;
SWIGTYPE_p_Agedge_t e;
g = gv.digraph("G");
System.out.println(gv.setv(g,"aaa","xxx"));
System.out.println(gv.getv(g,"aaa"));
sg = gv.graph(g,"SG");
n = gv.node(g,"hello");
System.out.println(gv.getv(n,"label"));
System.out.println(gv.setv(n,"aaa","xxx"));
System.out.println(gv.getv(n,"aaa"));
m = gv.node(g,"world");
System.out.println(gv.getv(m,"aaa"));
e = gv.edge(n,m);
System.out.println(gv.setv(e,"aaa","xxx"));
System.out.println(gv.getv(e,"aaa"));
gv.rm(e);
gv.rm(n);
gv.rm(m);
gv.rm(g);
g = gv.readstring("digraph G {a->b}");
gv.rm(g);
g = gv.read("hello.gv");
gv.layout(g,"dot");
gv.render(g,"png","hello.png");
gv.rm(g);
}
}
I have pointed the library correctly, but at runtime getting UnsatisfiedLinkError. Any one ever tried using the graphviz native library?Please let me know how to configure JNI.
I believe you're problem is that you are trying to import from gv, but you aren't specifying what you want to import. It's kind of like if you were to just write ---
import org.graphviz.internal;
Instead, try importing a specific library from the gv library. For example, if you want a directed graph, you would probably do something like this ---
import org.graphviz.internal.gv.digraph;
Take a look at the following document and see if it helps at all. It might give you a better idea of where gv comes from and how you can import the methods associated with it.
http://www.graphviz.org/pdf/gv.3java.pdf
Get graphviz-java
Make sure you have graphviz-java installed. For MacPorts this should work like this:
sudo port install graphviz +java
This actually failed for me on the first try and complained about swig-java, this fixed it:
sudo port install swig-java
sudo port install graphviz +java
Now the library is installed under /opt/local/lib/graphviz/java for me, search for libgv.jnilib if you don't find it there.
Setup class path
I compiled in Eclipse, for that I added /opt/local/lib/graphviz/java to the build path as an external class folder.
Compile
This is a simple example that writes an image. Note that System.loadLibrary("gv") must happen before calling anything in graphviz.
import org.graphviz.SWIGTYPE_p_Agedge_t;
import org.graphviz.SWIGTYPE_p_Agnode_t;
import org.graphviz.SWIGTYPE_p_Agraph_t;
import org.graphviz.gv;
public class Main {
static {
System.loadLibrary("gv");
}
public static void main(String[] args) {
SWIGTYPE_p_Agraph_t g = gv.digraph("G");
SWIGTYPE_p_Agnode_t n = gv.node(g, "hello");
SWIGTYPE_p_Agnode_t m = gv.node(g, "world");
SWIGTYPE_p_Agedge_t e = gv.edge(n, m);
gv.layout(g, "dot");
gv.render(g, "png", "hello.png");
}
}
Run
Run with java.library.path=/opt/local/lib/graphviz/java, e.g. -Djava.library.path=/opt/local/lib/graphviz/java as VM arguments in an Eclipse run configuration.
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);
I have a small test program which runs perfectly in the JBuilder 6 debugger. When I make a .jar file and run it I get an error
>java -jar testadll.jar
Start of DLL test
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidispatch (/com/sun
/jna/win32-x86/jnidispatch.dll) not found in resource path
at com.sun.jna.Native.loadNativeLibraryFromJar(Native.java:708)
at com.sun.jna.Native.loadNativeLibrary(Native.java:685)
at com.sun.jna.Native.<clinit>(Native.java:109)
at testadll.TestThisDLL$PenniesLib.<clinit>(TestThisDLL.java:24)
at testadll.TestThisDLL.main(TestThisDLL.java:33)
I have searched my drive and there is no jnidispatch.dll on it.
The program is
package testadll;
import com.sun.jna.Library;
import com.sun.jna.Native;
//import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.win32.StdCallLibrary;
//import com.sun.jna.*;
public class TestThisDLL {
public interface PenniesLib extends StdCallLibrary {
PenniesLib INSTANCE = (PenniesLib) Native.loadLibrary(
"PenniesLib", PenniesLib.class);
int a();
}
public static void main( String args[] ) {
System.out.println("Start of DLL test");
//TestDLL t = new TestDLL();
//System.out.println("DLL loaded");
int value = PenniesLib.INSTANCE.a();
System.out.println("DLL response is " + String.valueOf(value));
}
}
You've apparently merged JNA's classes with your own jar file, but omitted its native support. Ensure that all files from the original jna.jar (not just class files) are copied to the new destination and that their original paths are preserved.
Specifically, your jar file must include com/sun/jna/win32-x86/jnidispatch.dll. If you want to include support for other platforms, you must include com/sun/jna/*/jnidispatch as well.
You should use a version of jna.jar, that supports 64 bit, for example
jna-4.1.0.jar or jna-3.4.0.jar.
I have to call a dll method and I don't have the source code from dll, I was reading about JNI and understood that you should have the source to input the JNI library in the code (.h).
My second shoot is JNA, but I am getting the same error, although you don't have to change anything in DLL.
I created two classes to test:
interface:
package icom;
import com.sun.jna.Library;
public interface IConectorT extends Library {
int StartConector(byte[] conectorStatus, String icomPath);
}
DLL method call:
package icom;
import com.sun.jna.Native;
public class ConectorTJna {
public static void main(String args[]) {
IConectorT lib = (IConectorT) Native.loadLibrary("ConectorT", IConectorT.class);
int teste = lib.StartConector(null, "C:\\ICOM");
System.out.println("RESULT: " + teste);
}
}
When I call the lib.StartConector method I get this:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error
looking up function 'StartConector': The specified procedure could not
be found. at com.sun.jna.Function.(Function.java:179) at
com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:350) at
com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:330) at
com.sun.jna.Library$Handler.invoke(Library.java:203) at
$Proxy0.StartConector(Unknown Source) at
icom.ConectorTJna.main(ConectorTJna.java:10)
Did you specify path to the library, e.g. using system property?
Here are the details from "Getting Started with JNA" guide:
Make your target library available to your Java program. There are two
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.
Taken from: https://github.com/twall/jna/blob/master/www/GettingStarted.md