使用jni我试图让c程序在java下工作。问题是。
首先,我创建了java类testcli.java:
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();
}
然后用
javac testcli.java
并用
javah testcli
之后创建librari ctest.c并用
gcc -o libctest.so -shared -I/path/to/jni.h ctest.c -lc
我创建了libctest.so,然后将以下代码添加到bashrc中:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/Desktop/project2/thesys/src
当我去打字的时候
java testcli
它告诉我“没有这样的文件或目录”。
我在32位机器上运行java8oracle,xubuntu32位。有什么想法吗?
编辑:在这里添加了ctest.c代码(我想这里可能有一些错误)
# 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 *env, jobject jobj)
{
const char *msg = NULL;
perror(msg);
exit(1);
}
JNIEXPORT jint JNICALL Java_testcli_cliFromC
(JNIEnv *env, jobject jobj)
{
/* 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);
}
1条答案
按热度按时间dxxyhpgq1#
实际上,一切都很好。错误来自您的
stdErr()
功能,它实际上表明它在工作。我也有同样的错误。为什么会这样?这将打印程序中导致的最后一个错误。现在,你应该只使用
perror()
如果你的程序真的出错了。否则,它将打印errno
以前是变量。errno
可能是由java本身内部的某些东西设置的。它可能一直在寻找某个文件,但它的不存在对java来说不是问题,所以它继续工作。但是errno
设置为该值并保持该值,这就是程序中打印的内容。为了证明这一点,我在你的报告中增加了以下内容
testcli.java
:还有我的
main
是:以及
ctest.c
我补充说:是吗
javac
,javah
以及gcc
再次运行程序,现在我得到:请注意,由于
exit()
在stdErr()
.