[Guide] Simple C# in Java

1,375 views
Skip to first unread message

Loc Nguyen

unread,
Oct 30, 2014, 1:17:54 AM10/30/14
to jni...@googlegroups.com
I spent a day figuring out how to do this. Here's how I did it:



-MY TOOLS
/*
Win 7 x64 Pro
Java 1.7 x86
Visual Studio 2010 Ultimate
Eclipse
*/



-PREP
Must have Visual Studio. This guide uses Visual Studio 2010.
Must have .NET 4. Mind the framework version. For now use v4.0.30319.
Must have Java 1.5+. JAVA_HOME set correctly.
IDE of your choice
. This guide uses Eclipse.
Download the jni4net binaries.

Also, a .NET-compiled DLL of your choice. Example:





















-STEP 1
Extract from jni4net-0.8.8.0-bin.zip/
bin/proxygen.exe
bin
/proxygen.exe.config
lib
/jni4net.j-0.8.8.0.jar
(x86) lib/jni4net.n.w32.v40-0.8.8.0.dll or (x64) lib/jni4net.n.w64.v40-0.8.8.0.dll
lib
/jni4net.n-0.8.8.0.dll



-STEP 2
Open Visual Studio Command Prompt.
Enter:
corflags c:\users\loc\desktop\proxygen.exe /32BIT+ /Force

Use the correct path above. proxygen.exe should now be modified.



-STEP 3
Ready DLL or JAR assembly to have proxies generated.
Open Command Prompt. See usage details:
usage: proxygen.exe path\to\<config>.xml
usage
: proxygen.exe path\to\<library>.jar -wd work\directory -cp java\dependenci
es
;comma\separated;classpath -dp .NET\dependencies;comma\separated;assemblies
usage
: proxygen.exe path\to\<library>.dll -wd work\directory -cp java\dependenci
es
;comma\separated;classpath -dp .NET\dependencies;comma\separated;assemblies

Example usage of proxygen.exe to generate .NET proxies for Java:
// Short example
c
:\users\loc\desktop\proxygen.exe "c:\users\loc\desktop\testlibrary.dll" -wd "c:\users\loc\desktop"

// Crazy example with dependencies
desktop
\proxygen.exe "C:\Users\Loc\Documents\Visual Studio 2010\Projects\hasuite\maplelib2\bin\Release\maplelib.dll" -wd "c:\users\loc\desktop" -cp "C:\Users\Loc\Documents\Visual Studio 2010\Projects\hasuite\maplelib2\bin\Release\naudio.dll"

Use correct paths above.



-STEP 4
Edit the newly generated build.cmd file.

Near the bottom, where there is
csc

replace with:
(x86)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
(x64)
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe


Run build.cmd



-STEP 5
Set up your project pipeline in Eclipse by importing the above, somewhat like this:




-STEP 6
Add some code:
package com.skipper.test;

import java.io.File;
import java.io.IOException;

import testlibrary.Test;
import net.sf.jni4net.Bridge;

public class Program {

public static void main(String[] args) {
try {
Bridge.setVerbose(true);
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new File("testlibrary.j4n.dll"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Test test = new Test();
test.Hello();
test.Repeat("It works!");
}
}



-STEP (FINAL)
Run the Java application and profit.
loading core from C:/Users/Loc/Documents/_Java/Jni4NetTest/jni4net.n-0.8.8.0.dll
loading jni4net.n-0.8.8.0, Version=0.8.8.0, Culture=neutral, PublicKeyToken=134a23405600bab4 from C:\Users\Loc\Documents\_Java\Jni4NetTest\jni4net.n-0.8.8.0.dll
loaded jni4net.n-0.8.8.0, Version=0.8.8.0, Culture=neutral, PublicKeyToken=134a23405600bab4 from C:\Users\Loc\Documents\_Java\Jni4NetTest\jni4net.n-0.8.8.0.dll
Initialized jni4net core
core loaded from C:/Users/Loc/Documents/_Java/Jni4NetTest/jni4net.n-0.8.8.0.dll
clr.version         :v4.0.30319
clr.arch            :32bit
java.home           :C:\Program Files (x86)\Java\jre7
java.version        :1.7.0_65
sun.arch.data.model :32
loading testlibrary.j4n, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null from C:\Users\Loc\Documents\_Java\Jni4NetTest\testlibrary.j4n.dll
loaded testlibrary.j4n, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null from C:\Users\Loc\Documents\_Java\Jni4NetTest\testlibrary.j4n.dll
Hello World, from C#!
It works!

Pavel Šavara

unread,
Oct 30, 2014, 3:34:24 AM10/30/14
to jni...@googlegroups.com
Hi Loc,

this is great stuff. Would you please contribute it to the wiki on gitHub ?

Thanks a lot
Pavel


--
--
You received this message because you are subscribed to jni...@googlegroups.com
http://groups.google.com/group/jni4net?hl=en-GB?hl=en-GB
http://jni4net.sf.net/

---
You received this message because you are subscribed to the Google Groups "jni4net" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jni4net+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Loc Nguyen

unread,
Oct 30, 2014, 9:15:48 PM10/30/14
to jni...@googlegroups.com
Wiki was kickstarted. Talented github wiki writers and jni4net experts please contribute as well.

Pavel Šavara

unread,
Oct 31, 2014, 4:12:21 AM10/31/14
to jni...@googlegroups.com
I updated the web page to link to it, thanks again :-)

On Fri, Oct 31, 2014 at 2:15 AM, Loc Nguyen <locngu...@gmail.com> wrote:
Wiki was kickstarted. Talented github wiki writers and jni4net experts please contribute as well.

--

dushkin

unread,
Apr 12, 2015, 1:40:04 AM4/12/15
to jni...@googlegroups.com
Loc, Pavel,

 Thank you for your detailed walkthrough.
 However, I got some BadImageFormatException issue.
 It is detailed in a new post in the group.

 Would you kindly advise?

Thanks!

בתאריך יום חמישי, 30 באוקטובר 2014 בשעה 07:17:54 UTC+2, מאת Loc Nguyen:
Reply all
Reply to author
Forward
0 new messages