简体   繁体   English

如何在java上使用PJSIP等C库?

[英]How to use a C library such as PJSIP on java?

Is there a way to use and invoke method calls from a library compiled in C such as the PJSIP library? 有没有办法从C编译的库中使用和调用方法调用,如PJSIP库?

I want to use and invoke calls from the pjsip lib that i have compiled but the obvious issue is that pjsip is a C library not a java library ie a jar file. 我想使用和调用我编译的pjsip lib中的调用,但显而易见的问题是pjsip是一个C库而不是一个java库,即一个jar文件。

Possible? 可能? Maybe JNI? 也许是JNI?

Thanks 谢谢

edit: tried using Swig to do the task at hand but all it does is create a empty class file. 编辑:尝试使用Swig来完成手头的任务,但它只是创建一个空的类文件。

here is the command i use to execute swig: 这是我用来执行swig的命令:

swig  -verbose -java -package com.josh.sip.util -o jni_wrapper.c pjsua.i

output from swig command swig命令输出

    Language subdirectory: java
Search paths:
   .\
   .\swig_lib\java\
   C:\Users\jonney\Downloads\swigwin-2.0.9\Lib\java\
   .\swig_lib\
   C:\Users\jonney\Downloads\swigwin-2.0.9\Lib\
Preprocessing...
Starting language-specific parse...
Processing types...
C++ analysis...
Generating wrappers...

pjsua java class it creates for me: 它为我创建的pjsua java类:

    /* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 2.0.9
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */

package com.josh.sip.util;

public class pjsua {
}

The pjsua jni file it creates for me 它为我创建的pjsua jni文件

/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 2.0.9
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */

package com.josh.sip.util;

public class pjsuaJNI {
}

The jni_wrapper.c it generates for me below: 它为我生成的jni_wrapper.c如下:

    /* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 2.0.9
 * 
 * This file is not intended to be easily readable and contains a number of 
 * coding conventions designed to improve portability and efficiency. Do not make
 * changes to this file unless you know what you are doing--modify the SWIG 
 * interface file instead. 
 * ----------------------------------------------------------------------------- */

#define SWIGJAVA

/* -----------------------------------------------------------------------------
 *  This section contains generic SWIG labels for method/variable
 *  declarations/attributes, and other compiler dependent labels.
 * ----------------------------------------------------------------------------- */

/* template workaround for compilers that cannot correctly implement the C++ standard */
#ifndef SWIGTEMPLATEDISAMBIGUATOR
# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560)
#  define SWIGTEMPLATEDISAMBIGUATOR template
# elif defined(__HP_aCC)
/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */
/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */
#  define SWIGTEMPLATEDISAMBIGUATOR template
# else
#  define SWIGTEMPLATEDISAMBIGUATOR
# endif
#endif

/* inline attribute */
#ifndef SWIGINLINE
# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
#   define SWIGINLINE inline
# else
#   define SWIGINLINE
# endif
#endif

/* attribute recognised by some compilers to avoid 'unused' warnings */
#ifndef SWIGUNUSED
# if defined(__GNUC__)
#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
#     define SWIGUNUSED __attribute__ ((__unused__)) 
#   else
#     define SWIGUNUSED
#   endif
# elif defined(__ICC)
#   define SWIGUNUSED __attribute__ ((__unused__)) 
# else
#   define SWIGUNUSED 
# endif
#endif

#ifndef SWIG_MSC_UNSUPPRESS_4505
# if defined(_MSC_VER)
#   pragma warning(disable : 4505) /* unreferenced local function has been removed */
# endif 
#endif

#ifndef SWIGUNUSEDPARM
# ifdef __cplusplus
#   define SWIGUNUSEDPARM(p)
# else
#   define SWIGUNUSEDPARM(p) p SWIGUNUSED 
# endif
#endif

/* internal SWIG method */
#ifndef SWIGINTERN
# define SWIGINTERN static SWIGUNUSED
#endif

/* internal inline SWIG method */
#ifndef SWIGINTERNINLINE
# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE
#endif

/* exporting methods */
#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#  ifndef GCC_HASCLASSVISIBILITY
#    define GCC_HASCLASSVISIBILITY
#  endif
#endif

#ifndef SWIGEXPORT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#   if defined(STATIC_LINKED)
#     define SWIGEXPORT
#   else
#     define SWIGEXPORT __declspec(dllexport)
#   endif
# else
#   if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
#     define SWIGEXPORT __attribute__ ((visibility("default")))
#   else
#     define SWIGEXPORT
#   endif
# endif
#endif

/* calling conventions for Windows */
#ifndef SWIGSTDCALL
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#   define SWIGSTDCALL __stdcall
# else
#   define SWIGSTDCALL
# endif 
#endif

/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
# define _CRT_SECURE_NO_DEPRECATE
#endif

/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
# define _SCL_SECURE_NO_DEPRECATE
#endif



/* Fix for jlong on some versions of gcc on Windows */
#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
  typedef long long __int64;
#endif

/* Fix for jlong on 64-bit x86 Solaris */
#if defined(__x86_64)
# ifdef _LP64
#   undef _LP64
# endif
#endif

#include <jni.h>
#include <stdlib.h>
#include <string.h>


/* Support for throwing Java exceptions */
typedef enum {
  SWIG_JavaOutOfMemoryError = 1, 
  SWIG_JavaIOException, 
  SWIG_JavaRuntimeException, 
  SWIG_JavaIndexOutOfBoundsException,
  SWIG_JavaArithmeticException,
  SWIG_JavaIllegalArgumentException,
  SWIG_JavaNullPointerException,
  SWIG_JavaDirectorPureVirtual,
  SWIG_JavaUnknownError
} SWIG_JavaExceptionCodes;

typedef struct {
  SWIG_JavaExceptionCodes code;
  const char *java_exception;
} SWIG_JavaExceptions_t;


static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg) {
  jclass excep;
  static const SWIG_JavaExceptions_t java_exceptions[] = {
    { SWIG_JavaOutOfMemoryError, "java/lang/OutOfMemoryError" },
    { SWIG_JavaIOException, "java/io/IOException" },
    { SWIG_JavaRuntimeException, "java/lang/RuntimeException" },
    { SWIG_JavaIndexOutOfBoundsException, "java/lang/IndexOutOfBoundsException" },
    { SWIG_JavaArithmeticException, "java/lang/ArithmeticException" },
    { SWIG_JavaIllegalArgumentException, "java/lang/IllegalArgumentException" },
    { SWIG_JavaNullPointerException, "java/lang/NullPointerException" },
    { SWIG_JavaDirectorPureVirtual, "java/lang/RuntimeException" },
    { SWIG_JavaUnknownError,  "java/lang/UnknownError" },
    { (SWIG_JavaExceptionCodes)0,  "java/lang/UnknownError" }
  };
  const SWIG_JavaExceptions_t *except_ptr = java_exceptions;

  while (except_ptr->code != code && except_ptr->code)
    except_ptr++;

  (*jenv)->ExceptionClear(jenv);
  excep = (*jenv)->FindClass(jenv, except_ptr->java_exception);
  if (excep)
    (*jenv)->ThrowNew(jenv, excep, msg);
}


/* Contract support */

#define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else


#include "pjsua.h"


#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

I have the pjsua.h file inside same location as my .i file so its easier to find both whilst executing the swig command and any advice? 我将pjsua.h文件放在与我的.i文件相同的位置,这样在执行swig命令和任何建议时更容易找到它们?

I basically want swig or any other tool to create a pjsua or pjsip java class with all its implementations or interface methods i can use to actually use the library. 我基本上想要swig或任何其他工具来创建一个pjsua或pjsip java类,其中包含我可以用来实际使用库的所有实现或接口方法。

Such as a pain that there is no java version of this sip stack 比如这个sip堆栈没有java版本的痛苦

I've found the simplest way to do this, if you have the header files, is to use SWIG . 我发现最简单的方法是,如果你有头文件,就是使用SWIG You create a yourmodule.i file for your module (the library) and include all of the classes you want to access via JNI. 您为模块(库)创建了一个yourmodule.i文件,并包含您要通过JNI访问的所有类。 Eg: 例如:

%module Foo
%{
#include "Bar.h"
%}

You can also provide typemaps from C to Java structures. 您还可以提供从C到Java结构的类型映射。

If you want to ignore a certain method: 如果你想忽略某种方法:

%ignore fooBar(int baz);

You can run swig to generate some java classes like this: 你可以运行swig来生成一些像这样的java类:

swig -c -verbose -java -package com.your.java.package -o jni_wrapper.c yourfile.i

See the Java documentation for more information and examples. 有关更多信息和示例,请参阅Java文档

Try JNA with JNAerator: https://code.google.com/p/jnaerator . 使用JNAerator尝试JNA: https//code.google.com/p/jnaerator I've used it successfully in my own projects. 我在自己的项目中成功使用了它。

I would recommend using Javacpp I used it before and it worked pretty good, the thing you need here is just a bridge, SWIG is not very friendly, and I wasted a lot of time with it before. 我建议使用Javacpp我之前使用它并且它工作得很好,你需要的东西只是一个桥梁,SWIG不是很友好,我以前浪费了很多时间。

As for the requested specific library try this one pjsip-jni , its wrapper for the pjsip written in java and jni. 至于请求的特定库,请尝试使用pjsip-jni ,它是用java和jni编写的pjsip的包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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