No output from executable using JNI

Hi, Guys -
New to JNI, have a question: I can get both my C and Java code to compile, a .dll to build, and the program to run, but I get no output. Execution seems to endl at the System.loadLibrary() call. I've tried to get it running in both cmd and cygwin windows without success.
Any ideas?
Thanks.
----- Environment/platform/makefile info ------
Win2kPro
Java 1.4.1
DCTAgent.class: DCTAgent.java
     ${JAVA_DIR}/javac DCTAgent.java
     ${JAVA_DIR}/javah -jni -d ./include DCTAgent
DCTAgentImp.dll: DCTAgentImp.c
     ${CC} -D__int64="long long" ${PCAP_INCLUDE} ${JAVA_INCLUDE} -mrtd -g -c DCTAgentImp.c
     ${CC} ${PCAP_INCLUDE} ${JAVA_INCLUDE} -shared -g -o DCTAgentImp.dll DCTAgentImp.o
----- Java code ------
public class DCTAgent {
public native void hello();
private void helloWorld() {
System.err.println("calling hello() native...");
hello();
System.err.println("... done!");
static {    
System.err.println("loading...");
System.loadLibrary("DCTAgentImp");
public static void main(String[] args) {       
new DCTAgent().helloWorld();
----- C file ------
#include <jni.h>
#include "include/DCTAgent.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_DCTAgent_hello (JNIEnv *env, jobject obj) {
printf("\nHello world!");
return;
----- C header ------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class DCTAgent */
#ifndef IncludedDCTAgent
#define IncludedDCTAgent
#ifdef __cplusplus
extern "C" {
#endif
* Class: DCTAgent
* Method: hello
* Signature: ()V
JNIEXPORT void JNICALL Java_DCTAgent_hello
(JNIEnv *, jobject);
#ifdef __cplusplus
#endif
#endif
----- output ------
$ c:/~path.to.java~/java -Djava.library.path=C:\mypath DCTAgent
loading...
$

Your problem sounds a lot like I was having until about an hour ago when I found this:
http://www.cygwin.com/ml/cygwin/2004-04/msg00099.html
The short answer (originally posted by Igor Pechtchanski) is to set the flag
-mno-cygwin
when you are linking your DLL.
To do this you need to install the packages "gcc-mingw" and "mingw-runtime" in your cygwin installation.
The reason for the problem is apparently that cygwin1.dll cannot be dynamically loaded, and programs will hang if/when they try to dynamically load it. If your DLL does not require the POSIX functions found in cygwin1.dll, you can force the equivalent of a mingw-compiled DLL with that flag, and cygwin1.dll will not be dynamically loaded.
It works like a charm for me, and has solved my problems with calling C++ code from JNI, as well as calling C++ code from Matlab (through an intermediary C DLL, although I think there are ways to eliminate that as well). Hope that helps.

Similar Messages

  • Executing DLLs from Java using JNI vs Runtime.exec()

    I am trying to understand the best way to execute a dll using java. The dll I am testing with takes a few input parameter. If I use JNI with System.loadLibrary("dll"); what do I need to do to pass the arguements in as well? Can I just add the arguements in the java code like private int xyz = "value"; and expect that the dll will accept them or is there special definitions I have to set up in the dll to make it work?
    Alternatively I was looking at using Runtime to execute the dll by using Runtime.exec("dll param1 param2",env,filePath); Does anyone know if there are drawback to doing it this way vs. using JNI or is this just as efficient? Any help would be appreciated.

    You seem to be confused...
    "execute a dll using java"
    Unless I'm mistaken, a dll is not executable. A dll is a library of code. This code has (hopefully) some well-defined entry points (declared in a header file somewhere) which you must call in a C/C++ file. The arguments you pass to the dll will come from java through JNI.
    As far as your understanding of this entire process, it is obviously confused beyond the scope of a simple reply. I recommend you read the examples and ALL of the documentation available here:
    http://java.sun.com/docs/books/tutorial/native1.1/index.html
    When you get the/an example running (or just set up to run) then post your code and comments here for more help.
    Ian

  • Remoting and localized output from executables

    Hello all, I am in need of a bit of assistance.
    We have client computers which have non-English Windows 7, and thus any executable that produce output produce non-English output. This is not a problem when using PowerShell locally, all localized output is displayed properly.
    Problems arise when using PowerShell remoting, where output does not display non-English characters correctly. Examples of native executables which produce incorrect output: ipconfig, ping, wingmmt.
    Host's output encoding:
    IsSingleByte      : True
    BodyName          : ibm850
    EncodingName      : Western European (DOS)
    HeaderName        : ibm850
    WebName           : ibm850
    WindowsCodePage   : 1252
    IsBrowserDisplay  : False
    IsBrowserSave     : False
    IsMailNewsDisplay : False
    IsMailNewsSave    : False
    EncoderFallback   : System.Text.InternalEncoderBestFitFallback
    DecoderFallback   : System.Text.InternalDecoderBestFitFallback
    IsReadOnly        : True
    CodePage          : 850
    Remoting output encoding:
    IsSingleByte      : True
    BodyName          : iso-8859-1
    EncodingName      : Western European (Windows)
    HeaderName        : Windows-1252
    WebName           : Windows-1252
    WindowsCodePage   : 1252
    IsBrowserDisplay  : True
    IsBrowserSave     : True
    IsMailNewsDisplay : True
    IsMailNewsSave    : True
    EncoderFallback   : System.Text.InternalEncoderBestFitFallback
    DecoderFallback   : System.Text.InternalDecoderBestFitFallback
    IsReadOnly        : True
    CodePage          : 1252
    Following command can be run to change console output encoding to UTF8:
    [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
    However, running this on remote session will result in "Exception setting "OutputEncoding": "The handle is invalid." Running this on host works, but has no effect on remote session.
    Apparently $OutputEncoding only works for input, not output. But I tried to set it to UTF8 anyway, with no effect.
    This problem does not affect PowerShell cmdlets, as Write-Host and Write-Output produce correct output even in remote session.
    Previously host had PowerShell 3 and client had PowerShell 2, now both have PowerShell 4 and problem still persists.
    Anyone know how I can receive correct output from localized native executables? Thanks in advance.

    Actually I am using PS remoting from server to client workstations.
    I have provided several examples of commands that produce incorrect characters in place of non-English characters in the first post. Likewise I have provided encoding info in the first post.
    As for culture info, info below.
    Get-Culture on host:
    LCID             Name             DisplayName
    1035             fi-FI            Finnish (Finland)
    Get-Culture in remote session:
    LCID             Name             DisplayName
    1035             fi-FI            Finnish (Finland)
    Get-Culture locally on client:
    LCID             Name             DisplayName
    1035             fi-FI            Finnish (Finland)

  • To read a DLL from java using JNI

    Hi,
    I had some DLL files (VB 5.0) which are providing interfaces. Is there any chance of accessing that using java (JNI). Also is there any De - Compiler available so that I can get the source code of the DLL file.
    Regards,
    Hari.

    Hola Hari
    To make a dll loadable through JNI, the interface
    inside
    the dll must be implemented the JNI way. Also a COM
    interface does not help.
    I would reccomend a certain product to automatise the
    JNI interfacing process, but that would
    require that you have the sourcecode of the dll
    available.
    But one thing you can do is to write a wrapper that
    contains the dll classes and is implemented the JNI
    way.
    More precisely you write an interface wrapper in c/c++
    that matches the methods of the dll and does use JNI
    methods. Now inside that c++ wrapper you load the dll
    and forward the calls from java to c++ vice versa.
    Download the demo version of CENTRO.java here:
    www.ablon.de
    Bye, nilsHi Nils,
    Thank you for your suggestion. Im new to JNI.
    That dll's are in Visual Basic. I don't have the source code of the dll file. I got the interfaces available in the dll file from a Visual Basic program which make use of the dll file(which we are talking about). As I know only the method signatures in the interfaces(dll file), I request you to kindly provide me some help to wrap it in C/C++ program.
    Thanks in advance.
    Regards,
    Hari.

  • Executing a java application from c++ using jni

    hi,
    how do i execute a java application from c++ ?. it should behave similar to typing 'java abc.class' at the dos prompt.
    i've done up till recognizing the method id. GetStaticMethodID(). I tried using CallStaticVoidMethod() but didnt work. are there any other methods i should be using?

    Look at the source code to the "java" command that is included in the sources that come with the JDK. Since it is the exact code that is run when you type java at the command line, it should be close to what you want.

  • Can I call a C function from Java (using JNI) ??

    hello,
    I need to call one C function from remote device and get its return value(o/p) (for my application it is device SSID ) and display in the client PC.
    Is it possible in java without using SNMP connction?.
    please give me idea about it.
    I need this information as soon as possible.
    Thank you.

    see JNI...
    basically declare a native method in your java class, then run javah on the class thus generating a *.h file. Then implement the c/c++ function. Compile the c/c++ part as a shared library, and make sure its accessible from your LD_LIBRARY_PATH env variable.
    Go through the steps in the JNI docs, and things shoulde be fine... hopefully ;)

  • Calling Java from C++  using JNI

    I have been looking into JNI, and I ran sample where the Java calls Native c/c++ methods. Now to do it the other way, having C/C++ to call Java method by passing argument and getting the results back, I think I need to follow the example in Chapter 5 sun, which is creating JVM in the native code, accessing the Java class and method, and then finally calling the Java method.
    First I like to know if I'm right above, and second if I need to pass a C++ class as the argument to the Java method and then the result from the method is Java class, what would be the best way to do? I know I need some to convert the class type some how.
    Thanks,
    Eli

    Are you also wanting to launch Java from C++? Unzip JAVA_HOME/src.zip and look at the source code for java.exe
    If you have native called by a Java object then you can call any non native method in the Java object from the C/C++ side. Example Java method, reportCurrentStagePosition :
        public void reportCurrentStagePosition (int fault, double x, double y, double z, double &#966;)
            if (fault == 0)
                reportCurrentStagePosition (x, y, z, &#966;);
            else
                faultMessage = "Stage Positioning Fault: X="+eFormat(x)+", Y="+eFormat(y)+", Z="+eFormat(z)+", phi="+eFormat(&#966;);
                System.out.println ("***Aero Fault Message: "+faultMessage);
        public native long moveLinearXYZPhiAxes() throws PositionException;Then from the native side of moveLinearXYZPhiAxes the obj is the calling object and has the reportCurrentStagePosition method
    JNIEXPORT jlong JNICALL Java_com_hypernex_goniometer_AerotechWrapperN_moveLinearXYZPhiAxes (JNIEnv *env, jobject obj)
         jmethodID jReportPositions = env->GetMethodID(env->GetObjectClass(obj), "reportCurrentStagePosition", "(IDDDD)V");
         if (jReportPositions == NULL)
              printf ("***ERROR*** about to call jReportPositions but it is NULL ***\n");
         else
             env->CallVoidMethod (obj, jReportPositions, 0, xFeedBack, yFeedBack, zFeedBack, phiFeedBack);
    }Since reportCurrentStagePosition returns a void you use the CallVoidMethod of env. The "(IDDDD)V" is the signature of the method. If it is there GetMethodID returns a non null.

  • Calling a third Party dll from java using JNI

    Hi
    I want an immediate help from u all.
    I have athird party c dll with .h and .lib file
    I wont be able to cahnge any thing in those files.
    I want to call the functions of that from my Java code.
    Ist possible to call the dll without writing any c or c++ wrapper over it?if yes , how to do it?

    Hi,
    You may use a generic wrapper like JNative.
    Commercial wappers also exists.
    --Marc (http://jnative.sf.net)                                                                                                                                                                                                                               

  • Remove comma in query output from Char used as KF

    Hello Friends,
    To be able to display a characteristic in a particular way I used formula variable and displayed it as Key Figure in the query output. By doing this I am getting the output in the required format, but only problem is that the value is being displayed with commas. As it is an order number, I want to display it without commas.
    I don't want to use VB as the query is to be published in web also.
    Please suggest if there is any way to achieve this.
    Thanks in advance.
    Praveen

    Hey Praveen, there's a few ways to skin this cat. The simplest method is to make sure that the user parameters in SU3 don't have commas for numbers. The problem is that this is a global setting so you won't get commas for other numbers in which you may want commas. Also, you'd have to make sure your replacement variable was defined as type "number". The other options include using VBA in BEx, and/or Table Interface in the Web. If you are using BW 3.5, you can use XSLT to render a table with and not have commas for that value by using the XML Query Result Set Item. Hope that helps,
    Prakash

  • How to list the installed softwares from registry using JNI

    Hi everybody,
    I'm new to JNI.
    For example assume Acrobat reader and firefox are the softwares installed in my system
    i want to read all the exe files which are below this location.
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
    I found solution for how to read the following files Example: (AcroRd32.exe and firefox.exe, etc);
    which are located in the below path in registry.
    by specify the location
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe
    by using the RegOpenKeyEx() function.
    But now i need to list all the exe files which are under
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
    Example take Acrobat reader and firefox are the softwares installed means it has to list
    AcroRd32.exe
    firefox.exe
    which is under the key
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
    Through which method i can get all the files.
    Provide some sample code for the function too.
    kindly reply for this asap.
    Also provide the links which is useful for this question.
    Regards
    Pugazharasu Chandrakesan

    I found the answer byself.

  • JNI: Getting String | StringBuffer  to Java from C using input argument

    I've written a native method in C which takes command String from Java, executes the command, and obtains both an integer return_code and a C-string result_string as an output. The return_code goes back to Java from the return statement at the end of the native method...my problem is, that I would like to pass into the native method either a String or StringBuffer, and use that to relay the result_string back to the Java code, as well. Eg., loosely, if I have a
    JNIEXPORT jint JNICALL ExecuteCmd
    (JNIEnv *env, jclass jcls, jstring cmdString, jobject returnBuffer)
    where jobject returnBuffer is a StringBuffer created as StringBuffer(2048), I'd like to take return_string once I have it and do something like
    returnBuffer = env->NewStringUTF(result_string);
    (I know the syntax env->NewStringUTF is a bit different to what's in the JNI book, but it's what works on the HP-UX system I'm using.)
    I know already that this particular bit of code doesn't work. My question is, does the general idea have any potential to work, or am I trying to do something that's impossible?

    Your general approach works just fine. What you have to do is use JNI to find the appropriate method for the StringBuffer class, and invoke it.

  • Input and Output flow from executable to NI Teststand

    Hi,
    I have a *.exe file generated from Labview and which has inpu 'x' and 'y', output 'z'. I wanted to pass these inpyts 'x' and 'y' to teststand and get the output from 'z' when the executable is called.
    Please provide me the solution for this.
    Many Thanks
    Haneef

    Hello Ray Farmer,
    I have seen the examples, where in I found how to pass the inputs to the TestStand from the executables.
    But my requirement is, I have a executable with one input button say 'OK'. I will use this executable in NI TestStand, the TestStand should not execute farther to next step until and unless 'OK' button has been made to invoke from the executable.
    The intension here is to control the flow of execution using executable.
    I guess you are clear with my doubt. Please try to help me as I am learner to this kind of scenarios.
    Many Thanks
    Haneef

  • ORA_FFI problem in using JNI from C dll

    Hi,
    I am encountering a strange problem in ORA_FFI while calling a C dll.
    The C program uses JNI to dynamically load a jvm (using LoadLibrary('C:\\jdev1012\\jdk\\jre\\bin\\server\\jvm.dll'))and then call an
    instance object. The problem is when invoked at runtime it executes
    fine the first time but from the same session if its invoked again the
    JVM fails to initialize. It seems the JVM is not getting released though
    I am using DestroyJavaVM.
    I am using forms10g and could have used ORA_JAVA but i have some
    other C projects with which i am trying to integrate into one dll.
    I am really stuck and any experience on this will really be helpful.
    -Thanks,
    Samrat

    I suspect the "-d64" switch is interpreted by the java launcher to make it load a 64-bit JVM library rather than one of the the usual 32-bit JVM libraries. If so, you would need to load the same 64-bit JVM in your own program. Try looking in SDK source files java.c and java_md.c to get started.
    (I don't have HP-UX or any 64-bit system here, so this is just a best guess.)
    -slj-

  • Using output from pwd in current directory

    Hi,
    I'm new to EEM in IOS-XR or EEM in general. 
    I'm looking to use the output from the CLI in the current vty session and use it as a argument to a TCL-script.
    Example: 
    RP/0/RSP0/CPU0:Router(config)#router isis  1
    RP/0/RSP0/CPU0:Router(config-isis)#pwd
    Mon Mar 17 10:38:01.827 CET
    router isis 1
    I was thinking a alias that triggers a tcl-script and then executes a command in the same working directory as the user who executed the alias is in or some other way and can save the information from the current session. 
    Is this is possible?

    Unfortunately, this is not possible.  The config submode is not passed as a variable to the CLI event detector.

  • How can i call a VB6 project from my java application using JNI

    hi
    can anyone tell me the procedure of calling a VB6 project from any java application using JNI
    if anyone does know then tell me the detail procedure of doing that. I know that i have to create a dll of that VB6 project then to call it from the java application.
    if anyone know that procedure of creating dll file of an existing VB6 project please reply
    please if anyone know then let me know

    Ahh, kind of a duplicate thread:
    http://forums.java.sun.com/thread.jspa?threadID=631642
    @OP. You could have clarified your original post and the relationship of your question to java. You did not need a new thread.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Maybe you are looking for

  • How to make next and previous button function

    Hello Can anybody help me with how to achieve next and previous button function such as the one at www.bestbuy.com? I would appreciate any help or suggestion. Thank you!

  • Error combining Power Point 2007 files into a single pdf (Acrobat 11)

    Hi,  Am running Acrobat 11 and am getting an error when attempting to combine files.  Trying to combine multiple power point 2007 files into a single PDF using Acrobat 11.  Am getting an error "unable to find Adobe PDF resource files.  Do you want to

  • Wake for network access while on battery?

    I want to wake for network access when my mac is not plugged in to a power source. Is there a command I can run in terminal to do this? Its not like it consumes that much battery power to do this. Heres a simular thread that was never answered. https

  • Attachments given in the portal to be stored in R'3

    Hi, We have a requirement, where the user will be attaching an attachment of  any type ( like JPEG, PDF, DOC, XLS etc) and that needs to be stored / handled in R/3. And it should be shown later. Is anyone have any idea how to handle this? I came to k

  • How to make a button appear boxed in when clicked

    For my Prototyping class I have to make an interactive fast food ordering system. I have majority of frames made I just need to add the interactive part to it. I am using AS2 in Flash CS3. I have the picture and the text grouped and are classified as