Calling JNI function
I do have a question:
there are two possibilities to make a JNI call,
i.e. in C:
jsting jstr = (*env)->NewStringUTF(env, "");
or in C++:
jsting jstr = env->NewStringUTF("");
when i compile using (gcc, g++ respectively) the first line produces an
base operand of `->' has non-pointer type `JNIEnv_'
error while the compilation with the second line works without any problem.
there are many example programs that use the syntax of the first line, but how does one get these things to work?
env is defined as follows:
JNIEnv *env;
sure, i could use the other syntax but as the most examples in the web use the syntax as denoted in the first line i thought that it should work too. otherwise i would have to replace the syntax in these programs.
Similar Messages
-
I get EXCEPTION_ACCESS_VIOLATION by calling JNI function.
Hello Everyone.
I need someone's help that I have a problem with JNI.
I got an error as below when a Java program calls JNI function that I created with VC++.
JNI functions are called not once and got an error not first call.
First of all, The JNI function is called at Main function.
then, Main function create another thread.
and next JNI function is called in another thread.
when the JNI function is called first time is suceeded.
but, second time is always failed.
I wrote down some of detail in source code's comment.
// ErrorLog is from here ------------------------------------------------------
# An unexpected error has been detected by HotSpot Virtual Machine:
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d7c7c5f, pid=2460, tid=3144
# Java VM: Java HotSpot(TM) Client VM (1.5.0_14-b03 mixed mode)
# Problematic frame:
# V [jvm.dll+0x87c5f]
--------------- T H R E A D ---------------
Current thread (0x00843650): JavaThread "Thread-0" [_thread_in_vm, id=3144]
siginfo: ExceptionCode=0xc0000005, reading address 0x00000009
Registers:
EAX=0x00000001, EBX=0x06d77d40, ECX=0x00000008, EDX=0x00843710
ESP=0x0afaf4a8, EBP=0x0afaf4d8, ESI=0x0afaf4ec, EDI=0x0afaf53c
EIP=0x6d7c7c5f, EFLAGS=0x00010246
Top of Stack: (sp=0x0afaf4a8)
0x0afaf4a8: 0afaf4ec 06d77d40 6d7cfa7e 00000001
0x0afaf4b8: 0afaf53c 0afaf4ec 06d77d40 00843070
0x0afaf4c8: 00841a10 00841a18 00841e04 00843650
0x0afaf4d8: 0afaf53c 100020a6 00843710 0082f3d0
0x0afaf4e8: 0afaf5a0 0afaf5a4 06d77d40 06d77d40
0x0afaf4f8: cccccccc cccccccc cccccccc cccccccc
0x0afaf508: cccccccc cccccccc cccccccc cccccccc
0x0afaf518: cccccccc cccccccc cccccccc cccccccc
Instructions: (pc=0x6d7c7c5f)
0x6d7c7c4f: 5f 5e 5b c3 8b 44 24 04 8b 0d 14 e2 8b 6d 53 56
0x6d7c7c5f: 8b 34 01 8b 0d 10 e2 8b 6d 57 8b 3c 01 8b 0d 0c
Stack: [0x0af70000,0x0afb0000), sp=0x0afaf4a8, free space=253k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V [jvm.dll+0x87c5f]
C [FilterDriver.dll+0x20a6]
C [FilterDriver.dll+0x16c7]
C [FilterDriver.dll+0x1c59]
C [FilterDriver.dll+0x22b3]
j xx.xx.xx.xx.xx.TestClass.get(Ljava/lang/String;)Z+0
j xx.xx.xx.xx.xx.ClassExtendedThread.run()V+24
v ~StubRoutines::call_stub
V [jvm.dll+0x875dd]
V [jvm.dll+0xdfd96]
V [jvm.dll+0x874ae]
V [jvm.dll+0x8720b]
V [jvm.dll+0xa2089]
V [jvm.dll+0x1112e8]
V [jvm.dll+0x1112b6]
C [MSVCRT.dll+0x2a3b0]
C [kernel32.dll+0xb683]
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j xx.xx.xx.xx.xx.TestClass.get(Ljava/lang/String;)Z+0
j xx.xx.xx.xx.xx.ClassExtendedThread.run()V+24
v ~StubRoutines::call_stub
--------------- P R O C E S S ---------------
Java Threads: ( => current thread )
0x008433b0 JavaThread "DestroyJavaVM" [_thread_blocked, id=2676]
=>0x00843650 JavaThread "Thread-0" [_thread_in_vm, id=3144]
0x0083d5f0 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=2024]
0x0083c730 JavaThread "CompilerThread0" daemon [_thread_blocked, id=3444]
0x0083b560 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=2784]
0x00839970 JavaThread "JDWP Command Reader" daemon [_thread_in_native, id=2876]
0x00838c60 JavaThread "JDWP Event Helper Thread" daemon [_thread_blocked, id=2980]
0x00836250 JavaThread "JDWP Transport Listener: dt_socket" daemon [_thread_blocked, id=536]
0x0082e410 JavaThread "Finalizer" daemon [_thread_blocked, id=2100]
0x0082d190 JavaThread "Reference Handler" daemon [_thread_blocked, id=1968]
Other Threads:
0x0082c470 VMThread [id=296]
0x0083fd50 WatcherThread [id=464]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap
def new generation total 576K, used 300K [0x02bd0000, 0x02c70000, 0x030b0000)
eden space 512K, 58% used [0x02bd0000, 0x02c1b200, 0x02c50000)
from space 64K, 0% used [0x02c50000, 0x02c50000, 0x02c60000)
to space 64K, 0% used [0x02c60000, 0x02c60000, 0x02c70000)
tenured generation total 1408K, used 0K [0x030b0000, 0x03210000, 0x06bd0000)
the space 1408K, 0% used [0x030b0000, 0x030b0000, 0x030b0200, 0x03210000)
compacting perm gen total 8192K, used 1711K [0x06bd0000, 0x073d0000, 0x0abd0000)
the space 8192K, 20% used [0x06bd0000, 0x06d7bf00, 0x06d7c000, 0x073d0000)
No shared spaces configured.
Dynamic libraries:
0x00400000 - 0x0040d000 C:\Program Files\Java\jdk1.5.0_14\bin\javaw.exe
0x7c940000 - 0x7c9dd000 C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c932000 C:\WINDOWS\system32\kernel32.dll
0x77d80000 - 0x77e29000 C:\WINDOWS\system32\ADVAPI32.dll
0x77e30000 - 0x77ec1000 C:\WINDOWS\system32\RPCRT4.dll
0x77cf0000 - 0x77d7f000 C:\WINDOWS\system32\USER32.dll
0x77ed0000 - 0x77f17000 C:\WINDOWS\system32\GDI32.dll
0x77bc0000 - 0x77c18000 C:\WINDOWS\system32\MSVCRT.dll
0x762e0000 - 0x762fd000 C:\WINDOWS\system32\IMM32.DLL
0x60740000 - 0x60749000 C:\WINDOWS\system32\LPK.DLL
0x73f80000 - 0x73feb000 C:\WINDOWS\system32\USP10.dll
0x6d740000 - 0x6d8de000 C:\Program Files\Java\jdk1.5.0_14\jre\bin\client\jvm.dll
0x76af0000 - 0x76b1b000 C:\WINDOWS\system32\WINMM.dll
0x6d300000 - 0x6d308000 C:\Program Files\Java\jdk1.5.0_14\jre\bin\hpi.dll
0x76ba0000 - 0x76bab000 C:\WINDOWS\system32\PSAPI.DLL
0x6d400000 - 0x6d435000 C:\Program Files\Java\jdk1.5.0_14\jre\bin\jdwp.dll
0x6d710000 - 0x6d71c000 C:\Program Files\Java\jdk1.5.0_14\jre\bin\verify.dll
0x6d380000 - 0x6d39d000 C:\Program Files\Java\jdk1.5.0_14\jre\bin\java.dll
0x6d730000 - 0x6d73f000 C:\Program Files\Java\jdk1.5.0_14\jre\bin\zip.dll
0x6d290000 - 0x6d297000 C:\Program Files\Java\jdk1.5.0_14\jre\bin\dt_socket.dll
0x719e0000 - 0x719f7000 C:\WINDOWS\system32\WS2_32.dll
0x719d0000 - 0x719d8000 C:\WINDOWS\system32\WS2HELP.dll
0x71980000 - 0x719bf000 C:\WINDOWS\System32\mswsock.dll
0x76ed0000 - 0x76ef7000 C:\WINDOWS\system32\DNSAPI.dll
0x76f60000 - 0x76f68000 C:\WINDOWS\System32\winrnr.dll
0x76f10000 - 0x76f3c000 C:\WINDOWS\system32\WLDAP32.dll
0x76f70000 - 0x76f76000 C:\WINDOWS\system32\rasadhlp.dll
0x607c0000 - 0x60816000 C:\WINDOWS\system32\hnetcfg.dll
0x719c0000 - 0x719c8000 C:\WINDOWS\System32\wshtcpip.dll
0x10000000 - 0x10076000 C:\xx\whaticreated.dll
0x71a50000 - 0x71a62000 C:\WINDOWS\system32\MPR.dll
VM Arguments:
jvm_args: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:1759
java_command: xx.xx.xx.xx.xx.Sample start
Launcher Type: SUN_STANDARD
Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_14
PATH=C:\WINDOWS\system32;C:\WINDOWS;D:\tools\apache-ant-1.7.0\bin;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;D:\PROGRA~1\ATT\Graphviz\bin;D:\Program Files\doxygen\bin;D:\Program Files\Microsoft SDK\Bin\.;D:\Program Files\Microsoft SDK\Bin\WinNT\.;C:\Program Files\Java\jdk1.5.0_14\bin;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin;D:\Program Files\Microsoft SDK\Bin\.;D:\Program Files\Microsoft SDK\Bin\WinNT\.
USERNAME=xxx
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 2, GenuineIntel
--------------- S Y S T E M ---------------
OS: Windows XP Build 2600 Service Pack 2
CPU:total 2 (cores per cpu 2, threads per core 1) family 6 model 15 stepping 2, cmov, cx8, fxsr, mmx, sse, sse2
Memory: 4k page, physical 1038124k(270512k free), swap 1711740k(953716k free)
vm_info: Java HotSpot(TM) Client VM (1.5.0_14-b03) for windows-x86, built on Oct 5 2007 01:21:52 by "java_re" with MS VC++ 6.0
// The end of ErrorLog --------------------------------------------------------
and the codes are below.
// TestClass.java
public class TestClass {
// Load native library
static{
System.loadLibrary("DLLName");
public native int start(String str, int count);
public native int end();
public native boolean get(String str);
// ClassExtendedThread.java
public class ClassExtendedThread extends Thread {
private TestClass tc;
// Constructor
public ClassExtendedThread(TestClass in_tc)
ts = in_tc;
// This will be executing in another thread
public void run()
while( true )
if( !false )
String str = new String();
// get function returns false means nothing to catch event.
if( false == ts.get( str ) ) // calling first time is suceed. but, I got an error second time.
// Set Interval for loop.
// Sample.java
public class Sample {
public static void main(String[] args) {
TestClass tc = null;
ClassExtendedThread cet = null;
tc = new TestClass();
// Init something
if(0 != TestClass.start(xxx, xxx)) // First JNI function suceeds.
System.out.println("start error.");
return;
// Starting Thread
cet = new ClassExtendedThread(tc);
cet.start();
}Have anyone got any idea to resolve this problem?
Edited by: fc3srx7m on Apr 25, 2008 3:56 AMHi Martin.
Thank you for your advice.
I thought the problem is in the FilterDriver.dll as well.
I tried DebugBreak() into native code.
but, I still can't find where the problem is.
I'm trying to convert C structure to Java class like using structConverter which is tool of book for JNI.
Here's some code including native code.
// Java source codes //////////////////////////////////////////////////////////////////////////////////
// Event.java ----------------------------------------------------------------------------------------------
// This class for C structure Convert
public class Event {
private int aaa;
private long bbb;
private String ccc;
// Getter/Setter
public int getAaa() {
return aaa;
public void setAaa(int aa) {
aaa = aa;
public long getBbb() {
return bbb;
public void setBbb(long cc) {
bbb = bb;
public String getCcc() {
return ccc;
public void setCcc(String cc) {
ccc = cc;
// Field Initialize
private native static void initFIDs();
static {
initFIDs();
// EventStruct,java --------------------------------------------------------------------------------------
public class EventStruct extends Event {
private void _init()
setAaa(0);
setBbb(0);
setCcc(new String());
// Constructor
public LogEventStruct()
_init();
System.loadLibrary("FilterDriver");
// EndOfJavaSource ////////////////////////////////////////////////////////////////////////////////////
// Native source code /////////////////////////////////////////////////////////////////////////////////
// Event.cpp ----------------------------------------------------------------------------------------------
// FieldIDs
static jfieldID Event_Aaa_FID;
static jfieldID Event_Bbb_FID;
static jfieldID Event_Ccc_FID;
// Initialize Adapter
static void initEventFieldIDs(JNIEnv* env, jclass clazz)
Event_Aaa_FID = env->GetFieldID(clazz, "aaa", "I");
Event_Bbb_FID = env->GetFieldID(clazz, "bbb", "J");
Event_SFlNm_FID = env->GetFieldID(clazz, "ccc", "Ljava/lang/String;");
// Call from Java
JNIEXPORT void JNICALL Java_Event_initFIDs(JNIEnv *env, jclass clazz)
initEventFieldIDs(env, clazz);
// Setter
void jni_SetAaa_in_Event(EVENT* __EVENT_, JNIEnv *env, jobject thisaaa)
env->SetIntField(thisaaa, Event_Aaa_FID, (jint)__EVENT_->aaa);
// Getter
void jni_GetAaa_from_Event(EVENT* __EVENT_, JNIEnv *env, jobject thisaaa)
__EVENT_->aaa = (int)env->GetIntField(thisaaa, Event_Aaa_FID);
// Setter
void jni_SetBbb_in_Event(EVENT* __EVENT_, JNIEnv *env, jobject thisbbb)
env->SetLongField(thisbbb, Event_Bbb_FID, __EVENT_->bbb);
// Getter
void jni_GetBbb_from_Event(EVENT* __EVENT_, JNIEnv *env, jobject thisbbb)
__EVENT_->bbb = (unsigned int)env->GetLongField(thisbbb, Event_Bbb_FID);
// Setter
void jni_SetCcc_in_Event(EVENT* __EVENT_, JNIEnv *env, jobject thisccc)
env->SetObjectField(thisccc, Event_Ccc_FID, env->NewStringUTF(__EVENT_->ccc));
// Getter
void jni_GetCcc_from_Event(EVENT* __EVENT_, JNIEnv *env, jobject thisccc)
jboolean isCopy;
const char *str = __EVENT_->ccc;
jstring jstr = (jstring)env->GetObjectField(thisccc, Event_Ccc_FID);
str = env->GetStringUTFChars(jstr, &isCopy);
if(JNI_TRUE == isCopy)
env->ReleaseStringUTFChars(jstr, str);
// Getting (Java Object to C Structure)
void jni_SetAll_in_Event(EVENT *__EVENT_, JNIEnv *env, jobject thisEvent)
jni_SetAaa_in_Event(__EVENT_, env, thisEvent);
jni_SetBbb_in_Event(__EVENT_, env, thisEvent);
jni_SetCcc_in_Event(__EVENT_, env, thisEvent);
// Setting (C structure to Java Object)
void jni_GetAll_in_Event(EVENT *__EVENT_, JNIEnv *env, jobject thisEvent)
jni_GetAaa_from_Event(__EVENT_, env, thisEvent);
jni_GetBbb_from_Event(__EVENT_, env, thisEvent);
jni_GetCcc_from_Event(__EVENT_, env, thisEvent);
// AnotherLib.h ------------------------------------------------------------------------------------
// C Structure which I want to convert.
typedef struct EVT {
int aaa;
unsigned int bbb;
char ccc[256];
} EVENT;
// prototype
BOOL GetEvent( EVENT* buf, char *name );
// test.h -----------------------------------------------------------------------------------------------------
#if !defined(AFX_TEST_H__F9A4A432_24D3_4DE4_89D7_A2009B8EC2AA__INCLUDED_)
#define AFX_TEST_H__F9A4A432_24D3_4DE4_89D7_A2009B8EC2AA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
#include "AnotherLib.h"
#include "Event.h" // output by javah command
extern void jni_GetAll_in_Event(EVENT *__EVENT_, JNIEnv *env, jobject thisEvent);
extern void jni_SetAll_in_Event(EVENT *__EVENT_, JNIEnv *env, jobject thisEvent);
#endif // !defined(AFX_TEST_H__F9A4A432_24D3_4DE4_89D7_A2009B8EC2AA__INCLUDED_)
// test.cpp --------------------------------------------------------------------------------------------------
JNIEXPORT jboolean JNICALL Java_get(JNIEnv *env, jobject obj, jstring name)
EVENT s_Event;
char strName[_MAX_PATH];
//DebugBreak();
memset(&s_Event, 0x00, sizeof(EVENT));
memset(strName, 0x00, _MAX_PATH);
jni_SetAll_in_Event(&s_Event, env, obj);
BOOL ret = GetEvent(&s_Event, strName);
if(TRUE == ret)
jni_GetAll_in_Event(&s_Event, env, obj);
// convert char array to jstring
name = env->NewStringUTF(strName);
return ret;
// EndOfNativeSource ////////////////////////////////////////////////////////////////////////////////Edited by: fc3srx7m on Apr 27, 2008 10:05 PM -
Help calling JNI function from a java thread
I am using the following code to call a JNI fuction from a java thread:
public class CMSMessageHandler extends MessageHandler{
static{
System.loadLibrary("jastb");
public native int cmsReadIt(ByteBuffer buff);
private ByteBuffer dirbuf = null;
/** Creates a new instance of CMSMessageHandler */
public CMSMessageHandler() {
// allocate to max size datagram
dirbuf = ByteBuffer.allocateDirect(65536);
public void readMessages(){
/*Thread worker = new Thread(){
public void run(){*/
int count;
while (true){
try{
count = cmsReadIt(dirbuf);
} catch (Exception e){
e.printStackTrace();
worker.start();*/
public static void main(String[] args) {
CMSMessageHandler handler = new CMSMessageHandler();
handler.readMessages();
When I run this main method with the thread commented out, it works great. When I run the main method with the thread, it works great for a while, and then funny things start happening with the native library I am using. My native library uses shared memory, signals, and semaphores. It feels a bit like my java program is stomping on the resources that my native library is using, when a run the java program with a thread.
Is there something I don't know about calling native code from a thread? Should I be doing something explicitly to protect my native library's shared memory, signals, or semaphores?
Thanks,
Lisa.Does the library do any initialization when loaded? Does it make a dangerous assumption that the thead calling cmdReadIt will always be the same thread that initially loaded it? Try loading the library in the worker thread instead of in the main thread via the static initializer.Yes. There is a call to another native method cmsOpenIt that does CMS initialization. I put cmsOpenIt as well as the System.loadLibrary("jastb") in the worker thread and I'm still experiencing the same problems.
Incidently, the mere presence of another Thread in this program does not seem to cause problems. It's only when I run calls to cmsReadIt in a thread that I get into trouble.
For example, this works fine:
public class CMSMessageHandler extends MessageHandler{
public native int cmsReadIt(ByteBuffer buff);
public ByteBuffer dirbuf = null;
static {
System.loadLibrary("jastb");
public CMSMessageHandler() {
// allocate to max size datagram
dirbuf = ByteBuffer.allocateDirect(65536);
public void readMessages(){
try{
int count = 0;
while (true){
count = cmsReadIt(dirbuf);
} catch (Exception e){
public static void main(String[] args) {
CMSMessageHandler handler = new CMSMessageHandler();
Thread worker = new Thread(){
public void run(){
while (true){
try{
Thread.currentThread().sleep(1000);
System.out.println("sleep thread.");
} catch (Exception e){
e.printStackTrace();
worker.start();
handler.readMessages();
}Are there JVM flags that I should experiment with that might help mitigate/diagnose this problem? -
Java Webservice calling C function through JNI
I have a java class that is using the JNI to call native interface written in C. The called C function has dependencies in vendor supplied c function library in .lib (CAUEVT.LIB) format. I compiled the C function as .DLL file and put the .DLL file in the -Djava.library.path and same thing I put in the opmn.xml file for the Java option.
Now, when I run the Java class from command line it works fine and makes call to the external system everything works fine.
However, when I deploy the same as web service and try to test the web service, Java expects the external library also in .DLL format (The error message is "This application has failed to start because CAUEVT.dll was not found. Re-installing the application may fix this problem")
If I look the application log file, I see the following error.
java.lang.UnsatisfiedLinkError: D:\lib\CAEventLoggerImp.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1676)
I am wondering if the java class can run without any problem, why is the webservice execution fails....?The problem was that when Java loads the shared DLL we create from the java.library.path setting, but if the loaded DLL refers to any other DLL, it expects it from the System path. And for some reason, OC4J, doesn't expose all the system path unless entered in the Evnironment Variable explicitly in the Administration tab in OEM., server properties in the OEM. (opmn.xml)
IT works fine after these changes. -
Call Java functions in a C DLL
Hi,
I am develop a dynamic link library in C on AIX that will internally call java functions using JNI. This libaray will be finally used by other C Executable.
There is only one function in C code i.e. Connect. This function internally use JNI and call java functions. Now while compiling I am facing problems. I think that there is some problem with my makefile.
The make file is given hereunder:
JAVA_HOME = /usr/java14
JAVA_INC = $(JAVA_HOME)/include
CC = cc
LD = cc
# Flags to create a dynamic library.
DYNLINKFLAGS = -G -ostdlib -bnoentry -bM:SRE -brtl -bE:Interface.exp
# files removal
RM = rm -f
#------------------------------------- Libs -----------------------------------#
JAVALIBS = -L$(JAVA_HOME)/jre/bin/classic/
LIBS = -ljvm -lpthread -lxnet -lnsl -lm -ldl
#-------------------------------- Dependency rules---------------------------#
# shared library files
LIB_FILES = Interface.a
#-------------------------------------OBJs-------------------------------------#
# shared libraries object files
LIB_OBJS = Interface.o
all: $(LIB_FILES)
# create our librarys
Interface.a: Interface.o
$(LD) $(DYNLINKFLAGS) $(JAVALIBS) $(LIBS) $(LIB_OBJS) -o $@
# compile C source files into object files.
%.o: %.c
$(CC) $(DYNLINKFLAGS) -L$(JAVA_INC) -c $<
# clean everything
clean:
$(RM) $(LIB_OBJS) $(LIB_FILES)
# clean the library's object files only
cleanlibobjs:
$(RM) $(LIB_OBJS)Now the problem is that the size of the Interface.a file is very small only 552 bytes. whereas the size of Interface.o file is 34428. Which clearly indicates that the problem is with the Interface.a file i.e during linking.
Can any body help me to figure out the problem.
Regards,
Ahmad Jalil QarshiI guess, you are better off to ask this question in a C forum. This is a Java forum, you know.
-
With java how to call a function offered by user32.dll?
Hi,everyone.
Please think about my question.Now i want to retrieve all the user's
information in NT OS.will it be possible? could it be done by calling a method
of dll? i have tried to define a native method,but i got java.lang.UnsatisfiedLinkerror when i execute the class, what will be the problem?
can you please offer me a piece of code like it. but the dll should be available with the OS,such as user32.dll,kernal32.dll ...ect, not the one using jni to generate.
Thank you for suggestions.can you please offer me a piece of code like it. but
the dll should be available with the OS,such as
user32.dll,kernal32.dll ...ect, not the one using jni
to generate.You cannot directly call C functions in this way. You have to use JNI. Have your native method written to normal JNI standards call the functions in the OS. Your native method will also have to take care of creating the data structures needed to make the OS call and also creating the Java data structure you want to return. -
Calling C Functions in existing DLL's from Java
Hi Guys ,
The tutorial in this site talks about creating ur own DLL's and then calling them from Java . I need to call C functions in existing DLL's from Java . How do I go about doing this ? . Any help on this would be much appreciated.
regards
muraliWhat you are interested in can be done with what's called "shared stubs", from the JNI book (http://java.sun.com/products/jdk/faq/jnifaq.html), although you don't need the book to do it (I didn't).
The example code will call functions with any number and kind of parameters, but doing that requires some assembly language. They supply working examples for Win32 (Intel architecture) and Solaris (Sparc).
If you can limit yourself to functions to a single function signature (number and types of parameters), or at least a small set that you know you'll call at compile time, you can modify the example so that the assembly language part isn't needed, just straight C code.
Then you'll have one C library that you compile and a set of Java classes and you can load arbitrary functions out of arbitrary dynamic libraries. In my case you don't even have to know what the libraries and functions are ahead of time, the user can set that up in a config file.
You mentioned doing this with Delphi. One thing to watch out for is C versus Pascal (Win32) function calling convention. A good rule of thumb; if it crashes hard, you probably picked the wrong one, try the other. :-) -
Calling c++ function in java
Hi,
I would like to know how I can modify c++ program so that I can use its(c++) functions in java.
the program is c++ ready-made and i would like to use its functions in a java package.
i read the turiols concerning this on java.sun.com but does that mean that i have append "jni.h", "JNIEXPORT", etc to the c++ file?
Hints shall be appreciated.
Thanks.OK, thanks.I have tried the jni procedure but without success.
Is it possible for Java talk to native code (c++) via TCP/IP.
Please advice of any links/knowledge.
Thanks.
1. Strictly speaking, java does not operate on C++; it
operates on C functions. This is more than a quibble:
o JNI expects to call C functions, not methods of C++
objects.
o In fact, if you really just have functions, but they
are compiled as C++, then you will probably have to
declare C linkage.
2. Functions called from java must conform to JNI
calling conventions.
o In general, the starting point is to define how you
would like the object and native method to look in
java, then run jni.h to generate the appropriate C
header definitions, then write the C code.
o Functions called from java must exist in shared
libraries - on Windows, in dlls.
o Existing C function libraries are typically accessed
by writing a "wrapper" dll that implements the
required java interface and then calls the existing
library functions.
o In fact, a wrapper could actually instantiate C++
objects and call their methods.
3. You might care to look up something called JACE,
which some people claim can generate the kinds of
wrappers discussed here. -
How can I call a function in C or C++
hello friends, I want to write a prog. in Java to call a function written in C or C++ without disturbing the c/c++ function
thanksUse the Java Native Interface (JNI). See the Java Tutorial at http://www.javasoft.com/docs/books/tutorial/native1.1/index.html
-
Call native function fflush & strprn from java
Hi,
I need to call a native function fflush and stdprn from java for printing a file.
How can i call this function?
Can any one help me with sample code.
Thanks,
rpalaniveluThanks your reply,
Actually my problem is need to take printout using dot matrix printer(text printer) with different font size.
So, i am using native method which is available in c.
in c program i am using fflush,stdprn and fprintf.
Here i've attached my sample program also.
#include <jni.h>
#include "NativePrint.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE_LEN 100
JNIEXPORT jint JNICALL
Java_NativePrint_dotMatrixPrint(JNIEnv* env, jobject obj )
FILE *fp;
char filename[20];
char line[100]; /* A line read from the file */
int lineNumber = 0;
/* Print control codes */
char boldOn = 14;
char boldOff = 20;
char contentlenOn = 15;
char contentlenOff = 18;
printf("Printing from C3");
strcpy( filename , "sayHello.c" );
/* Open the file in read mode */
fp = fopen( filename , "r" );
/* Error in opening file, then exit */
if ( fp == NULL )
printf( "\nERROR: %s cannot be opened\n" , filename );
exit( 1 );
while ( fgets( line , 100 , fp ) != NULL )
lineNumber++; /* Line we are about to process next */
printf("%s",line);
/* If this is the first line */
if ( lineNumber == 1 )
/* then print it in bold */
//fprintf( stdprn , "%c" , boldOn );
fprintf( stdprn , "%c" , contentlenOn );
fprintf( stdprn , line );
fprintf( stdprn , "%c" , contentlenOff);
//fprintf( stdprn , "%c" , boldOff );
else
/* else print it in normal mode */
fprintf( stdprn , line );
fflush(stdprn);
return 0; -
How to call a function from a JAR file
Hi ALL,
I am trying to call a Public function from a JAR file from my JAVA
file. Can you please provide an example or suggest a good resource?
Basically my java file Test.Java should call a function called 'export_a_doc' from a JAR file Export.jar. How do I refer this in my Java file and call 'export_a_doc'?
ThanxHi,
HERE IS MY JAVA FILE
// We need this for Starting our Export Process
import com.newexport.Myclass;
public class MyTest {
// Our function to Start Export
protected static void test(String[] args) {
System.out.println("Starting. \n---------------------");
Myclass p = new Myclass();
try {
p.Start_Export(args);
System.out.println("Finished. \n---------------------");
} catch(Exception e) {
e.printStackTrace();
public static void main(String[] args) {
test(args);
HERE IS THE BATCH FILE TO BUILD AND RUN
cd \JNI\Srcs
echo "-------------"
c:\JBuilder7\jdk1.3.1\bin\javac -classpath C:\JNI\Export.jar MyTest.java
echo "-------------"
\JBuilder7\jdk1.3.1\bin\java.exe -classpath c:\JNI\Export.jar MyTest
The Export.jar uses other jar files
Raj -
How to call Java function which returns byteArray
context I have a class say
public class ByteArray{
public byte[] getByteArray(String str){
return str.getBytes();
}Consider i have object of ByteArray (some how) in native C code. I want to call getByteArray("Test") and obtain a byteArray. what is the C JNI function can i use?hy
CREATE OR REPLACE FUNCTION GET_SAL1(NN in NUMBER)
RETURN BOOLEAN
IS
BEGIN
INSERT INTO STD(ENO) VALUES(NN);
RETURN TRUE;
EXCEPTION
WHEN OTHERS THEN
RETURN false;
END;
you can call :
if GET_SAL1(NN) then
else
error
end if;
I hope i understand
Regards -
Returning values from JNI functions to Java
I have a JNI function which returns a "jstring" to the calling Java function.
I need also to return 2 byte values.
Can I return them all...? if so, how?I have a JNI function which returns a "jstring" to the
calling Java function.
I need also to return 2 byte values.
Can I return them all...? if so, how?"Also"? Then you do the same thing as if you were returning two different types in java only code, you wrap it in an object. -
Calling C++ Functions
Is there a way to call C++ functions from an application that is written in Java? Any help would be appreciated.
Yes, its called jni
http://java.sun.com/docs/books/tutorial/native1.1/index.html -
HI!
What's the best way (and simple) to call DLL function from JAVA.
DLL function (developed in C) has two input parameters (string) and return an integer.
Could you help with an example?
Thanks a lot.Do a google search for 'JNI tutorial' and it will turn up hundreds of examples.
Maybe you are looking for
-
How to Default sales office and group in Organisation tab of crmd_order
Hi Experts, In crmd_order transaction when user enters sold-to-party, then organisational determination takes place which brings sales area,distribution channel and division automatically under organisation tab. My requirement is to fetch the corresp
-
Assignment of different Dicument Type in Automatic Payment Program
Hi SAP Gurus, Presently the payments are being handled centrally at Corp Office in a State. The payment method is C and the clearing doc type is KZ. Now the Company is also starting to use APP at another office in another state. The payment method is
-
Parental Controls - love em, hate em
I really like the parental controls in Yosemite - nice granularity and good job Apple. However, there is one challenge that I face that hope as an easy answer that I may be overlooking. I have Web parental controls set to the default of 'Try to limi
-
Possible memory leak in forms api 6i
There appears to be a bug in forms 6i. I am getting an error at the following snippet of code. The Name prints fine and the whole code runs great if i comment out free(v_value), but obviously i would not want to do that. I am using MS VC++ 6.0 compil
-
I need to turn bonjour on for my iphone to connect to itunes
when i try to connect my iphone4 to itunes it says i cannot connect because my moblie device is not started, then it says something about bonjour needs to be turn back on