Hello,

So I'm following this tutorial (http://www.csharp.com/javacfort.html) to learn how to link java with C and I want to be able to link java to two C programs (one for linux and one for windows). How do I create a java class that figures out what OS it's on and then chooses the appropriate c code to use? Also the tutorial was written for linux, so would there be any difference with windows?

Thanks

For info on linux VS windows in the JNI read this.
to find out the operating system you can use System.getProperty("os.name"); NOTE: i am not too familiar with the JNI myself, but it is one of the things about java that typically destroys platform independence, but what your trying to do may be possible, but as i said i am not too familiar with the JNI so i cannot be sure

I"M STUCK!!!!!!! (step 5 in the tutorial)
Let me know what I screwed up on......
Is there an easier way to link c and java?????


Error:

gcc -c -D_REENTRANT -fPIC \ -I/home/csharp/java/j2sdk1.4.2_03/include \ -I/home/csharp/java/j2sdk1.4.2_03/include/linux -c LinCode.c
gcc: -I/home/csharp/java/j2sdk1.4.2_03/include: No such file or directory
gcc: -I/home/csharp/java/j2sdk1.4.2_03/include/linux: No such file or directory
In file included from LinCode.c:7:
OneWayCom.h:2:17: error: jni.h: No such file or directory
In file included from LinCode.c:7:
OneWayCom.h:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘jint’
OneWayCom.h:23: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘jint’
OneWayCom.h:31: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
LinCode.c:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘jint’

C code:

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include "OneWayCom.h"  // Required header for JNI


JNIEXPORT jint JNICALL Java_OneWayCom_openPort(JNIEnv *env, jobject obj, jchar portNum)
{
   jint fd;/* File descriptor for the port */
   char portName[11] = "/dev/ttyS";
   portName[9] = portNum;
   portName[10] = '\0';
   fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);

   if (fd != -1)
   {
      fcntl(fd, F_SETFL, 0);
      struct termios options;

       /*
        * Get the current options for the port...
        */

       tcgetattr(fd, &options);

       /*
        * Set the baud rates to 9600...
        */

        cfsetispeed(&options, B9600);
        cfsetospeed(&options, B9600);
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;
        options.c_oflag &= ~OPOST; //raw output

       /*
        * Enable the receiver and set local mode...
        */

        options.c_cflag |= (CLOCAL | CREAD);

       /*
        * Set the new options for the port...
        */

       tcsetattr(fd, TCSANOW, &options);

   return fd;
}


JNIEXPORT jint JNICALL Java_OneWayCom_send(JNIEnv *env, jobject obj, jintArray data, jint size, jint fd)
{
   char array[size+1];
   array[size] = '\0';
   for(int i=0; i < size; i++)
   {
      array[i] = (char) (0x00 + data[i]);
   }
   return write(fd,array , size);
}

JNIEXPORT void JNICALL Java_OneWayCom_end(JNIEnv *env, jobject obj, jint fd)
{
   close(fd);
}

Other stuff:

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

#ifndef _Included_OneWayCom
#define _Included_OneWayCom
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     OneWayCom
 * Method:    openPort
 * Signature: (C)I
 */
JNIEXPORT jint JNICALL Java_OneWayCom_openPort
  (JNIEnv *, jobject, jchar);

/*
 * Class:     OneWayCom
 * Method:    send
 * Signature: ([III)I
 */
JNIEXPORT jint JNICALL Java_OneWayCom_send
  (JNIEnv *, jobject, jintArray, jint, jint);

/*
 * Class:     OneWayCom
 * Method:    end
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_OneWayCom_end
  (JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif

Actually I just copied and pasted those commands without realizing what they were....... I'm using Ubuntu, so I'm clueless in regard about where anything is located in Ubuntu. What would the command be for both Ubuntu and Win XP???

it looks like you are including java's include stuff, i don't know anything about linux or Ubutnu. and i am not sure about whether it is the same for both systems, i think i have said all that i can say about the JNI.

Time to wait for a professional, sorry i cannot help anymore :(

This article has been dead for over six months. Start a new discussion instead.