I recently started coding in Java and have been trying to use the native methods to link code from other languages like C and C++.
My code for the file is:

public class NativeDemo {
int i;
public static void main(String args[]) {
NativeDemo ob = new NativeDemo();
ob.i = 10;
System.out.println("This is ob.i before the native method:" +
ob.i);
ob.test(); // call a native method
System.out.println("This is ob.i after the native method:" +
ob.i);
}
// declare native method
public native void test() ;
// load DLL that contains static method
static {
System.loadLibrary("NativeDemo");
}
}

I compiled the file to produce the .class file and then with the following command :
javah -jni NativeDemo
have been able to produce the header file NativeDemo.h

Next I coded the .c file:

/* This file contains the C version of the
test() method.
*/
#include <jni.h>
#include "NativeDemo.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_NativeDemo_test(JNIEnv *env, jobject obj)
{
jclass cls;
jfieldID fid;
jint i;
printf("Starting the native method.\n");
cls = (*env)->GetObjectClass(env, obj);
fid = (*env)->GetFieldID(env, cls, "i", "I");
if(fid == 0) {
printf("Could not get field id.\n");
return;
}
i = (*env)->GetIntField(env, obj, fid);
printf("i = %d\n", i);
(*env)->SetIntField(env, obj, fid, 2*i);
printf("Ending the native method.\n");
}

But now when I try to use this command:
Cl/LD NativeDemo.c
the response I get is:
'Cl' is not recognized as an internal or external command,
operable program or batch file.

How should I proceed after this, and where exactly can I link my .c file with me Java code?

Recommended Answers

All 6 Replies

But now when I try to use this command:
Cl/LD NativeDemo.c
the response I get is:
'Cl' is not recognized as an internal or external command,
operable program or batch file.

The OS can't find the command you entered: Cl
Do you have a Cl.exe file on your computer? Does the PATH point to its location?

No I do not have nay such file on my PC. I thought it would come bundled with the jdk files I downloaded for my Vistas machine.
Can you tell me how I can get it?

Its not part of Java. Try Google.

But now when I try to use this command:
Cl/LD NativeDemo.c
the response I get is:
'Cl' is not recognized as an internal or external command,
operable program or batch file.

That my friend is because you have not installed the C compiler needed to compile your .C file. The function of the above command (Cl/LD) is I suppose to generate a DLL from your C/C++ file, and it corresponds to invoking the C compiler (which the tutorial you are using refers to) just like the javac command invokes the java compiler.
As far as which compiler your tutorial is using ... I dont know, just like NormR1 said google it.

How should I proceed after this, and where exactly can I link my .c file with me Java code?

You cannot link with the source code file, you need to get either a DLL or a *.so file from your .C file to link it with your Java code.

considering the fact that the OP hasn't visited the Daniweb fora for over three years, I'm pretty sure he won't come back to read this update.
pretty pointless to add it and revive the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.