简体   繁体   中英

Compiling errors in a C program under JNI

first of all i'd like to point the fact i'm not too expert with Java and less with C, after that, i'm using JNI to use a C program which is a client application for sockets.

First i've created the main class testcli:

public class testcli {

    public native void stdErr();
    public native int cliFromC();


    static {
        System.loadLibrary("ctest"); 
    }

    public static void main(String[] args) {

        new testcli().stdErr();
        new testcli().cliFromC();

    }

}

After that i compiled with

javac testcli.java

then created the header file

javah testcli

At this point i've copied the strings i needed and pasted in the new file ctest:

#include <jni.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>          
#include <arpa/inet.h>
#include <netdb.h>

#define PORT 20000
#define LENGTH 512 

JNIEXPORT void JNICALL Java_testcli_stdErr
  (JNIEnv *, jobject)

{
    perror(msg);
    exit(1);
}

JNIEXPORT jint JNICALL Java_testcli_cliFromC
  (JNIEnv *, jobject)

{
    /* Variable Definition */
    int sockfd; 
    int nsockfd;
    char revbuf[LENGTH]; 
    struct sockaddr_in remote_addr;
    //struct hostent *server; // per la parte scritta da me

    /* Get the Socket file descriptor */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor! (errno =%d)\n",errno);
        exit(1);
    }


    /* Fill the socket address struct */
    remote_addr.sin_family = AF_INET; 
    remote_addr.sin_port = htons(PORT); 

    /*questo lo faccio così perchè localhost è 127.0.0.1, nel caso in cui debba recuperare l'ip dall'hostname uso la parte commentata sopra*/
    inet_pton(AF_INET, "127.0.0.1", &remote_addr.sin_addr); //metto in remote_addr.sin_addr l'indirizzo ip 127.0.0.1 nel formato desiderato



    bzero(&(remote_addr.sin_zero), 8);

    /* Try to connect the remote */
    if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
    {
        fprintf(stderr, "ERROR: Failed to connect to the host! (errno = %d)\n",errno);
        exit(1);
    }
    else 
        printf("[Client] Connected to server at port %d...ok!\n", PORT);

/* Send File to Server */
    //if(!fork())
    //{
        char* fs_name = "/home/elia/Desktop/project/tesi/PublicKey1";
        char sdbuf[LENGTH]; 
        printf("[Client] Sending %s to the Server... ", fs_name);
        FILE *fs = fopen(fs_name, "r");
        if(fs == NULL)
        {
            printf("ERROR: File %s not found.\n", fs_name);
            exit(1);
        }

        bzero(sdbuf, LENGTH); 
        int fs_block_sz; 
        while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
        {
            if(send(sockfd, sdbuf, fs_block_sz, 0) < 0)
            {
                fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
                break;
            }
            bzero(sdbuf, LENGTH);
        }
        printf("Ok File %s from Client was Sent!\n", fs_name);
    //}
    close (sockfd);
    printf("[Client] Connection lost.\n");
    return (0);

    }   

At this point i added to my bashrc those lines:

C_INCLUDE_PATH=/usr/lib/jvm/java-8-oracle/include/
C_INCLUDE_PATH=/usr/lib/jvm/java-8-oracle/include/linux/

export C_INCLUDE_PATH

Saved and wrote in terminal

ECHO $C_INCLUDE_PATH

and it gave me only

/usr/lib/jvm/java-8-oracle/include/linux/

which i don't know if it's good or not cause first string is missing. After it i try to compile my ctest.c with this command line:

gcc -o libctest.so -shared -I/usr/lib/jvm/java-8-oracle/include ctest.c -lc

and have this errors, which i completely don't understand.

ctest.c: In function ‘Java_testcli_stdErr’:
ctest.c:19: error: parameter name omitted
ctest.c:19: error: parameter name omitted
ctest.c:22: error: ‘msg’ undeclared (first use in this function)
ctest.c:22: error: (Each undeclared identifier is reported only once
ctest.c:22: error: for each function it appears in.)
ctest.c: In function ‘Java_testcli_cliFromC’:
ctest.c:27: error: parameter name omitted
ctest.c:27: error: parameter name omitted

I'm figurin out that completely copy-pasting my working client.c into the ctest.c not work well but i sincerely don't know how to fix it. One thing to know is that C code works alone, with obvious

void error(const char *msg)

before the first brace bracket and

int main(int argc, char *argv[])

before second one. If some1 can help it's really really appreciated. Thx very much for the effort!!

Cheers

JNIEXPORT void JNICALL Java_testcli_stdErr
(JNIEnv *, jobject)
{
    perror(msg);
    exit(1);
}

The first thing wrong here is that your parameters don't have names. While this can be effectively ignored with the proper compiler flags, I wouldn't recommend it. You should give your parameters names, eg:

JNIEXPORT void JNICALL Java_testcli_stdErr
(JNIEnv* env, jobject obj)
...

The next issue is perror(msg); , as msg isn't declared nor defined. Did you forget to pass it into your function? Is it supposed to be a global variable? Either way, it doesn't exist, and you can't print something that doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM