I have 2 libs (first.so and second.so), and I need to call a function from 'second.so' in the 'first.so'.
Now in my case I don't want to do static link to second.so.
I need to call it dynamically.
So what I need is to use
System.loadlibrary("first.so")
System.loadlibrary("second.so")
Now in the c++ code of "first.so" I need to call a function from the second.so.
Can I do this without dlopen?
When you compile the C++ code and build your first.so you instruct the linker to use external symbols from the second library. The second library must be on the library search path at run time so that the systems dynamic linker can find it.
Related
I'm developing a simple javacard applet using the jcdk 3.0.5u3 with Eclipse Oxygen3. If I use a simple API from GlobalPlatform like the GPSystem.getCardContentState() results in error.
I've tried to add the globalplatform.jar file from GP API v1.1 and v1.6 to the Reference Libraries part of the package explorer. I also imported the "org.globalplatform.*" into the code.
import org.globalplatform.*;
if(GPSystem.getCardContentState() == GPSystem.APPLICATION_SELECTABLE){
//Do something
}
The converter returns "export file global platform.exp of package org.globalplatform not found"
Java Card doesn't just require a compile stage, it also performs the linking that is usually performed as dynamic linking in the JVM of a normal Java application. Basically it orders the methods and such, and then calls the right serial ID. You don't want your Applet to contain the string names of your fields after all: it would explode the memory requirements, and dynamically looking for classes and fields is not a good idea either within such a restricted platform.
So if you call external libraries then you need to configure:
the .jar file containing the .class files for the normal compiler;
the .exp file which contains the an export of the mapping of the normal names and the ID of the classes and fields specific for the converted classes of the called library;
If it is not already present on the card, you may also need the version specific .cap file for uploading. However, the GP functionality is should already be present on the card.
The ID's are only unique for a specific .cap file / preloaded byte code. This is why you always need the right .exp file for the code that is loaded. If another field is added, the ordering is different and the wrong fields would be linked, if the linker executes at all. So having the right .exp file is a requirement for correct conversion to .cap for your application / library.
For the JCDK I think you just need to configure the right -exportpath, as the GP should be included with the JCDK.
I am working on a system where I am using part of the JOGL OpenGL bindings to Java. I find the basic idea behing the "gluegen" C to Java binding generator good but the docs are thin and I havn't been able to locate a simple sample of gluegen usage consisting of:
A C module the does something like modify a string or print a string to stdout (ie: easily tested from java code) an expanded version one with multiple methods that handle each data type handled by gluegen.
the appropriate "cfg", ".h" files needed to power gluegen for the sample source.
A build script - ideally simple not factored out too much into an endless chain to follow generic makefile or shell script :)
the result of running the build script gives you the generated and sample code for a complete native side DLL with DLL main, export tables, and whatever else is needed to load into java on a windows box (.lib for unix)
A C compile of the code with MSVC and/or GCC to compile and link the code and generated code to produce the DLL.
The generated java class or classes that mate with the DLL including initialization.
In a sense sort of a "hello world" in C to be called from Java as a place to start.
PK
I have seen a few posts on this, but I haven't seen any solutions so far. I have a .jar file that I'm converting to a .NET DLL via IKVM. I'm trying to figure out how to make the methods in the DLL available inside the excel VBA environment. here are the details.
1.) installed IKVM & registered it's DLL's to GAC
2.) ran IKVM to create the a .net .dll (mytest.dll)
ikvmc mytest.jar
3.) registered the new .dll
regasm mytest.dll
4.) From here i created a VB.NET project and added mytest.dll and IKVM.OpenJDK.Core.dll as references to the project. I am then able to access the methods within the .dll in .NET. This is great!
5.) what I really want to do is be able to use the .dll in VBA as well. Initially vba wouldn't accept the .dll directly as it's a .net library. I attempted to create a type library:
regasm /codebase /tlb mytest.dll
This created a .tlb file which is nice, but it did throw a warning about the library not being strongly named.
6.) then I loaded the .tlb as a reference in my vba editor. This works, however when I try to access the methods nothing shows up. Similarly if I look in the object viewer for my library i can see my two classes but not the members of those classes.
Additionally, I imagine that I probably also need to somehow reference the IKVM.OpenJDK.Core.dll inside VBA as well. However I can't do that either since it's a .NET .dll.
Has anyone had success converting a .jar file into something that can be used with VBA?
I think you always need to explicitly mark a class to be usable via COM interop. Here's an example of a Java class that is usable from VBA:
import cli.System.Runtime.InteropServices.*;
#ClassInterfaceAttribute.Annotation(ClassInterfaceType.__Enum.AutoDual)
public class SampleWidget {
public int Add(int x, int y) {
return x + y;
}
}
Here are the steps to compile:
Copy IKVM.Runtime.dll and all IKVM.OpenJDK.*.dll into the current directory or the GAC.
Run "ikvmstub mscorlib" to generate mscorlib.jar.
Create a Java source named SampleWidget.java containing the code above.
javac -cp mscorlib.jar;. SampleWidget.java
ikvmc -out:SampleLibrary.dll SampleWidget.class -r:mscorlib.dll
tlbexp SampleLibrary.dll
regasm /codebase SampleLibrary.dll (this step needs administrator rights)
Now you can add a reference to the SampleLibrary.tlb from VBA and use the SampleWidget class.
I am trying to generate JNI code from my Java class.
I was able to generate the header without a problem using javah
Whenever I run javah with the -stubs command I get this error:
Error: JNI does not require stubs, please refer to the JNI documentation.
Does anyone know if there is a way to generate a JNI .c file for a class.
I can make the file from my header by hand, but it seems like something a tool should be able to do.
I found an unresolved bug request about this from 2000 so I'm not too hopeful.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4368114
-stubs is a leftover from a long-obsolete first cut at JNI. It never generated anything useful for any version of Java used in the current century.
Just copy the header file into your .c file and add a function body to each function.
Or perhaps have a look at http://jna.java.net/.
I am trying to access the function available inside the .dll file. But it give exception like "Exception in thread "main" java.lang.UnsatisfiedLinkError: *.jniGetAudioInputLevel()D". DLL file is loaded but when I try to access the methods it give an error message.
According to my knowledge:-
This exception only occurs if the .dll is not in the class path or the dll is not present in the jar file.
.dll file is only created if all the code is running with out any error.
I have seen the methods in the dll using tools like for example: Anywhere PE Viewer, PE Explorer etc. They both are showing the methods available in the .dll file.
How this in accessibility to the function can be configured out by any other idea?
An UnsatisfiedLinkError is also thrown if the native counterpart to a method declared native can't be found. This can easily happen if the native code was not named with the full Java package name separated using '_'.
For example,
package com.mycompany.stuff;
public native void doSomething();
Requires that a native library (DLL, so, *SRVPGM, etc depending on your system) be found and loaded with System.loadLibrary(), which contains and exports a function named
com_mycompany_stuff_doSomething
If you are certain that the native library is being loaded, my guess is that the function is not correctly named, or is not exported.
I agree with Software Monkey, but I have one very significant addition related to the function name. The function name in native library should starts from '_Java_'. In your case it should be:
_Java_com_mycompany_stuff_doSomething
I found it by chance and spent two days to figure out why JVM cannot find the function in the DLL if it is there. In my case javah generates header file with function name without underscore sign before 'Java_'. Therefore, I had to update it manually in order to make it work.
I wonder why it was not mentioned about underscore prefix in "The Java Native Interface: Programmer's Guide and Specification", "Java Native Interface 6.0 Specification" provided with Java 6 Documentation (I worked with jdk 1.6.0_30, but the JDK version should not be the issue) and some other resources.
Normally we are getting this exception when JVM can't find the .dll file.