Integerating objective c with java through JNI in MAC

Hi,
I hv some code in objective c and i hv to call some methods from java code. So i want to use JNI interface for that.
Could any body give me refrence on how to go about it. I know little bit about integerating c code and java thorugh jni on windows.
Thanks in advace.
Regards,
Prashant

you need to create dll using vc++ or Borland compiler
i will try to help you out.
//mac.java
class mac
    public native void getMac();
    private native String getLine(String prompt);
    static {
     System.loadLibrary("myMac");//This is myMac.DLL
     /*if generated by borland
     System.loadLibrary("mymac");//This is mymac.dll
     public static void main(String[] args)
    mac mc=new mac();
    String input = mc.getLine("Enter Some Thing ");
    System.out.println("You Entered " + input);
    System.out.println(mc.getMac());
}prompt> javac mac.java
prompt> javah -jni mac
Borland compiler
/*mymac.c*/
#include <jni.h>
#include "mac.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_mac_getMac(JNIEnv *env, jobject obj)
    printf("I am in  getmac dir in mymac.dll (Borland) \n");
    return;
JNIEXPORT jstring JNICALL
Java_mac_getLine(JNIEnv *env, jobject obj, jstring enter)
    char buf[128];
    const char *str = (*env)->GetStringUTFChars(env, enter, 0);
    printf("%s", str);
    (*env)->ReleaseStringUTFChars(env, enter, str);
    scanf("%s", buf);
    return (*env)->NewStringUTF(env, buf);
}To generate DLL
create bcc32.cfg file in %BORLAND_INSTALL%/BCC5/bin Folder
In the bcc32.cfg file add following code
-I"%BORLAND_INSTALL%\BCC55\include;%JAVA_HOME%\include;%JAVA_HOME%\include\win32"
-L"%BORLAND_INSTALL%\BCC55\lib;%BORLAND_INSTALL%\BCC55\Lib\PSDK;"
i.e the path for borland compiler and java.
now goto prompt and say
prompt>bcc32 -tWM -tWD mymac.c
This will create mymac.lib File
Now say
prompt>implib -c mymac.lib mymac.dll
That's it!!
there you are with dll
Now you can run the mac.class file by using java
VC++
Click File->New->Win32Dynamic-Link Library
Give name and Select
A simple DLL project
You will have
myMac.CPP file
// myMac.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "mac.h"
#include "jni.h" //can copy or give full path
#include <math.h>
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
    return TRUE;
JNIEXPORT void JNICALL
Java_mac_getMac(JNIEnv *env, jobject obj)
    printf("You are in getMac (VC++) DLL\n");
JNIEXPORT jstring JNICALL
Java_mac_getLine(JNIEnv *env, jobject obj, jstring enter)
    char buf[128];
    const char *str = env->GetStringUTFChars(enter, 0);
    printf("\n%s", str);
    env->ReleaseStringUTFChars(enter, str);
    scanf("%s", buf);
    return env->NewStringUTF(buf);
}Hope this is useful
regards
kanad Deshpande

Similar Messages

  • Known problems or limits regarding accessing BDB with Java via JNI

    Hi,
    we are currently in an evalutation phase and are planning to use BDB (C|JE) as back-end for a queueing system. This system is written in Java. One key requirement is high availability of the data hold in the back-end. We need the guarantee, that no data is lost, e.g. data added after checkpoints. The throughput is expected to be quite high (> 10.000.000 records hold within one single BDB-environment (we would use n parallel BDB-envs inside the service).
    So i'm wondering about using JE or DB... The former doesn't provide a replication system but is a 100%-pure Java implementation, the latter is written in C and accessed via JNI, which adds some overhead and maybe instability issues but comes with an integrated replication system. JE would require us to develop an in-application replication system or to use a DFS for HA or something like that.
    Are there any known issues regarding using DB with Java via JNI? Or any other hints?
    TIA,
    Dirk

    Silly me, I forgot to mention that a null return value means that an exception was thrown so you should get that and print the value (although I suspect it will be "class not found").

  • Looking for answer keys for "Object First with Java"

    Where can I find a book with anwser keys for the book " Object First with Java" Third Edition?

    zxcvbnm wrote:
    Where can I find a book with anwser keys for the book " Object First with Java" Third Edition?This ought to be an interesting thread. :)

  • Manipulating C++ Objects in Java Through JNI

    Hi,
    I have some existing C++ classes objects that I would like to access through Java methods. However, if I use a java method to create an instance of a C++ object, and then I want to edit the state of that C++ object through java, how do I reference the correct C++ object.
    For example, if I have a C++ object called Ball, with two variables x and y representing its position and a constructor as well as set methods to set x and y. I then make a java class Ball with the same variables, constructor, and setter methods. But when the java constructor is called, I want the C++ version to be created, and when the java setter methods are called, I want the x and y of the correct C++ Ball instance to change.
    Is this is possible, and if so what are the steps needed?
    Thank you

    See example with C++ structure in
    http://www.sharewareplaza.com/Java-Platform-Invoke-API-Demo-version-download_49212.html

  • Transient java object instance from jvm through jni?

    Hello, All
    Sometimes I want manage the memory take by object instance,
    for example, If I want to design a cache which cache a huge number object.
    Then, there may two problems:
    1, May occur OutOfMemory error.
    2, I want like to manually reclaim the memory take by the cached object,
    not by gc, because it may take a little time to check the reference for gc.
    Just as you know, there is keyword transient for serialization.
    I want know if some same mechanism for object instance creating or release, managing memory by jni, or by some other way?

    No, since you're not supposed to do that. hence you can't really expect Java to provide the means...
    Maybe you can use classes like WeakReference etc. but that won't bypass GC.

  • Communicate with java through javascript [accessControlException]

    all right I have an applet with this method in it:
    public String getText() throws UnsupportedFlavorException, IOException {
    Clipboard clipboard = getToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(this);
    return contents.getTransferData(DataFlavor.stringFlavor);
    }in order to access the clipboard, the applet must be signed and it is.
    Everything works fine from Java's side, but when I try accessing the method from javascript, i get this error:
    SyntaxError: java.security.AccessControlException: access denied (java.awt.AWTPermission accessClipboard)why is that?
    here is my javascript code:
         var applet = document.getElementsByTagName('APPLET')[0];
         try {
               var textFromClipboard = applet.getText();
                     alert(textFromClipboard);
         } catch (err) {
                      alert(err); // java.security error everytime...
         }

    yes, I've looked into that but my method throws Exception and I can't capture them from outside that new inner class:
    AccessController.doPrivileged(new PrivilegedAction() {
                public Object run() THROWS EXCEPTION {
                            Clipboard clipboard = getToolkit().getSystemClipboard();
                            Transferable contents = clipboard.getContents(this);
                            return contents.getTransferData(DataFlavor.stringFlavor);
    });^ won't work

  • CF 9.0.1 with Java 1.6 on Mac OS X Won't Start

    Any suggestions? It works perfectly with a plain-jane install of 9,0,0,251028.. but if I update to 9.0.1, here's what I get:
    [Local (virtualtmo)]:Starting Macromedia JRun 4.0 (Build 108858), cfusion server
    [Local (virtualtmo)]:03/15 19:09:24 info JRun Naming Service listening on *:2904
    [Local (virtualtmo)]:03/15 19:09:24 info No JDBC data sources have been configured for this server (see jrun-resources.xml)
    [Local (virtualtmo)]:03/15 19:09:24 info JRun Web Server listening on *:8300
    [Local (virtualtmo)]:03/15 19:09:24 info JRun Proxy Server listening on *:51020
    [Local (virtualtmo)]:03/15 19:09:24 info Deploying enterprise application "cfusion-ear" from: file:/Applications/JRun4/servers/cfusion/cfusion-ear/
    [Local (virtualtmo)]:03/15 19:09:25 info Deploying web application "Adobe ColdFusion 9" from: file:/Applications/JRun4/servers/cfusion/cfusion-ear/
    [Local (virtualtmo)]:03/15 19:09:25 INFO License Service: Flex 1.5 CF Edition enabled
    [Local (virtualtmo)]:03/15 19:09:25 INFO Starting Flex 1.5 CF Edition
    [Local (virtualtmo)]:03/15 19:09:25 user JSPServlet: init
    [Local (virtualtmo)]:03/15 19:09:26 user ColdFusionStartUpServlet: init
    [Local (virtualtmo)]:03/15 19:09:26 user ColdFusionStartUpServlet: ColdFusion: Starting application services
    [Local (virtualtmo)]:03/15 19:09:26 user ColdFusionStartUpServlet: ColdFusion: VM version = 19.1-b02-329
    [Local (virtualtmo)]:03/15 19:09:26 Information [scheduler-11] - Starting logging...
    [Local (virtualtmo)]:03/15 19:09:26 Information [scheduler-11] - Starting license...
    [Local (virtualtmo)]:03/15 19:09:26 Information [scheduler-11] - Invalid ColdFusion 9 license.
    [Local (virtualtmo)]:03/15 19:09:26 Information [scheduler-11] - Invalid ColdFusion 9 license.
    [Local (virtualtmo)]:03/15 19:09:26 Information [scheduler-11] - Developer Edition enabled
    [Local (virtualtmo)]:03/15 19:09:26 Information [scheduler-11] - Starting crypto...
    [Local (virtualtmo)]:03/15 19:09:27 Information [scheduler-11] - Installed JSafe JCE provider: Version 3.6 RSA Security Inc. Crypto-J JCE Security Provider (implements RSA, DSA, Diffie-Hellman, AES, DES, Triple DES, DESX, RC2, RC4, RC5, PBE, MD2, MD5, RIPEMD160, SHA1, SHA224, SHA256, SHA384, SHA512, HMAC-MD5, HMAC-RIPEMD160, HMAC-SHA1, HMAC-SHA224, HMAC-SHA256, HMAC-SHA384, HMAC-SHA512)
    [Local (virtualtmo)]:03/15 19:09:27 Information [scheduler-11] - Starting security...
    [Local (virtualtmo)]:03/15 19:09:27 Information [scheduler-11] - Starting scheduler...
    [Local (virtualtmo)]:03/15 19:09:27 Information [scheduler-11] - Starting WatchService...
    [Local (virtualtmo)]:03/15 19:09:27 Information [scheduler-11] - Starting debugging...
    [Local (virtualtmo)]:03/15 19:09:27 Information [scheduler-11] - Starting sql...
    [Local (virtualtmo)]:03/15 19:09:27 Information [scheduler-11] - Pool Manager Started
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - Starting mail...
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - Starting runtime...
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - CORBA Configuration not enabled
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - Starting cron...
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - Starting registry...
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - Starting client...
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - The metrics service is disabled for the J2EE edition
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - Starting xmlrpc...
    [Local (virtualtmo)]:03/15 19:09:28 Information [scheduler-11] - Starting graphing...
    [Local (virtualtmo)]:03/15 19:09:29 Information [scheduler-11] - Starting verity...
    [Local (virtualtmo)]:03/15 19:09:29 Information [scheduler-11] - Starting solr...
    [Local (virtualtmo)]:03/15 19:09:29 Information [scheduler-11] - Starting archive...
    [Local (virtualtmo)]:03/15 19:09:29 Information [scheduler-11] - Starting document...
    [Local (virtualtmo)]:Error(03/15 at 07:12:36) Unable to start server. Server startup timed out.
    [Local (virtualtmo)]:Error(03/15 at 09:31:44) Operation is canceled.
    [Local (virtualtmo)]:Error(03/15 at 10:56:35) Error retrieving server version. Cannot perform the operation. Check if the server is accessible from web browser.

    What spec is your mac os x? as we had an issue when updating our version and for some reason when we updated to os 10.6 (Currently 10.6.3) the update went fine..

  • How to access data structures in C dll from java thru JNI?

    We have been given API's( collection of C Functions) from some vendor.
    SDK from vendor consist of:
    Libpga.DLL, Libpga.h,Libpga.lib, Along with that sample program Receiver.h (i don't know its written in C or C++), I guess .C stnads for C files?
    Considering that I don't know C or C++ (Except that I can understand what that program is doing) & i have experience in VB6 and Java, In order to build interface based on this API, I have two option left, Use these dll either from VB or Java.
    As far as I know, calling this DLL in VB requires all the data structures & methods to be declared in VB, I guess which is not the case with Java (? I'm not sure)
    I experiemnted calling these function from Java through JNI, and I successfully did by writting wrapper dll. My question is whether I have to declare all the constants & data structures defined in libpga.h file in java, in order to use them in my java program??
    Any suggesstion would be greatly appreciated,
    Vini

    1. There are generators around that claim to generate suitable wrappers, given some dll input. I suggest you search google. Try JACE, jni, wrapper, generator, .... Also, serach back through this forum, where there have been suggestions made.
    2. In general, you will need to supply wrappers, and if you want to use data from the "C side" in java, then you will need java objects that hold the data.

  • Passing Image data through JNI?

    Hello,
    I am trying find the fastest way to pass image data from Java through JNI. Passing Java images through JNI seems to be a very common thing to do so I expect there's an oft-used approach. Can anyone give me a good explanation of this or point me to some references?
    I need to pass the image data to a C++ function which operates on this data, writing the results to a destination buffer which goes back to Java and gets put back into an image.
    Currently, I'm using BufferedImages and getRGB to get an int array from my source image, passing this through JNI along with another int array to be used as the destination buffer, then calling setRGB with my destination buffer after the JNI function returns. getRGB() preserves the alpha channel of png files, which is functionality I need. The problem with this approach is that getRGB is extremely slow. It can take up to two seconds with a large image on my 1.2 Ghz Athlon. It seems to be performing a copy of the entire source image's data. Maybe there is a way to decode a jpg or png directly into an int array?
    -Aaron Dwyer

    Here's how you could do it for TYPE_4BYTE_ABGR
    BufferedImage img=new BufferedImage(13,10,BufferedImage.TYPE_4BYTE_ABGR);
    img.setRGB(0,0,0xff0000);     
    img.setRGB(5,1,0x00ff00);        
    img.setRGB(1,1,0xcafebabe);
    DataBuffer db=img.getRaster().getDataBuffer();
    int dataType=db.getDataType();
    if (dataType!=DataBuffer.TYPE_BYTE)
         throw new IllegalStateException("I can do it only for TYPE_BYTE");
    int imgType=img.getType();
    if (imgType!=BufferedImage.TYPE_4BYTE_ABGR)
         throw new IllegalStateException("I can do it only for TYPE_4BYTE_ABGR");
    DataBufferByte dbi=(DataBufferByte)db;
    byte[] array=dbi.getData();
    SampleModel model=img.getSampleModel();
    if (!(model instanceof PixelInterleavedSampleModel))
         throw new IllegalStateException("I can do it only for PixelInterleavedSampleModel");
    PixelInterleavedSampleModel pisModel=(PixelInterleavedSampleModel)model;
    if (pisModel.getPixelStride()!=4)
         throw new IllegalStateException("I can do it only for pixel stride of 4");
    if (dbi.getNumBanks()!=1)
         throw new IllegalStateException("I can do it only for 1 band");
    int scanlineBytes=pisModel.getScanlineStride();
    // Access the green Pixel on Position 5,1
    System.out.println( (int)array[5*4 + scanlineBytes] &0xff);     // Alpha        
    System.out.println( (int)array[5*4+1 + scanlineBytes] &0xff);     // Red        
    System.out.println( (int)array[5*4+2 + scanlineBytes] &0xff);     // Green        
    System.out.println( (int)array[5*4+3 + scanlineBytes] &0xff);     // Blue     I've added some checks to make sure we're accessing the data the right way.

  • 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

  • How to share a folder with java? in netbios in windows

    hi. I want to write a programa that user can select a directory than
    click the share button and the directory will be shared to other pcs.
    the other pcs can acces this directory like \\servername\shareddirectory
    the question is this. how can i share a director with java or JNI's?
    please help me.
    with my best regards....

    thanks about net share command.
    i know how to use Runtime.getRuntime.exec()
    but i cont find the syntax of net share command.
    can u give an example? i use net help share and i try different
    sytaxes but i cant find the correct one.
    please give an examle about net share command.
    thanks a lot.

  • Access InfoObject with Java

    Hello to all access-bw-with-java-professionals,
    how is it possible to access InfoObjects with the Java BI SDK (not with JCo, Java and ABAP)? Can you also access ODS-Objects directly with Java?
    We have a SAP WebAS Java 6.40 with the java application running on it and a SAP BW 3.1 which we want to access.
    Thanks for all tips&hints,
    Volker
    ps: The reason why we need to access these objects is because we have to write our own persistence-layer, because we cannot use something like Hibernate. At least we do not know that something similar exists...

    I want to know what PAM is
    because I want to answer the questionI think it stands for something like "Pluggable
    Authentication Module" or something like that.
    It's a modular authentication system.
    That's from memory. For real answers, try using
    Google.
    A java interface to PAM would be interesting.Technically JAAS is an implementation of a PAM.
    http://java.sun.com/products/jaas/index-10.html
    http://java.sun.com/security/jaas/doc/pam.html
    That will not help the poster in this case though, since he seems to be interested in one specific implementation of a PAM, running on linux, for which I have found no java implementation.

  • Set Java Object with Java Long Object from JNI

    Hi,
    i am getting crazy with the jni...
    in Java i have the follwoing:
    public class blabla{
    Object myObject;
    i chose Object cause the return value from native is not known
    in case for a long value i handle it like this :
    //get the Long class
    jclass LongCls = env->FindClass("Ljava/lang/Long;");
    jmethodID jmid = env->GetMethodID(LongCls, "<init>","(J)V");
    //create an Long Object with the long return value
    jobject LongObj = env->NewObject(LongCls,jmid,longreturnvalue);
    //set Long Objekt - doesnt work!(jclazz is the class of the return Object of the blabla class)
    jfid = env->GetFieldID(jclazz, "myObject", "Ljava/lang/Object;");
    env->SetObjectField( returnobjectofblabla,jfid,LongObj );
    the long value is set fine in the Long Object but i cant set the Long Object with the SetObjectField method into the Object myObject.
    Java is shown that the myObject contains a Long Object but the long value of the Long Object is totally wrong and doesnt equal to the long value which has been set.But setting the value works fine- any ideas or solutions?
    Thank you!

    charliess wrote:
    sometimes i really wonder about the answers in this forum - until now i got not one answer that helps me, even i tried to write down the problem very detailed...
    Sometimes I wonder about people who jump onto a site and immediately assume that
    1. They are the most important person in the world
    2. That they deserved the absolute undivided attention of absolute everyone.
    3. That everyone should be absolutely polite to them and provide them with perfect answers.
    4. Feel free to denigrate and posit their own opinion on everything while ignoring anything else that anyone else says.
    5. Expect that everyone else should understand their poorly worded questions.
    Myself I find such individuals extremely amusing. Although they seldom stick around long here. At least not under their original alias.
    plaz tel me how to realise your error checking?example plz
    You start by having at least a basic understanding of C/C++ and java.
    Then you read the JNI documents, all of them, for each method that you are using.
    Then you write code such that it actually reflects the documents. For example you check return types. And probably deal with the possibility that JNI methods might throw exceptions.
    as far as i know the jni methods like getfieldids and so on throw exceptions other like stObject dont do that the just return nothing
    i can see all exceptions in my Java console thats not the Problem, setting the Long Object does not throwing a exception btw->
    Myself I wouldn't expect that a JNI exception would show in the java console. Could I suppose though.
    However nothing so far would suggest that an exception is being thrown. That however doesn't alter the fact that you must still write the code such that it could happen.
    is it possible in general to set an java Long Object created in C(native) into a Object type (Object) overgiven as parameter in my JNI function with the setObject method?
    Is it possible that you could actually follow my previous request and create some code that actually runs instead of cut and pasting whatever you feel like?
    plz answer also with code and examples i did the same for you...No you didn't. You posted something which would not compile and quite possibly doesn't represent your actual code at all.
    And please note that we do not get paid to do this. If you want to pay someone to solve your problem then there are sites for that.
    Paying someone allows you to make all sorts of demands. The more you pay the more people will put up with.
    If you don't want to pay and do in fact want free answers then it will probably be in your interest to
    1. Be polite
    2. Answer the questions that have been asked of you.

  • Pass an array of integers from C to Java with JNI

    Hello,
    I have a C function that returns a struct me. The fields of this struct are all of type integer. I, with the fields of this structure create an array of integers in C language I would like to pass this array of integers c, in Java using JNI.
    How can I make this?

    I don't see how you compiled that.
    It certainly will not compile for me.
    jintArray position=(jintArray)(*env)->NewIntArray(env,2);That is the invocation idiom for C code.
    jint f[2];You cannot have a variable declaration in the middle of a method (block) in C code. That is only allowed in C++.
    C code and C++ code is not the same.
    The following is C++ code. Note that it does NOT include getBinaryPoint() since that method could be the source of the problem.
    jintArray position = env->NewIntArray(2);
    if(position==NULL)     return NULL;
    jint f[2];
    f[0]=3;
    f[1]=4;
    env->SetIntArrayRegion(position,0,2,f);
    return position;

  • Problem with Java Memory "Could not reserve enough space for object heap"

    Hi gurus,
    I am not an expert with Java´s configuration, and I have a situation that I don´t understand. First of all, I am working at Centos 6.2 with jdk_1.6 and Tomcat 7.
    The problem is...
    - If I run Tomcat with JAVA_OPTS="-Xmx128m"* (at catalina.sh) all works fine.
    - If I run Tomcat with JAVA_OPTS="-Xmx512m"* (at catalina.sh) an error appears:
    Error occurred during initialization of VM
    Could not reserve enough space for object heap
    Could not create the Java virtual machine.
    This appear when I run java -version or when I try to stop Tomcat, and the Tomcat isn´t able to stop.
    The strange thing is that my server has more than 200M free in physical memory. So, why Tomcat isn´t able to stop? and Why Java doesn´t use the free memory in my server?
    Thanks in advanced.

    Hello EJP, thanks for your answer.
    I have explained bad.
    The server has 703M free when Tomcat is stopped. I had mentioned that my server has more than 200M free in physical memory when Tomcat is running with JAVA_OPTS="-Xmx512m", so I don´t understand why these errors appear.
    Do you understand me?
    Recently I have checked the swap memory, and it is disabled. In spite of swap memory is disabled I think java wouldn´t need this memory because it has free physical memory
    Thanks again.

Maybe you are looking for