tried using this lib for opus on android and AFAIK, the link and the .so look to be good.
ERROR on call to C function ( i followed these intstructs )
D/OpusRecorder( 1964): Start recording!
E/art ( 1964): No implementation found for int com.droidkit.opus.OpusLib.startRecord(java.lang.String) (tried Java_com_droidkit_opus_OpusLib_startRecord and Java_com_droidkit_opus_OpusLib_startRecord__Ljava_lang_String_2)
E/AndroidRuntime( 1964): FATAL EXCEPTION: Thread-8227
E/AndroidRuntime( 1964): Process: com.borneo.speech, PID: 1964
E/AndroidRuntime( 1964): java.lang.UnsatisfiedLinkError: No implementation found for int com.droidkit.opus.OpusLib.startRecord(java.lang.String) (tried Java_com_droidkit_opus_OpusLib_startRecord and Java_com_droidkit_opus_OpusLib_startRecord__Ljava_lang_String_2)
E/AndroidRuntime( 1964): at com.droidkit.opus.OpusLib.startRecord(Native Method)
E/AndroidRuntime( 1964): at com.borneo.speech.OpusRecorder.run(OpusRecorder.java:439)
W/ActivityManager( 724): Force finishing activity 1 com.borneo.speech/.Speech_API_Activity
The .so is packaged in apk
function exist in the .so with Type= "T"...
aar$ nm -D libopus.so | head
000084c1 T Java_com_droidkit_opus_OpusLib_closeOpusFile
000084c5 T Java_com_droidkit_opus_OpusLib_isOpusFile
00008491 T Java_com_droidkit_opus_OpusLib_openOpusFile
00008471 T Java_com_droidkit_opus_OpusLib_readOpusFile
00008489 T Java_com_droidkit_opus_OpusLib_seekOpusFile
00008145 T Java_com_droidkit_opus_OpusLib_startRecord ***
---CLang details---
implementation in C:
audio.c
#include "com_droidkit_opus_OpusLib.h" <-- from the Java class=com.droidkit.opus.OpusLib via javah
//rev belo per #Nicklas
JNIEXPORT jint JNICALL Java_com_droidkit_opus_OpusLib_startRecord(JNIEnv *env, jobject javaThis, jstring path) {
const char *pathStr = (*env)->GetStringUTFChars(env, path, 0);
int result = initRecorder(pathStr);
if (pathStr != 0) {
(*env)->ReleaseStringUTFChars(env, path, pathStr);
}
return result;
}
c headers...
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_droidkit_opus_OpusLib */
#ifndef _Included_com_droidkit_opus_OpusLib
#define _Included_com_droidkit_opus_OpusLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_droidkit_opus_OpusLib
* Method: startRecord
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_droidkit_opus_OpusLib_startRecord
(JNIEnv *, jobject, jstring);
/*
* Class: com_droidkit_opus_OpusLib
* Method: writeFrame
* Signature: (Ljava/nio/ByteBuffer;I)I
*/
JNIEXPORT jint JNICALL Java_com_droidkit_opus_OpusLib_writeFrame
(JNIEnv *, jobject, jobject, jint);
/*
* Class: com_droidkit_opus_OpusLib
* Method: stopRecord
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_droidkit_opus_OpusLib_stopRecord
(JNIEnv *, jobject);
/*
* Class: com_droidkit_opus_OpusLib
* Method: isOpusFile
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_droidkit_opus_OpusLib_isOpusFile
(JNIEnv *, jobject, jstring);
...
#ifdef __cplusplus
}
#endif
#endif
===JAVA Details Calls to C===
==EDITED== Java Native Decl.
/**
* OpusLib native binding
*/
public class OpusLib {
static {
System.loadLibrary("opus");
}
/**
* Starting opus recording
*
* #param path path to file
* #return non zero if started player
*/
public native int startRecord(String path);
/**
* Writing audio frame to encoder
*
* #param frame buffer with sound in 16 bit mono PCM 16000 format
* #param len len of data
* #return not null if successful
*/
public native int writeFrame(ByteBuffer frame, int len);
/**
* Stopping record
*/
public native void stopRecord();
/**
* Checking Opus File format
*
* #param path path to file
* #return non zero if opus file
*/
public native int isOpusFile(String path);
/**
* Opening file
*
* #param path path to file
* #return non zero if successful
*/
public native int openOpusFile(String path);
/**
* Seeking in opus file
*
* #param position position in file
* #return non zero if successful
*/
public native int seekOpusFile(float position);
/**
* Closing opus file
*/
public native void closeOpusFile();
/**
* Reading from opus file
*
* #param buffer
* #param capacity
*/
public native void readOpusFile(ByteBuffer buffer, int capacity);
/**
* Is playback finished
*
* #return non zero if playback is finished
*/
public native int getFinished();
/**
* Read block size in readOpusFile
*
* #return block size in bytes
*/
public native int getSize();
/**
* Offset of actual sound for playback
*
* #return offset
*/
public native long getPcmOffset();
/**
* Total opus pcm duration
*
* #return pcm duration
*/
public native long getTotalPcmDuration();
}
try {
opus = new OpusLib();
}catch(UnsatisfiedLinkError ulx) {
Log.e(LTAG, "Illegal native Load: " + ulx.getMessage());
}
...
public class OpusLib {
static {
System.loadLibrary("opus");
}
AFAIK , lib is getting loaded ok
UnsatisfiedLink Excp occurs on last line below:
private String mPath = path;
if (mShouldRecord)
int mint = opus.startRecord(mPath); <-- throws unsatisfiedLink - no implementation found
startRecord is in the symbols table altho im not certain all the function parm types match up? see the "string_2"...
startRecord__Ljava_lang_String_2)
==== Build info Gradle and linkedit ===
link :
command: /usr/local/src/android-ndk-r10d/ndk-build NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/home/rob/src/tmp/speechnw/libraries/opus/build/intermediates/ndk/release/Android.mk APP_PLATFORM=android-19 NDK_OUT=/home/rob/src/tmp/speechnw/libraries/opus/build/intermediates/ndk/release/obj NDK_LIBS_OUT=/home/rob/src/tmp/speechnw/libraries/opus/build/intermediates/ndk/release/lib APP_STL=stlport_static APP_ABI=armeabi-v7a
[armeabi-v7a] Compile thumb : opus <= audio.c
...
[armeabi-v7a] SharedLibrary : libopus.so
[armeabi-v7a] Install : libopus.so => /home/rob/src/tmp/speechnw/libraries/opus/build/intermediates/ndk/release/lib/armeabi-v7a/libopus.so
build.gradle
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.1.1"
ndk {
moduleName "opus"
cFlags "-DANDROID_NDK " +
"-DDISABLE_IMPORTGL " +
"-w -std=gnu99 -O3 -fno-strict-aliasing -fprefetch-loop-arrays " +
"-DNULL=0 -DSOCKLEN_T=socklen_t -DLOCALE_NOT_USED -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64 "+
"-Drestrict='' -D__EMX__ -DOPUS_BUILD -DFIXED_POINT -DUSE_ALLOCA -DHAVE_LRINT -DHAVE_LRINTF -fno-math-errno "
"-DAVOID_TABLES "
ldLibs "log", "m"
stl "stlport_static"
abiFilter "armeabi-v7a"
}
}
Replace this line:
JNIEXPORT jint JNICALL Java_com_droidkit_opus_OpusLib_startRecord(JNIEnv *env, jclass class, jstring path) {
with:
JNIEXPORT jint JNICALL Java_com_droidkit_opus_OpusLib_startRecord(JNIEnv *env, jobject javaThis, jstring path) {
The function you've defined in C maps to a static method in Java, but in Java you're using it as an instance method. What I've replaced is this: jclass class, with jobject javaThis, in your C function signature. Please also note that in your C headers you've also declared the functions with instance method signatures, just as it seems you're intending to.
Related
I am deploying a program and encountered error of
Caused by: java.lang.Exception: java.lang.UnsatisfiedLinkError:
com.package.JniClass.JniGeoDbReader.openGeoDb()Ljava/lang/String;
I managed to load the dll but I feel like something has caused the string to not be converted correctly and caused the error. The following is the JNI code I am currently testing to deploy on Karaf :
JNI Codes>>>JAVA and c++
JniGeoDbReader.class
package com.package.JniClass;
public final class JniGeoDbReader {
private JniGeoDbReader() { }
public static native String openGeoDb(); }
ServiceClass.class <--- This class is the simplified version
import com.package.JniClass.JniGeoDbReader;
public class ServiceClass{
public void fetchResponse(){
System.load("D:\\DLL_PATH\\com_package_jniGeoDbReader.dll");
LOGGER.info(">>>>>>>>>>>>>>>>> {}", JniGeoDbReader.openGeoDb());
}
}
com_package_JniClass_JniGeoDbReader.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_package_JniClass_JniGeoDbReader */
#ifndef _Included_com_package_JniClass_JniGeoDbReader
#define
_Included_com_package_JniClass_JniGeoDbReader_JniClass_JniGeoDbReader
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_package_JniClass_JniGeoDbReader
* Method: openGeoDb
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_com_package_JniClass_JniGeoDbReader_openGeoDb
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
dllmain.cpp <--I got this code from somewhere else
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "com_package_JniClass_JniGeoDbReader.h"
#include <string>
using namespace std;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
JNIEXPORT jstring JNICALL
Java_com_package_JniClass_JniGeoDbReader_openGeoDb
(JNIEnv *env , jobject obj) {
string message = "Welcome to JNI";
int byteCount = message.length();
const jbyte* pNativeMessage = reinterpret_cast<const jbyte*>
(message.c_str());
jbyteArray bytes = env->NewByteArray(byteCount);
env->SetByteArrayRegion(bytes, 0, byteCount, pNativeMessage);
// find the Charset.forName method:
// javap -s java.nio.charset.Charset | egrep -A2 "forName"
jclass charsetClass = env->FindClass("java/nio/charset/Charset");
jmethodID forName = env->GetStaticMethodID(
charsetClass, "forName", "
(Ljava/lang/String;)Ljava/nio/charset/Charset;");
jstring utf8 = env->NewStringUTF("UTF-8");
jobject charset = env->CallStaticObjectMethod(charsetClass,
forName, utf8);
// find a String constructor that takes a Charset:
// javap -s java.lang.String | egrep -A2 "String\(.*charset"
jclass stringClass = env->FindClass("java/lang/String");
jmethodID ctor = env->GetMethodID(
stringClass, "<init>", "([BLjava/nio/charset/Charset;)V");
jstring jMessage = reinterpret_cast<jstring>(
env->NewObject(stringClass, ctor, bytes, charset));
return jMessage; }
I got the code in the cpp from here: Send C++ string to Java via JNI. I would like to do something similar, that is to return a string without accepting any parameters. But for some reason, it is throwing error. Please guide me in what causes this error and which part of this code I should change. Thank you.
UPDATE
Here is the full stacktrace:
at com.sun.proxy.$Proxy116.fetchCatalogue(Unknown Source)
at com.package.ArcGisDiscoveryJob$CatalogueTask.call(ArcGisDiscoveryJob.java:485)
at com.package.ArcGisDiscoveryJob$CatalogueTask.call(ArcGisDiscoveryJob.java:450)
at com.package.BaseTaskExecutor.invoke(BaseTaskExecutor.java:84)
at com.package.ArcGisDiscoveryJob.traverse(ArcGisDiscoveryJob.java:131)
at com.package.ArcGisDiscoveryJob.run(ArcGisDiscoveryJob.java:224)
at com.package.BaseScheduler$1.run(BaseScheduler.java:101)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.Exception: java.lang.UnsatisfiedLinkError: com.package.JniClass.JniGeoDbReader.openGeoDb()Ljava/lang/String;
at com.package.service.rest.impl.ArcGisClientProxy$1.call(ArcGisClientProxy.java:161)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 3 more
Caused by: java.lang.UnsatisfiedLinkError: com.package.JniClass.JniGeoDbReader.openGeoDb()Ljava/lang/String;
at com.package.JniClass.JniGeoDbReader.openGeoDb(Native Method)
at com.package.service.rest.impl.ServiceClass.fetchResponse(ServiceClass.java:364)
at com.package.service.rest.impl.ServiceClass.fetchCatalogue(ServiceClass.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.package.service.rest.impl.ArcGisClientProxy.callEndPoint(ArcGisClientProxy.java:241)
at com.package.service.rest.impl.ArcGisClientProxy.access$000(ArcGisClientProxy.java:32)
at com.package.service.rest.impl.ArcGisClientProxy$1.call(ArcGisClientProxy.java:155)
... 4 more
Your header file and your CPP code don't match. The header declares jclass for the second function parameter, but the implementation has jobject. JNI itself would not care because jclass is the same as jobject, but your compiler will not recognize the function, because of the different parameter declaration. Therefore it will not see the extern "C" for the function and export it with CPP name mangling, and Java will not find it.
Edit: hadn't seen #user207421's comment, and while it is important that you always regenerate the header file if something changes in your native method, it won't help in your case because your header already matches the Java code. You also have to change your implementation to match the header.
I am trying to make a proof of concept where in a cpp program I fetch the windows username and then call this program from a java code, using Java native interface(JNI). Now what i have so far is, a sample JNI hello world program which is able to compile and print Hello world or what what ever parameter I set up. Now in a separate cpp snippet i am able to fetch the current username and it works as well; looks as follows:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
char acUserName[100];
DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, &nUserName)) {
cout << "User name is " << acUserName << "." << endl;
cin.get();
}
return 0;
}
When compiled and executed using my G++ in command prompt it prints the current username correctly.
Now i just want to combine the two programs. When I do that as follows
public class Sample1
{
public native String fetchCurrentUserName();
public static void main(String[] args)
{
System.loadLibrary("Sample1");
Sample1 sample = new Sample1();
String userName = sample.fetchCurrentUserName();
System.out.println("userName: " + userName);
}
}
This is the java part and the cpp part is as follows:
#include "Sample1.h"
#include <string.h>
#include <iostream>
#include <windows.h>
using namespace std;
JNIEXPORT jstring JNICALL Java_Sample1_fetchCurrentUserName
(JNIEnv *env, jobject obj) {
char acUserName[100];
DWORD nUserName = sizeof(acUserName);
if (GetUserName(acUserName, &nUserName)) {
cout << "User name is " << acUserName << "." << endl;
cin.get();
}
const char* userName = acUserName;
//const char* userName = "acUserName";
return env->NewStringUTF(userName);
}
void main(){}
This does not compile anymore, it says
Sample1.obj : error LNK2019: unresolved external symbol __imp_GetUserNameA referenced in function Java_Sample1_fetchCurrentUserName
Sample1.dll : fatal error LNK1120: 1 unresolved externals
I used the following command to compile the JNI code.
cl -I"C:\Program Files\Java\jdk1.8.0_131\include" -I"C:\Program Files\Java\jdk1.8.0_131\include\win32" -LD Sample1.cpp -FeSample1.dll
as per tutorial from https://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html. Now from few other questions in stack overflow it seems somehow my program is unable to link the windows library or leader somehow. But I have no clue as to how this can be solved. Any body have any bright ideas? Please make my day! Thank you for your time :D I looked into Linking of dll file in JNI which is very relevant for my question. But not sure if the solutions are applicable to me.
Edit
error: LNK1120: 5 unresolved externals This question had similar problem and it seems to have same solution which is as suggested by Remy to link with Advapi32.lib. At least now i learned few new things like #pragma comments and the role of Advapi32 library. Many thanks!
Your code compiles fine. You are actually getting a linker error, not a compiler error.
From the error message, you can see that your C++ code is trying to call a GetUserNameA() function. GetUserName() is a preprocessor #define macro in winbase.h (which is included by windows.h) that resolves to GetUserNameA() when UNICODE is not defined:
WINADVAPI
BOOL
WINAPI
GetUserNameA (
__out_ecount_part_opt(*pcbBuffer, *pcbBuffer) LPSTR lpBuffer,
__inout LPDWORD pcbBuffer
);
WINADVAPI
BOOL
WINAPI
GetUserNameW (
__out_ecount_part_opt(*pcbBuffer, *pcbBuffer) LPWSTR lpBuffer,
__inout LPDWORD pcbBuffer
);
#ifdef UNICODE
#define GetUserName GetUserNameW
#else
#define GetUserName GetUserNameA // <-- HERE!
#endif // !UNICODE
As there is no GetUserNameA() function implemented in your C++ code, but there is at least a declaration available (from winbase.h), the compiler emits machine code that references GetUserNameA() externally (via a symbol named __imp_GetUserNameA). When the linker is then invoked after compiling is finished, the linker outputs the "unresolved" error because it is not able to find an implementation of any GetUserNameA function to satisfy that reference.
GetUserNameA() is a Win32 API function implemented in Advapi32.dll. You need to link to Advapi32.lib from your compiler's Windows SDK so the linker can find the implementation of GetUserNameA().
One way to do that is to add a #pragma comment(lib) statement to your C++ code, eg:
#include "Sample1.h"
#include <windows.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "Advapi32.lib") // <-- add this!
JNIEXPORT jstring JNICALL Java_Sample1_fetchCurrentUserName(JNIEnv *env, jobject obj) {
char acUserName[100] = {};
DWORD nUserName = sizeof(acUserName);
if (GetUserNameA(acUserName, &nUserName)) {
cout << "User name is " << acUserName << "." << endl;
}
else {
DWORD dwErr = GetLastError();
cout << "Unable to get User name, error " << dwErr << "." << endl;
}
return env->NewStringUTF(acUserName);
}
void main() {}
Alternatively:
#include "Sample1.h"
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
#pragma comment(lib, "Advapi32.lib")
JNIEXPORT jstring JNICALL Java_Sample1_fetchCurrentUserName(JNIEnv *env, jobject obj) {
DWORD nUserName = 100, dwErr;
std::vector<char> acUserName(nUserName);
do {
if (GetUserNameA(&acUserName[0], &nUserName)) {
cout << "User name is " << &acUserName[0] << "." << endl;
break;
}
dwErr = GetLastError();
if (dwErr != ERROR_INSUFFICIENT_BUFFER) {
cout << "Unable to get the User name, error " << dwErr << "." << endl;
break;
}
acUserName.resize(nUserName);
}
while (true);
return env->NewStringUTF(&acUserName[0]);
}
void main() {}
That being said, NewStringUTF() requires an input string in modified UTF-8 format. However, GetUserNameA() outputs ANSI format (hence the A in its name), not in UTF-8, let alone modified UTF-8. Windows has no concept of modified UTF-8 at all. Your code will work correctly only if the username does not contain any non-ASCII characters in it.
Your C++ code really should be calling GetUserNameW() instead, which outputs in UTF-16 format. Then you can use NewString() instead of NewStringUTF() (Java strings use UTF-16 anyway), eg:
#include "Sample1.h"
#include <windows.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "Advapi32.lib")
JNIEXPORT jstring JNICALL Java_Sample1_fetchCurrentUserName(JNIEnv *env, jobject obj) {
WCHAR acUserName[100];
DWORD nUserName = 100;
if (GetUserNameW(acUserName, &nUserName)) {
--nUserName; // ignore the null terminator
wcout << L"User name is " << acUserName << L"." << endl;
}
else
{
nUserName = 0;
DWORD dwErr = GetLastError();
cout << "Unable to get the User name, error " << dwErr << "." << endl;
}
return env->NewString(reinterpret_cast<jchar*>(acUserName), nUserName);
}
void main() {}
Alternatively:
#include "Sample1.h"
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
#pragma comment(lib, "Advapi32.lib")
JNIEXPORT jstring JNICALL Java_Sample1_fetchCurrentUserName(JNIEnv *env, jobject obj) {
DWORD nUserName = 100, dwErr;
std::vector<WCHAR> acUserName(nUserName);
do {
if (GetUserNameW(&acUserName[0], &nUserName)) {
--nUserName; // ignore the null terminator
wcout << L"User name is " << &acUserName[0] << L"." << endl;
break;
}
dwErr = GetLastError();
if (dwErr != ERROR_INSUFFICIENT_BUFFER) {
nUserName = 0;
cout << "Unable to get the User name, error " << dwErr << "." << endl;
break;
}
acUserName.resize(nUserName);
}
while (true);
return env->NewString(reinterpret_cast<jchar*>(&acUserName[0]), nUserName);
}
void main() {}
I have a java Web application to collect some data from local and remote windows machine. I am using wmi connection to connect to machine.
I use tomcat as Web server. For wmi connection i wrote c++ code and connect java and c++ using JNI. When i start the server and enter login details, tomcat crashes.
In log file error is..
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffd1e6e2931, pid=3940, tid=0x0000000000000a6c
#
# JRE version: Java(TM) SE Runtime Environment (8.0_162-b12) (build 1.8.0_162-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.162-b12 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [Verify_.dll+0x2931]
#
I am using tomcat 8.5.28 and jdk1.8.0_162
Here is my java code: Verify_.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Verify_ extends HttpServlet{
public native int connect();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
System.loadLibrary("Verify_");
Verify_ verify = new Verify_();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String ServerName = request.getParameter("servername");
String UserName = request.getParameter("username");
String Password = request.getParameter("password");
int status = verify.connect();
if(status == 0)
out.println("Connected to " + ServerName);
else if(status == 1)
out.println("failed to initialize CoInitializeEx");
else if(status == 2)
out.println("failed to initialize CoInitializeSecurity");
else if(status == 3)
out.println("failed to initialize CoCreateInstance");
else if(status == 4)
out.println("failed to connect to Server");
else
out.println(status);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
doGet(request,response);
}}
Here is JNI interface code: Verify_.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Verify_ */
#ifndef _Included_Verify_
#define _Included_Verify_
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Verify_
* Method: connect
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Verify_1_connect
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Here is C++ code: Verify_.cpp
#include <iostream>
#include <wbemidl.h>
#include <conio.h>
#include <windows.h>
#include <comdef.h>
#include <wincred.h>
#include <string.h>
#include "Verify_.h"
#pragma comment(lib, "credui.lib")
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "Ole32.lib")
#define _WIN32_DCOM
JNIEXPORT jint JNICALL Java_Verify_1_connect (JNIEnv *, jobject){
BSTR pszRoot,pszUserName,pszPassword;
wcscpy(pszRoot, BSTR(L"\\\\172.21.111.250\\ROOT\\cimv2"));
wcscpy(pszUserName, BSTR(L"Administrator"));
wcscpy(pszPassword, BSTR(L"qwerty1233"));
HRESULT hr;
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr)) {
return 1;
}
hr = CoInitializeSecurity(
NULL, // Security descriptor
-1, // COM negotiates authentication service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
NULL, // Authentication info
EOAC_NONE, // Additional capabilities of the client or server
NULL); // Reserved
if (FAILED(hr)) {
CoUninitialize();
return 2;
}
IWbemLocator *pLoc = 0;
hr = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr)){
CoUninitialize();
return 3;
}
IWbemServices *pSvc = NULL;
// Connect to the root\default namespace with the current user.
hr = pLoc->ConnectServer(
pszRoot, //namespace
pszUserName, // User name
pszPassword, // User password
NULL, // Locale
NULL, // Security flags
NULL, // Authority
NULL, // Context object
&pSvc); // IWbemServices proxy
if (FAILED(hr)){
pLoc->Release();
CoUninitialize();
return 4;
}
// pSvc->Release();
// pLoc->Release();
CoUninitialize();
return 0;
}
i am trying for past 5 days. Pls help me. If there is any other way tell me..
Thanks in advance
0xc0000005
-> Access violation, mostly because using a NULL pointer.
BSTR pszRoot,pszUserName,pszPassword;
wcscpy(pszRoot, BSTR(L"\\\\172.21.111.250\\ROOT\\cimv2"));
You have to allocate memory when using wcscpy. So, the wcscopy is called with an invalid (random) address (in Debugmode: With NULL).
I'm not sure you are handling BSTR correct (I did not know BSTR). See definition of BSTR:
typedef OLECHAR *BSTR;
Why not assign directly?
pszRoot = BSTR(L"\\\\172.21.111.250\\ROOT\\cimv2");
thanks Ralph Erdt
i changed this part
BSTR pszRoot,pszUserName,pszPassword;
pszRoot = ::SysAllocString(L"\\\\172.21.111.250\\ROOT\\cimv2");
pszUserName = ::SysAllocString(L"Administrator");
pszPassword = ::SysAllocString(L"qwerty1233");
For a few days I'm trying to build OpenALPR example project for Android. It builds and launches, but after calling native method for recognizing it make exception:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.UnsatisfiedLinkError: Native method not found: org.openalpr.AlprJNIWrapper.recognizeWithCountryRegionNConfig:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/String;
at org.openalpr.AlprJNIWrapper.recognizeWithCountryRegionNConfig(Native Method)
at org.openalpr.app.AlprFragment$AlprTask.doInBackground(AlprFragment.java:78)
at org.openalpr.app.AlprFragment$AlprTask.doInBackground(AlprFragment.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more
I can't do anything, it appears all the time.
Application.mk
APP_ABI := armeabi-v7a
APP_CPPFLAGS := -frtti -fexceptions
APP_STL := gnustl_static
Android.mk after all of my attempts
LOCAL_PATH := $(call my-dir)
LIB_PATH := $(LOCAL_PATH)/../libs/armeabi-v7a
include $(CLEAR_VARS)
LOCAL_MODULE := leptonica
LOCAL_SRC_FILES := 3rdparty/liblept.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := tesseract
LOCAL_SRC_FILES := 3rdparty/libtess.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := simpleini
LOCAL_SRC_FILES := 3rdparty/libsimpleini.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := support
LOCAL_SRC_FILES := 3rdparty/libsupport.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := openalpr
LOCAL_SRC_FILES := 3rdparty/libopenalpr-static.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=off
include d:\Other\robovisor_mobile\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk
LOCAL_MODULE := openalpr-native
SOURCE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp)
HEADER_LIST := $(wildcard $(LOCAL_PATH)/*.h)
LOCAL_SRC_FILES := AlprJNIWrapper.cpp
LOCAL_SRC_FILES += $(HEADER_LIST:$(LOCAL_PATH)/%=%)
LOCAL_SRC_FILES += $(SOURCE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_EXPORT_C_INCLUDES += /home/sujay/builds/src/openalpr/src/openalpr
LOCAL_EXPORT_C_INCLUDES += /home/sujay/builds/src/OpenCV-2.4.9-android-sdk/sdk/native/include
FILE_LIST := $(foreach dir, $(LOCAL_EXPORT_C_INCLUDES), $(wildcard $(dir)/*.cpp))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES += /home/sujay/builds/src/openalpr/src/openalpr
LOCAL_C_INCLUDES += /home/sujay/builds/src/OpenCV-2.4.9-android-sdk/sdk/native/include
LOCAL_C_INCLUDES += /home/sujay/tools/android-ndk-r10/platforms/android-19/arch-arm/usr/include
LOCAL_SHARED_LIBRARIES += tesseract leptonica
LOCAL_STATIC_LIBRARIES += openalpr support simpleini
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
AlprJNIWrapper.cpp contains needed for me native functions
/**
* Created by sujay on 13/11/14.
*/
#include <string>
#include <sstream>
#include <cstdio>
#include <iostream>
// openCV includes
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
// open alpr includes
#include "support/filesystem.h"
#include "support/timing.h"
#include "alpr.h"
#include "cjson.h"
#include "AlprJNIWrapper.h"
#include "AlprNative.h"
using namespace alpr;
JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_recognize(JNIEnv *env,
jobject object, jstring jimgFilePath, jint jtopN)
{
jstring defaultCountry = env->NewStringUTF("us");
jstring defaultRegion = env->NewStringUTF("");
jstring defaultConfigFilePath = env->NewStringUTF(CONFIG_FILE);
return _recognize(env, object, defaultCountry, defaultRegion, jimgFilePath, defaultConfigFilePath, jtopN);
}
JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_recognizeWithCountryNRegion(
JNIEnv *env, jobject object, jstring jcountry,
jstring jregion, jstring jimgFilePath, jint jtopN)
{
jstring defaultConfigFilePath = env->NewStringUTF(CONFIG_FILE);
return _recognize(env, object, jcountry, jregion, jimgFilePath, defaultConfigFilePath, jtopN);
}
JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_recognizeWithCountryRegionNConfig
(JNIEnv *env, jobject object, jstring jcountry, jstring jregion,
jstring jimgFilePath, jstring jconfigFilePath, jint jtopN)
{
return _recognize(env, object, jcountry, jregion, jimgFilePath, jconfigFilePath, jtopN);
}
jstring _recognize(JNIEnv *env, jobject object,
jstring jcountry, jstring jregion, jstring jimgFilePath,
jstring jconfigFilePath, jint jtopN)
{
const char* countryChars = env->GetStringUTFChars(jcountry, NULL);
std::string country(countryChars);
env->ReleaseStringUTFChars(jcountry, countryChars);
if(country.empty())
{
country = "us";
}
const char* configFilePathChars = env->GetStringUTFChars(jconfigFilePath, NULL);
std::string configFilePath(configFilePathChars);
env->ReleaseStringUTFChars(jconfigFilePath, configFilePathChars);
if(configFilePath.empty())
{
configFilePath = "/etc/openalpr/openalpr.conf";
}
const char* imgFilePath = env->GetStringUTFChars(jimgFilePath, NULL);
int topN = jtopN;
std::string response = "";
cv::Mat frame;
Alpr alpr(country, configFilePath);
const char* regionChars = env->GetStringUTFChars(jregion, NULL);
std::string region(regionChars);
env->ReleaseStringUTFChars(jregion, regionChars);
if(region.empty())
{
alpr.setDetectRegion(true);
alpr.setDefaultRegion(region);
}
alpr.setTopN(topN);
if (alpr.isLoaded() == false) {
env->ReleaseStringUTFChars(jimgFilePath, imgFilePath);
response = errorJsonString("Error initializing Open Alpr");
return env->NewStringUTF(response.c_str());
}
if(fileExists(imgFilePath))
{
frame = cv::imread(imgFilePath);
response = detectandshow(&alpr, frame, "");
}
else
{
response = errorJsonString("Image file not found");
}
env->ReleaseStringUTFChars(jimgFilePath, imgFilePath);
return env->NewStringUTF(response.c_str());
}
JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_version
(JNIEnv *env, jobject object)
{
return env->NewStringUTF(Alpr::getVersion().c_str());
}
std::string detectandshow(Alpr* alpr, cv::Mat frame, std::string region)
{
std::vector < uchar > buffer;
std::string resultJson = "";
cv::imencode(".bmp", frame, buffer);
std::vector < char > buffer1;
for(std::vector < uchar >::iterator i = buffer.begin(); i < buffer.end(); i++) {
buffer1.push_back(*i);
}
timespec startTime;
getTimeMonotonic(&startTime);
//std::vector < AlprResults > results = alpr->recognize(buffer);
AlprResults results = alpr->recognize(buffer1);
timespec endTime;
getTimeMonotonic(&endTime);
double totalProcessingTime = diffclock(startTime, endTime);
//if (results.size() > 0)
{
resultJson = alpr->toJson(results/*, totalProcessingTime*/);
}
return resultJson;
}
std::string errorJsonString(std::string msg)
{
cJSON *root;
root = cJSON_CreateObject();
cJSON_AddTrueToObject(root, "error");
cJSON_AddStringToObject(root, "msg", msg.c_str());
char *out;
out = cJSON_PrintUnformatted(root);
cJSON_Delete(root);
std::string response(out);
free(out);
return response;
}
AlprJNIWrapper.java calls native method
/**
*
*/
package org.openalpr;
/**
* #author sujay
*
*/
public class AlprJNIWrapper implements Alpr {
static {
System.loadLibrary("lept");
System.loadLibrary("tess");
System.loadLibrary("opencv_java");
System.loadLibrary("openalpr-native");
}
/* (non-Javadoc)
* #see org.openalpr.Alpr#recognize(java.lang.String, int)
*/
#Override
public native String recognize(String imgFilePath, int topN);
/* (non-Javadoc)
* #see org.openalpr.Alpr#recognizeWithCountryNRegion(java.lang.String, java.lang.String, java.lang.String, int)
*/
#Override
public native String recognizeWithCountryNRegion(String country, String region,
String imgFilePath, int topN);
/* (non-Javadoc)
* #see org.openalpr.Alpr#recognizeWithCountryRegionNConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, int)
*/
#Override
public native String recognizeWithCountryRegionNConfig(String country,
String region, String imgFilePath, String configFilePath, int topN);
/*
* (non-Javadoc)
* #see org.openalpr.Alpr#version()
*/
#Override
public native String version();
}
Edited
There is info about processor on my phone.
Processor : ARMv7 Processor rev 3 (v7l)
processor : 0
BogoMIPS : 1993.93
Features : swp half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpv4
idiva idivt
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 3
Edited again
I tried to get info about function in result .so file and there is what I get:
Symbol table '.dynsym' contains 6 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 FUNC GLOBAL DEFAULT UND __cxa_finalize
2: 00000000 0 FUNC GLOBAL DEFAULT UND __cxa_atexit
3: 00002004 0 NOTYPE GLOBAL DEFAULT ABS _edata
4: 00002004 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
5: 00002004 0 NOTYPE GLOBAL DEFAULT ABS _end
I get it from:
<ndk_path>\toolchains\arm-linux-androideabi-4.8\prebuilt\windows\bin>arm-linux-androideabi-readelf.exe
-Ws <projects_path>\libs\armeabi-v7a\libopenalpr-native.so
Am I doing something wrong? Or there is really no my functions in .so file?
There are could be several issues.
First, you run the sample on unsupported device, like if lib built for armeabi-v7 (and its definitely so due to APP_ABI := armeabi-v7a setting in make file) and your device is intel x86 or lower than 7 armeabi version, etc.
Could be that sample is outdated while lib project has been updated, so some method names was changed or method was removed as deprecated, etc.
Also compiled NDK lib is package sensitive so if you place JNI class into a different package it wont work either.
Also the native library .so-file has to be placed into the right place of your project and its not a libs folder where you place jar-libs usually. Its a bit different folder like as for android studio:
...\src\main\jniLibs\aremabi-v7a\libYourLib.so (for aremabi-v7a version)
...\src\main\jniLibs\x86\libYourLib.so (for x86 version and so on)
UPDATE:
Don't forget to respect the min API level which is 19. App wont work on devices with lower API level, I mean you could change the min API level in project - don't do it.
The issue here is that libopenalpr-native.so has a dash - char in its name. Dash is a restricted char for resource naming in AOS. So I replaced it with "_" and app works now
And replace it here as well:
System.loadLibrary("openalpr_native");
And I didn't use your version of .so but only the included in project one.
UPDATE
Take a look: http://prntscr.com/6w6qfx your lib is just 5kb comparing to original 1.7Mb there is something wrong definitely. And it explains your question in comment:
Why there is no error on System.loadLibrary("openalpr-native");? I
just can't understand this situation - creates libopenalpr-native.so
file, it loads to program, by there is no method.
Could you just use original lib included in sample project instead? Just to test.
No correct method found in your library is due to name mismatch, you should wrap your JNI function code with extern "C" in C++ code.
I used to have some privilege issues when calling ExitWindowsEX Windows API function.
So I wrote the following code to get the privilege:
This works fine in C++
#include <cstdlib>
#include <windows.h>
#include <iostream>
using namespace std;
/*
*
*/
int MyExitWindows(int flag, int reason);
int main(int argc, char** argv) {
MyExitWindows(EWX_SHUTDOWN, 0);
}
int MyExitWindows(int flag, int reason) {
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return GetLastError();
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
ExitWindowsEx(flag, reason);
if (GetLastError() != ERROR_SUCCESS) {
return GetLastError();
}
return 0;
}
But this doesn't work when I call it from Java
#include <jni.h>
#include <cstdlib>
#include <windows.h>
#include "com_ehsunbehravesh_jshutdown_system_Shutdowner.h"
using namespace std;
int MyExitWindows(int flag, int reason);
JNIEXPORT jint JNICALL Java_com_ehsunbehravesh_jshutdown_system_Shutdowner_exitWindowsEx
(JNIEnv *env, jobject obj, jlong flag, jlong reason) {
return MyExitWindows(flag, reason);
}
int MyExitWindows(int flag, int reason) {
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
int cpid = GetCurrentProcessId();
printf("%d", cpid);
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return GetLastError();
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
ExitWindowsEx(flag, reason);
if (GetLastError() != ERROR_SUCCESS) {
return GetLastError();
}
return 0;
}
Is there any reason you are not using System.exit(int)?
Java attempts to control the shutdown of an application, perhaps it tries to prevent you doing it other ways.