• Post Reply Bookmark Topic Watch Topic
  • New Topic

unable to compile (calling C function from Java code)

 
Pingili Vishwanath
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I got the following example program for "calling C function from Java code" from google.

Iam unable to compile. Can anyone tell me how to compile this on Solaris 10 platform?

Thanks in Advance.

---------------------------------------------------------
File name: ReadFile.java

import java.util.*;

class ReadFile {
//Native method declaration
native byte[] loadFile(String name);
//Load the library
static {
System.loadLibrary("nativelib");
}

public static void main(String args[]) {
byte buf[];
//Create class instance
ReadFile mappedFile=new ReadFile();
//Call native method to load ReadFile.java
buf=mappedFile.loadFile("ReadFile.java");
//Print contents of ReadFile.java
for(int i=0;i<buf.length;i++) {
System.out.print((char)buf[i]);
}
}
}
----------------------------------------------------------------------

File Name: loadFile.c

#include <jni.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv * env, jobject jobj, jstring name) {
caddr_t m;
jbyteArray jb;
jboolean iscopy;
struct stat finfo;
const char *mfile = (*env)->GetStringUTFChars(
env, name, &iscopy);
int fd = open(mfile, O_RDONLY);

if (fd == -1) {
printf("Could not open %s\n", mfile);
}
lstat(mfile, &finfo);
m = mmap((caddr_t) 0, finfo.st_size,
PROT_READ, MAP_PRIVATE, fd, 0);
if (m == (caddr_t)-1) {
printf("Could not mmap %s\n", mfile);
return(0);
}
jb=(*env)->NewByteArray(env, finfo.st_size);
(*env)->SetByteArrayRegion(env, jb, 0,
finfo.st_size, (jbyte *)m);
close(fd);
(*env)->ReleaseStringUTFChars(env, name, mfile);
return (jb);
}
-----------------------------------------------------------

File Name: ReadFile.h (this is generated by javah command)

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ReadFile */

#ifndef _Included_ReadFile
#define _Included_ReadFile
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ReadFile
* Method: loadFile
* Signature: (Ljava/lang/String [B
*/
JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif
--------------------------------------------------------------------
[ March 02, 2007: Message edited by: Pingili Vishwanath ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam unable to compile.

What does this mean? Are you getting error messages? Which ones?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!