while run the java file i am getting an error java.lang.UnsatisfiedLinkError: JNIExample.getVersion([B[I)I - java-native-interface

D:\JNIExample\JNIExample\JNIExample>java JNIExample
Exception in thread "main" java.lang.UnsatisfiedLinkError: JNIExample.getVersion([B[I)I
at JNIExample.getVersion(Native Method)
at JNIExample.main(JNIExample.java:15)

Are you loading the dll/so library with the native code?
Typically done in the class needing it, like this:
static {
try {
System.loadLibrary("jniexample");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
Edit: accidentally used the old load call in the first answer. you probably should use loadLibrary(). See this article for a comparison

Related

How to check in JNI if java class exists?

If im using this:
if(!env->FindClass("com/test/app")){
Log("Not found");
return 0;
}
I got error: no such class
As mentioned in the documentation, FindClass can result in a variety of (Java) exceptions being thrown.
It is an error to make any further JNI calls with a pending Java exception. So you need to, at a minimum, clear the exception using env->ExceptionClear().

Catch block being bypassed on boost exception

Environment: Windows 10, Visual Studio 2015, x64, Unicode, boost 1.64
I'm having problems determining why a catch block is not being hit when a boost exception is purposely attempted. The point it to let boost tell me if a source string for a date is of the wrong format.
Here is the code in question:
std::string sDate("01/03/2017"); // purposely bad date format, should be 2017/01/03
bool bGoodDate(true);
try
{
boost::gregorian::date dtTemp(boost::gregorian::from_simple_string(sDate));
}
catch (const std::out_of_range& e)
{
bGoodDate = false;
}
catch (...)
{
bGoodDate = false;
}
When this code is in a stand-alone console program, it works. The out_of_range catch block is hit. But when the same code is in a Windows GUI app built with MFC in a static library (not sure that's relevant), the catch block is never hit. Instead, I see this boost exception wrapper referenced in an Unhandled Exception error thrown by the GUI app:
Unhandled exception at 0x00007FFC0BC09E08 in Journal.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month> > at memory location 0x00000078A3DEB100.
I have compared the compiler, debugger and linker settings between the two projects, and there were no differences, other than the number of third party libraries being larger in the GUI project, and the few console-vs-gui settings needed to build the applications.
I found this seemingly related question/answer, but I'm not sure how I'd go about finding a solution based on that answer: Weird issue with catching trivial boost exception
Any ideas on how I could track down the cause of this behavior?
Thanks.

c++ try/catch ignorance in iOS

In our app we have a c++ static library and I use Objective-C++ to work with it.
That c++ library utilizes rapidjson to parse XML data:
try {
rapidjson::Document document;
document.Parse(connection.data.description);
connection.openTime = document["openFrom"].GetInt();
connection.closeTime = document["openTo"].GetInt();
return true;
} catch (std::exception e) {
connection.openTime = 0;
connection.closeTime = 0;
return false;
}
The problem is that if document["openFrom"] cannot be converted into Int via GetInt() method, exception is not raised. Instead of that my app crashes with SIGABRT.
Assertion failed: (data_.f.flags & kIntFlag), function GetInt, file /Users/xxx/xxx/xx/ios/../src/rapidjson/document.h, line 1645.
On Android OS, btw, in the same case exception is raised successfully.
What could be the problem? I guess the issue is in Xcode's Swift compiler behavior.
As it clearly stated in the log you provided – it is not a crash, it is only a failed assert which internally calls abort() that results in SIGABRT which stands for 'signal abort'. Asserts are disabled in release mode so it should work fine there. Or you can disable asserts in rapidjson (by defining macro RAPIDJSON_ASSERT).

Compiling Native code (c++) for windows phone 8

I have created a cocos2d-x 3.0 RC Project which I wanted to play in windows as well. I have added libraries such as CocosDension, libExtensions, libNetwork, libCurl etc to my project as those are required for the game. When I compile everything is working fine, but when I try to run I am getting the following runtime errors. Below mentioned the is the exception that is thrown.
An exception of type 'System.IO.FileNotFoundException' occurred in
PhoneDirect3DXamlAppInterop.DLL but was not handled in user code. If
there is a handler for this exception, the program may be safely
continued.
This particular exception is thrown in below block of code
private void DrawingSurfaceBackground_Loaded(object sender, RoutedEventArgs e)
{
if (m_d3dInterop == null)
{
m_d3dInterop = new Direct3DInterop();
// Set WindowBounds to size of DrawingSurface
m_d3dInterop.WindowBounds = new Windows.Foundation.Size(
(float)Application.Current.Host.Content.ActualWidth,
(float)Application.Current.Host.Content.ActualHeight
);
// Hook-up native component to DrawingSurfaceBackgroundGrid
DrawingSurfaceBackground.SetBackgroundContentProvider(m_d3dInterop.CreateContentProvider());
DrawingSurfaceBackground.SetBackgroundManipulationHandler(m_d3dInterop);
// Hook-up Cocos2d-x delegates
m_d3dInterop.SetCocos2dEventDelegate(OnCocos2dEvent);
m_d3dInterop.SetCocos2dMessageBoxDelegate(OnCocos2dMessageBoxEvent);`
m_d3dInterop.SetCocos2dEditBoxDelegate(OpenEditBox);
}
}
Exception is thrown at line
m_d3dInterop = new Direct3DInterop();
I am also putting the error log below
The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
at PhoneDirect3DXamlAppComponent.Direct3DInterop..ctor()
at PhoneDirect3DXamlAppInterop.MainPage.DrawingSurfaceBackground_Loaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
If anyone has any idea reg the same, please do help
I'd update the the newly released Cocos2d-x v3 as it has WP8 support.
http://cocos2d-x.org/download
Release Notes: https://github.com/cocos2d/cocos2d-x/blob/v3/docs/RELEASE_NOTES.md

Calling a C++ dll method from Java using JNA and avoiding Method Name Mangling

I have been going through links on StackOverflow on how to resolve the Method name mangling but did not find any solution with a real time example.
Scenario-A C++ Ex.dll file is provided by client. I need to access the Ex.dll and call the methods in the same through Java.
Restrictions- Cannot modify the Ex.dll, i can only access the same.
Issue Faced- Getting the below exception when i access the Ex.dll through JNA
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'getCPUSpeed': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:134)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:336)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:316)
at com.sun.jna.Library$Handler.invoke(Library.java:203)
at $Proxy0.getCPUSpeed(Unknown Source)
at cpp.java.JnaTest.main(JnaTest.java:16)
Googled a lot and found that its due to method name Mangling, but again could not find any good sample code with the solution.
This is code i used-
import com.sun.jna.Native;
class JnaTest
{
public static void main(String args[])
{
try
{
JnaInterface jInterface = (JnaInterface) Native.loadLibrary("Ex", JnaInterface.class);
System.out.println("Calling C++ DLL method");
System.out.println("========================");
System.out.println("getCPUSpeed() -- "+jInterface.getCPUSpeed());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package cpp.java;
import com.sun.jna.Library;
public interface JnaInterface extends Library{
public int getCPUSpeed();
}
Update 1: **************************************
Below mentioned is the actual functions i get when i browse the DBMM.dll through dependency walker-
DBMM DLL functions-
??0cDbmmInterfaceCache##QAE#ABV0##Z
??0cDbmmInterfaceCache##QAE#XZ
??0cDbmmInterfaceControl##QAE#ABV0##Z
??0cDbmmInterfaceControl##QAE#XZ
??0cDbmmInterfaceEcon##QAE#ABV0##Z
??0cDbmmInterfaceEcon##QAE#XZ
??0cDbmmInterfaceKnob##QAE#XZ
??0cDbmmInterfaceOutput##QAE#ABV0##Z
??0cDbmmInterfaceOutput##QAE#H#Z
??0cDbmmInterfacePoolLoan##QAE#ABV0##Z
??0cDbmmInterfacePoolLoan##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z
??0cDbmmMacroEcon##QAE#ABV0##Z
??0cDbmmMacroEcon##QAE#ABVcDbmmInterfaceEcon##_N#Z
??0cDbmmMtgBasisConstSpreadModel##IAE#XZ
??0cDbmmMtgBasisConstSpreadModel##QAE#ABV0##Z
??0cDbmmMtgBasisConstSpreadModel##QAE#PBD#Z
??0cDbmmMtgBasisModel##QAE#ABV0##Z
??0cDbmmMtgBasisModel##QAE#XZ
??0cScaleFieldsSubSum##QAE#NN#Z
??1cDbmmInterfaceCache##QAE#XZ
??1cDbmmInterfaceControl##QAE#XZ
??1cDbmmInterfaceEcon##QAE#XZ
??1cDbmmInterfaceKnob##QAE#XZ
??1cDbmmInterfaceOutput##QAE#XZ
??1cDbmmInterfacePoolLoan##QAE#XZ
??1cDbmmMacroEcon##QAE#XZ
??1cDbmmMtgBasisConstSpreadModel##UAE#XZ
??1cDbmmMtgBasisModel##UAE#XZ
??1cScaleFieldsSubSum##QAE#XZ
??4cDbmmInterface##QAEAAV0#ABV0##Z
??4cDbmmInterfaceCache##QAEAAV0#ABV0##Z
??4cDbmmInterfaceControl##QAEAAV0#ABV0##Z
??4cDbmmInterfaceEcon##QAEAAV0#ABV0##Z
??4cDbmmInterfaceKnob##QAEAAV0#ABV0##Z
??4cDbmmInterfaceOutput##QAEAAV0#ABV0##Z
??4cDbmmInterfacePoolLoan##QAEAAV0#ABV0##Z
??4cDbmmMacroEcon##QAEAAV0#ABV0##Z
??4cDbmmMtgBasisConstSpreadModel##QAEAAV0#ABV0##Z
??4cDbmmMtgBasisModel##QAEAAV0#ABV0##Z
??4cScaleFieldsSubSum##QAEAAV0#ABV0##Z
??_7cDbmmMtgBasisConstSpreadModel##6B#
??_7cDbmmMtgBasisModel##6B#
??_FcDbmmInterfaceOutput##QAEXXZ
??_FcDbmmInterfacePoolLoan##QAEXXZ
??_FcScaleFieldsSubSum##QAEXXZ
?Add#cScaleFieldsSubSum##QAEXNN#Z
?InitSubsum#cScaleFieldsSubSum##QAEXNN#Z
?ReInit#cDbmmMacroEcon##QAEX_N#Z
Not sure how can i call these functions through Java.
Appreciate if somebody could provide me a solution from Java end, with a sample code please :)
Your function is decorated with JNI and stdcall conventions; it is not C++-mangled.
It looks like the library is a JNI library, given the Java_sysInfo_ prefix. If that is the case, you need only declare the equivalent Java-side, e.g.
// default package
public class sysInfo {
static { System.loadLibrary("Ex"); }
public static native int getCPUSpeed();
}
I think you'll probably find that this mapping is the correct one and that you don't need JNA.
EDIT
Given a C++ class with an arbitrary ctor input argument and method getCount():
extern "C" int getCountForName(const char* name) {
MyCPPClass mycpp(name);
return mycpp.getCount();
}
Compile that into a shared library, and load via JNA.
If your building the DLL through Visual studio, you should be able to build it as a release instead of debug to fix the function names if you are not already doing this.