`

C# method calls within a Java program

阅读更多

.net产生的比java晚,其类库的封装在某些方面也比java更优秀,更全面。比如最近在做一个OJ,看到网上的一些做法是用java+c++,C++用作所提交程序的测试。c++虽然好,但是他的编写比较复杂。因此,我选择的是C#,用.net的类库可以很方便的获得一个进程(用户提交的程序)运行的时间和消耗的内存。下面是我的测试程序:

 

首先我在网上查了一些资料:

C# method calls within Java Program

 

这篇文章大概传达了这样一个意思:

 

Java 调用C#过程:
Java -> JNI -> C++dll  <== Managed C++==> C# dll

 

使用C++调用C#的DLL

上面这篇文章我搜了一下,已经被转载了无数次了,关于网上c++调c# dll基本都是这篇文章

 

然后再用google搜一下jni的例子是一堆一堆的。

 

====================================================================

 

了解了java调c#在简单的过程,再了解了jni和c++如何调c#,这样用java调c#应该就没什么问题了.但是本人在做的时候还有一点小小的路径问题,报了一个jvmunexpected exception,让我真是郁闷了好久。

 

====================================================================

 

先写一个java类

 

Java代码 
  1. package com.ypoj.jni;  
  2.   
  3. public class TestJNI {  
  4.   
  5.     public native int add(int a, int b);  
  6.   
  7.     static {  
  8.         System.loadLibrary("CallCS");  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         TestJNI t = new TestJNI();  
  12.         System.out.println(t.add(1020));  
  13.     }  
  14. }  

 

然后用javah命令产生.h的文件(网上jni的文章介绍的很多)

我使用的IDE是NetBeans,生成的.class文件和源文件不在同一个文件夹下,把TestJNI.class拷贝到TestJNI.java同一目录下。然后运行cmd.exe在src目录下,javahcom.ypoj.jni.TestJNI

 

新建一个c++的类库,本人使用的是VS2008,这里需要注意的是,在刚刚讲到原理的时候是java->c++dll->managedc++->c#,然而vc++里面已经集成了managedc++,所以其实我做的时候并没有把c++和managedc++分开来做,因为vs2008里已经集成好了,具体做法是:选择《项目》->《属性页》->《配置属性》->《常规》->《公共语言运行库支持》,选择公共语言运行库支持(/clr)。这样就可以了

 

用javah生成的com_ypoj_jni_TestJNI.h

 

Cpp代码 
  1.   
  2. #include <jni.h>  
  3.   
  4.   
  5. #ifndef _Included_com_ypoj_jni_TestJNI  
  6. #define _Included_com_ypoj_jni_TestJNI  
  7. #ifdef __cplusplus  
  8. extern "C" {  
  9. #endif  
  10.   
  11. JNIEXPORT jint JNICALL Java_com_ypoj_jni_TestJNI_add  
  12.   (JNIEnv *, jobject, jint, jint);  
  13.   
  14. #ifdef __cplusplus  
  15. }  
  16. #endif  
  17. #endif  

 

 

这里我们就实现这个方法

JNIEXPORT jint JNICALLJava_com_ypoj_jni_TestJNI_add
  (JNIEnv *, jobject, jint, jint);

 

在编译的时候会提示需要加入两个头文件,分别是jni.h和jni_md.h,这两个文件可以在你本机安装的jdk的文件夹里搜到。如果编译的时候说找不到jni.h,则include的时候写成 #include "jni.h",<>改成""。

 

接下来先完成C#的 Dll

 

C#代码 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace OJMain  
  7. {  
  8.     public class OJEntrance  
  9.     {  
  10.         private int result;  
  11.         public int Result  
  12.         {  
  13.             get { return result + 10; }  
  14.             set { this.result = value; }  
  15.         }  
  16.     }  
  17. }  

 

 

最后实现C++里的那个函数

首先把生成的C#的Dll拷贝到C++源文件的同一个目录下,也就是和C++的dll,.cpp文件放在同一个目录,这里不是把C#的dll放在c++dll同一目录(Debug目录),虽然放在Debug目录也可以(改变引入的路径),但是当用java再调的时候就会出错了。

 

注意:一定先把c#的dll拷到c++的项目里,然后再去写那个jni函数

 

c++ 主 DLL 文件

 

Cpp代码 
  1. #include "stdafx.h"  
  2.   
  3. #include "jni.h"  
  4. #include "com_ypoj_jni_TestJNI.h"  
  5. #include "CallCS.h"  
  6.   
  7. //引入c#的库和命名空间  
  8. #using "OJMain.dll"  
  9. using namespace OJMain;  
  10.   
  11. JNIEXPORT jint JNICALL Java_com_ypoj_jni_TestJNI_add  
  12.   (JNIEnv *env, jobject obj, jint a, jint b)  
  13. {  
  14.     //c#中的对象  
  15.     OJEntrance ^o = gcnew OJEntrance();  
  16.     o->Result = a + b;  
  17.     return o->Result;  
  18. }  

 

 

生成c++的dll,

 

最后把c#和c++的dll拷贝到library.path下,我把他们拷贝在了jdk的bin目录下

 

运行结果

 

run:
40
成功生成(总时间:0 秒)

分享到:
评论

相关推荐

    CSharp-calls-JAVA-program.rar_C#调用java的dll_c# 调用 java sdk_c#调用ja

    在C#环境下调用JAVA程序的实例代码,添加dll文件后,引用package ,创建环境变量,即可调用java写的类及构造方法

    StyleCop(Microsoft Source Analysis for C#)

    (参数)Placement of method parameters within method declarations or method calls (元素排列)Standard ordering of elements within a class (注释格式)Formatting of documentation within element ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Calling a C Function from a Java Program 936 Numeric Parameters and Return Values 942 String Parameters 944 Accessing Fields 950 Encoding Signatures 954 Calling Java Methods 956 Accessing Array ...

    Remote Program Calls

    Remote Program Calls:SAP RFC介绍

    Array index out of bound exception

    The program next reads in an index k from the user, then calls a method readValue(int [ ] a, int k) that would return the value of a[k]. The main program displays the value a[k]. If the index is out ...

    Java Web Services

    Java Web Services shows you how to use SOAP to perform remote method calls and message passing;how to use WSDL to describe the interface to a web service or understand the interface of someone else's ...

    Allows a low-privileged COM client to delegate calls to a CO

    Allows a low-privileged COM client to delegate calls to a COM server that is running under a higher-priveleged NT user account.(41KB)

    java web services

    Java Web Services shows you how to use SOAP to perform remote method calls and message passing; how to use WSDL to describe the interface to a web service or understand the interface of someone else's...

    Twilio with C# Succinctly

    A functional introduction to Twilio for experienced C# developers. Ed Freitas will guide readers towards developing voice and messaging apps in C# using Twilio. With just some experience with C#, ...

    C#调用API串口通信.zip_API串口_C# API 串口_C# 串口通信_barn7nd_c# win串口api

    C#调用API串口通信C# calls API serial communication

    C# Game Programming Cookbook for Unity 3D - 2014

    4.3.1 A Simple Spawn Controller..................................49 4.3.1.1 Script Breakdown................................52 4.3.2 Trigger Spawner...........................................56 4.3.3 ...

    Typemock Isolator-Developers Guide

    Allows arbitrary calls, or rejects any method calls that were not explicitly expected Specifies sequenced and default return values Supports parameters and indexes Specifies dynamic return values ...

    Tracing Oracle Internal Calls

    Tracing Oracle Internal Calls

    java-cat-calls.rar_Cat-Calls

    java经典的猫叫程序,适合对面向对象概念的深入理解,运行好用

    WindowsFormsApplication1_C#_

    Callbacks and events are invoked on the thread ... Method calls will block until that thread becomes available. An exception will be generated if the thread does not become available in a timely manner.

    NET代码复杂度检查工具

    参数位置(Placement of method parameters within method declarations or method calls ) 元素标准排列(Standard ordering of elements within a class ) 注释格式(Formatting of documentation within ...

    A-key-query-mobile-calls.zip_prepaid java

    考虑到经常打电话查询话费使用情况,觉得甚是麻烦,于是就想开发个小程序来简化这个过程,因此就有了下面这个小程序,之所以称之为小程序,是因为它的功能很单一,就是查询话费使用情况和话费余额,但这也是日常生活...

    Using java swing components in MATLAB

    JCONTROL provides an easy way to integrate a full range of java GUIs from the java.awt and javax.swing libraries into MATLAB. Example: obj=JCONTROL(Parent, Style); obj=JCONTROL(Parent, Style,... ...

    C#学习的101个经典例子

    #学习的101个经典例子,例子个个经典,涵盖C#的方方面面,带有说详尽的注释 Advanced - Multithreading - How-To Async Calls Advanced - Remoting - How-To TCP Remoting Advanced - Serialization - How-To ...

Global site tag (gtag.js) - Google Analytics