participate


Java Native Interface (JNI) - Invoking C# program using .Net framework from a Java application
<<   Back to Forum  |   Give us Feedback
This topic has 10 replies on 1 page.
dgandhi
Posts:2
Registered: 2/14/05
Invoking C# program using .Net framework from a Java application   
Feb 14, 2005 11:19 AM

 
Hi,

I would like to know if it is possible to invoke a C# program using the .NET libraries from a Java application via JNI?
We have an exisiting Java application deployed over the web to client desktops (we bundle the jre along with the application). We have issues with the Java printing as it crashes the application when printing to certain printers. I would like to know if it would be possile to write a C# application that would be able to print - invoked from JNI. Would we have to bundle the .NET CLR along with the application to implement this?? As I do not have much exp with the .NET framework any help/suggestions would be highly appreciated.

thanks
 
IcEtB
Posts:21
Registered: 8/1/04
Re: Invoking C# program using .Net framework from a Java application   
Feb 15, 2005 5:31 AM (reply 1 of 10)  (In reply to original post )

 
Why C#???
Maybe you can make it in native code and then java app will call it, althougth you can call any app no matter the language
 
Peter-Lawrey
Posts:6,487
Registered: 5/5/04
Re: Invoking C# program using .Net framework from a Java application   
Feb 15, 2005 5:43 AM (reply 2 of 10)  (In reply to original post )

 
You may have trouble deploying a JNI solution over the web. e.g. applets
 
jschell
Posts:36,985
Registered: 11/3/97
Re: Invoking C# program using .Net framework from a Java application   
Feb 15, 2005 11:47 AM (reply 3 of 10)  (In reply to original post )

 
I would like to know if it is possible to invoke a C#
program using the .NET libraries from a Java
application via JNI?

If is a program (not a library) then why do you just not run it via Runtime.exec()?

Anyways, the magazine "C/C++ Users Journal" has a commercial product advertised that provides a bridge between languages.

No idea if it works but you might want to look at it.
 
alanyu
Posts:28
Registered: 6/4/97
Re: Invoking C# program using .Net framework from a Java application   
Feb 16, 2005 9:10 AM (reply 4 of 10)  (In reply to original post )

 
I would like to know the answer too.
In my case, I would like to use MSMQ from Java without using any commerical bridge.
I know there is C/C++ MSMQ API, but if JNI can use the .NET classes, the coding would be much much simpler to call the MSMQ .NET classes than calling the C/C++ MSMQ API.
 
dgandhi
Posts:2
Registered: 2/14/05
Re: Invoking C# program using .Net framework from a Java application   
Feb 16, 2005 2:08 PM (reply 5 of 10)  (In reply to #1 )

 
I found the C# code much easier to use along with the .NET libraries - do not have much exposure to Win 32 api - programming :-( Anyways thanks for all your responses - I think I have found a work around to my problem by using SWT/JFace.
 
IonicShade2
Posts:1
Registered: 2/28/05
Re: Invoking C# program using .Net framework from a Java application   
Feb 28, 2005 10:41 AM (reply 6 of 10)  (In reply to #5 )

 
Here is an illustration of using JNI with MSMQ:
http://blogs.msdn.com/dotnetinterop/archive/2005/02/28/381735.aspx
 
wvc-1
Posts:1
Registered: 3/1/05
Re: Invoking C# program using .Net framework from a Java application   
Mar 1, 2005 9:54 AM (reply 7 of 10)  (In reply to original post )

 
...I would like to know if it
would be possile to write a C# application that would
be able to print - invoked from JNI. Would we have
to bundle the .NET CLR along with the application to
implement this?? As I do not have much exp with the
.NET framework any help/suggestions would be highly
appreciated.

Sure, you can do this. Writing the JNI code to bridge between Java and .NET can be pretty ugly, but there are third-party bridges like JNBridge (www.jnbridge.com) that encapsulate this and take care of it in a clean manner. Check it out and see if it does what you want.
 
daddyam
Posts:1
Registered: 7/14/00
Re: Invoking C# program using .Net framework from a Java application   
Nov 2, 2005 12:58 PM (reply 8 of 10)  (In reply to original post )

 
Hi,

I would like to know if it is possible to invoke a C#
program using the .NET libraries from a Java
application via JNI?
We have an exisiting Java application deployed over
the web to client desktops (we bundle the jre along
with the application). We have issues with the Java
printing as it crashes the application when printing
to certain printers. I would like to know if it
would be possile to write a C# application that would
be able to print - invoked from JNI. Would we have
to bundle the .NET CLR along with the application to
implement this?? As I do not have much exp with the
.NET framework any help/suggestions would be highly
appreciated.

thanks

Hi,

You should take a look at the J-Integra Interoperability suite.

http://j-integra.intrinsyc.com

They have several different products that would meet your needs in bridging java to .NET.

Cheers,
Adam
 
DmitryRiachtchentsev
Posts:1
Registered: 03.11.05
Re: Invoking C# program using .Net framework from a Java application   
Nov 3, 2005 9:42 AM (reply 9 of 10)  (In reply to original post )

 
You can use the following API to run your .NET application via JNI.
Note, JVM and CLR co-existance in the same process is introduced in Java 6.0. I did not try to run it on the lower versions.

#include "windows.h"
#include "Oleauto.h"
#include "stdio.h"
#include "mscoree.h"
#import <mscorlib.tlb> raw_interfaces_only high_property_prefixes("_get","_put","_putref")

using namespace mscorlib;

WCHAR* createWCHAR( const char* szString ){
int nLength = strlen(szString)+1;
WCHAR* wszString = new WCHAR[nLength];
mbstowcs(wszString, szString, nLength);
return wszString;
}

...

char* szApplication="path"; // your .NET application
char* szVersion="v1.1.4322";// installed .NET version
char* szBuildFlavor="wks"; // "wks" or "svr"
char* szConfig=""; // path to the config file (if necessary)

WCHAR* wszApplication = createWCHAR(szApplication);
WCHAR* wszVersion = createWCHAR(szVersion);
WCHAR* wszBuildFlavor = createWCHAR(szBuildFlavor);
WCHAR* wszConfig = createWCHAR(szConfig);

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

ICorRuntimeHost* pHost = NULL;
IUnknown* pAppDomainThunk = NULL;
_AppDomain* pAppDomain = NULL;

HRESULT hr = CorBindToRuntimeHost( wszVersion, wszBuildFlavor, wszConfig, NULL,0,CLSID_CorRuntimeHost,IID_ICorRuntimeHost,(VOID**)&pHost);
if (FAILED(hr)) {
printf("CorBindToRuntimeHost error=%x\n", hr);
return -1;
}

hr = pHost->Start();
if (FAILED(hr)) {
printf("ICorRuntimeHost->Start error=%x\n", hr);
return -2;
}

hr = pHost->GetDefaultDomain(&pAppDomainThunk);
if (FAILED(hr)) {
printf("ICorRuntimeHost->GetDefaultDomain error=%x\n", hr);
return -3;
}

hr = pAppDomainThunk->QueryInterface(__uuidof(_AppDomain),(void**) &pAppDomain);
if (FAILED(hr)) {
printf("System::_AppDomain getting interface error=%x\n",hr);
return -4;
}

long lRet= 0;
// Execute the .NET assembly
hr = pAppDomain->ExecuteAssembly_2(_bstr_t(wszApplication), &lRet);
if (FAILED(hr)) {
printf("AppDomain::ExecuteAssembly_2 error=%x\n", hr);
ErrorDescription(hr);
return -5;
}

//lRet contains the exit code

CoUninitialize();

delete wszApplication;
delete wszVersion;
delete wszBuildFlavor;
delete wszConfig;


--Dmitry
 
rodk
Posts:12
Registered: 6/5/97
Re: Invoking C# program using .Net framework from a Java application   
Dec 4, 2007 12:19 PM (reply 10 of 10)  (In reply to #8 )

 
It is not pretty, but you can also run your C# code in a Windows Service, and have it listen on a socket.

When the Java program wants to invoke the C# code, it opens a socket to localhost, writes the parameters as a String (or whatever format you want). The C# code reads the String from the socket, performs the operations, and sends a response over the socket connection, where the Java program receives it.

The advantage of this approach is that you don't need a third party package. The disadvantages are you need a Windows Service, you have to worry about security (e.g., the C# code should make sure the connection is from localhost), and performance (setting up a socket is probably slower than making a JNI call).
 
This topic has 10 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 26
  • Guests : 129

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums