Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

unable to compile (calling C function from Java code)  RSS feed

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

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:

import java.util.*;

class ReadFile {
//Native method declaration
native byte[] loadFile(String name);
//Load the library
static {

public static void main(String args[]) {
byte buf[];
//Create class instance
ReadFile mappedFile=new ReadFile();
//Call native method to load
//Print contents of
for(int i=0;i<buf.length;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,
if (m == (caddr_t)-1) {
printf("Could not mmap %s\n", mfile);
jb=(*env)->NewByteArray(env, finfo.st_size);
(*env)->SetByteArrayRegion(env, jb, 0,
finfo.st_size, (jbyte *)m);
(*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" {
* Class: ReadFile
* Method: loadFile
* Signature: (Ljava/lang/String [B
JNIEXPORT jbyteArray JNICALL Java_ReadFile_loadFile
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
[ March 02, 2007: Message edited by: Pingili Vishwanath ]
Ulf Dittmer
Posts: 42972
  • 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!