JNI/Linux/chown

Discussion in 'Java' started by GfxGuy, May 28, 2005.

  1. GfxGuy

    GfxGuy Guest

    I've been getting the dreaded and oft discussed
    "java.lang.UnsatisfiedLinkError" exception...

    ---------
    The Java:
    ---------

    public class MyJNI
    {
    static
    {
    System.loadLibrary("MyJNI");
    }

    public native boolean chown(String filename, int user);
    }

    --------
    The C++:
    --------

    #include "MyJNI.h"
    #include <sys/types.h>
    #include <unistd.h>

    JNIEXPORT jboolean JNICALL Java_MyJNI_chown (JNIEnv *env, jobject jobj,
    jstring fname, jint fowner)
    {
    jboolean rval = JNI_FALSE;
    jboolean iscopy;
    const char *name;
    int owner = fowner;

    name = env->GetStringUTFChars(fname, &iscopy);

    if(chown(name,owner,owner) == 0) rval = JNI_TRUE;

    env->ReleaseStringChars(fname, (jchar *) name);

    return rval;
    }

    -----------
    The problem
    -----------

    The System.loadlibrary function works just fine - libMyJNI.so is indeed
    there and in the ld_library_path (I know this because I had to work
    through all of that, too). It says it can't find "chown", the specific
    function:

    java.lang.UnsatisfiedLinkError: chown
    at com.turner.utilities.MyJNI.chown(Native Method)
    at com.turner.utilities.MyUtils.chown(Unknown Source)
    ..
    ..
    ..

    MyUtils.chown is just another wrapper on top so that other higher level
    classes don't have to care about the JNI aspect of it.

    I haven't programmed in C/C++ in a long time... mostly written in Java
    and other scripting languages for the past few years, I just needed a
    way to "chown" files. My HelloWorld test worked just fine with the
    same compiler and linker options:

    INC= -I${JAVA_HOME}/include \
    -I${JAVA_HOME}/include/linux

    gcc -c ${INC} -fPIC -g MyJNI.c++
    gcc -shared -W1 -lstdc++ -o libMyJNI.so MyJNI.o

    TIA for any advice on either this specific problem, or if you could
    point me to something that exists to perform chown, that'd be cool...
     
    GfxGuy, May 28, 2005
    #1
    1. Advertising

  2. GfxGuy

    Lucy Guest

    > .> TIA for any advice on either this specific problem, or if you could
    > point me to something that exists to perform chown, that'd be cool...
    >


    1. maybe you need to give it the full path to the file not just the name of
    the file

    2. or you could just run it as a command
     
    Lucy, May 28, 2005
    #2
    1. Advertising

  3. On 27 May 2005 19:17:29 -0700, GfxGuy wrote:
    > I've been getting the dreaded and oft discussed
    > "java.lang.UnsatisfiedLinkError" exception...


    > java.lang.UnsatisfiedLinkError: chown
    > at com.turner.utilities.MyJNI.chown(Native Method)


    You've got a package declaration in your Java class, but you didn't
    generate the native hewader using the same fully qualified classname.
    You must include the package name when you run javah or it won't
    generate the correct native symbol names.

    BTW there's nothing remotely "++" about the C method you've posted. I
    don't see why you've named it .c++ and link with libstdc++.

    Also, add -D_REENTRANT to your CFLAGS.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 28, 2005
    #3
  4. GfxGuy

    Ross Bamford Guest

    On Fri, 2005-05-27 at 19:17 -0700, GfxGuy wrote:
    > I've been getting the dreaded and oft discussed
    > "java.lang.UnsatisfiedLinkError" exception...
    >
    > [snip voodoo]
    >
    > TIA for any advice on either this specific problem, or if you could
    > point me to something that exists to perform chown, that'd be cool...
    >


    Perhaps your purpose is to play with JNI? In that case, ignore this.
    Otherwise:

    public class Cot {
    private static final String CHOWN_COMMAND = "chown you:group file";
    public static void main(String[] args) {
    try {
    Process chown = Runtime.getRuntime().exec(CHOWN_COMMAND);
    chown.waitFor();
    System.out.println("Success: "+(chown.exitValue() == 0));
    } catch (Exception e) {
    System.out.println("Exception: "+e);
    }
    }
    }

    Saves a lot of messing about...

    --
    [Ross A. Bamford] [ross AT the.website.domain]
    Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
    http://www.roscopec0.f9.co.uk/ + in
     
    Ross Bamford, May 28, 2005
    #4
  5. GfxGuy

    GfxGuy Guest

    Actually, there is... I know the file operations are C, but the access
    to "env" is handled a differently. I know it's only a minor detail,
    but I like it that way better.

    When I tested my JNI HelloWorld, it would not work (again, got the
    UnsatisfiedLinkError) when I did not use libstdc++, a problem (and
    solution) that I found on this newsgroup.
     
    GfxGuy, May 28, 2005
    #5
  6. GfxGuy

    GfxGuy Guest

    Yes and no... if I want to make it portable, the idea is that only a
    small amount of native code would have to be rewritten and it would
    integrate better with each OS. I guess this could be packed off in
    some class that would have to be rewritten, too. Just seems to me
    there is a right way and a quick and easy way, they are not the same
    this time.

    Seeing as how I do need it quickly, though, and I'm not having much
    success with JNI, that I'll end up doing it this way now. Luckily, in
    an apparently strange twist of fate, we are actually (painfully slowly)
    replacing Windows boxes with Linux boxes.
     
    GfxGuy, May 28, 2005
    #6
  7. GfxGuy

    Ross Bamford Guest

    On Sat, 2005-05-28 at 05:20 -0700, GfxGuy wrote:
    > Yes and no... if I want to make it portable, the idea is that only a
    > small amount of native code would have to be rewritten and it would
    > integrate better with each OS. I guess this could be packed off in
    > some class that would have to be rewritten, too. Just seems to me
    > there is a right way and a quick and easy way, they are not the same
    > this time.
    >


    Trust me, this is /a/ right way. JNI is /not/ a right way to do this.
    It's not really the right way to do anything, IMHO, but especially not
    simple stuff like this. There are already myriad ways that one program
    can talk to another, VM or no.

    > Seeing as how I do need it quickly, though, and I'm not having much
    > success with JNI, that I'll end up doing it this way now. Luckily, in
    > an apparently strange twist of fate, we are actually (painfully slowly)
    > replacing Windows boxes with Linux boxes.
    >


    Does windows even have file ownership?

    Also seems to me that if you are going to a system without a chown
    binary, then you need to just write one. You still end up with a small
    bit of native code, but probably only a one-liner that compiles without
    the JNI libs.

    --
    [Ross A. Bamford] [ross AT the.website.domain]
    Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
    http://www.roscopec0.f9.co.uk/ + in
     
    Ross Bamford, May 29, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    907
    Pasturel Jean-Louis
    Mar 3, 2004
  2. James Colannino

    os.chown()

    James Colannino, Nov 11, 2005, in forum: Python
    Replies:
    3
    Views:
    9,385
    Tim Roberts
    Nov 13, 2005
  3. Carsten Haese

    Re: chown'ing by script

    Carsten Haese, Jan 6, 2010, in forum: Python
    Replies:
    4
    Views:
    292
    Steve Holden
    Jan 6, 2010
  4. Brian Candler
    Replies:
    2
    Views:
    334
    Emiel van de Laar
    Mar 2, 2005
  5. superfly2

    chown and file ownership

    superfly2, Jul 21, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    134
    J├╝rgen Exner
    Jul 22, 2004
Loading...

Share This Page