Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI/Linux/chown

Reply
Thread Tools

JNI/Linux/chown

 
 
GfxGuy
Guest
Posts: n/a
 
      05-28-2005
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...

 
Reply With Quote
 
 
 
 
Lucy
Guest
Posts: n/a
 
      05-28-2005
> .> 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


 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      05-28-2005
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
 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      05-28-2005
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


 
Reply With Quote
 
GfxGuy
Guest
Posts: n/a
 
      05-28-2005
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.

 
Reply With Quote
 
GfxGuy
Guest
Posts: n/a
 
      05-28-2005
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.

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      05-29-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments