You are on page 1of 12

HOW TO CALL JAVA API

FROM .NET
Prepared By :Bar nalz
Date
: 03/07/2012

Innovile Software Team


April 2010
Intranet Platform of Tools
This document provides the introductory information about Intranet Platform.

www.innovile.com

Disclaimer: This document is furnished by Innovile Telecommunications Ltd. without any claims,
warranties or indemnifications.
Improvements and changes in this document due to typographical errors, inaccuracy in current
information, or improvements to programs and/or equipment may be made by Innovile
Telecommunications Ltd. at any time without any notice(s). These changes will, however, be
incorporated into new revisions of this manual.
No part of this publication may be reproduced, transmitted, stored in a retrieval system, nor translated
into any human or computer language, in any form or by any means, electronic, mechanical, magnetic,
optical, manual or otherwise, without the prior written permission of the copyrighted owner, Innovile
Telecommunications Ltd.

All other trademarks are the property of their respective holders.


Copyright 2010 Innovile Telecommunications Ltd.
All rights reserved.

Form ID: INN-F-002-REV00_08.10.2010

Company Information

Support Contacts

Innovile Telecommunications Ltd.

E-Mail : support@innovile.com

Beyazevler 545 Sokak. No: 34

Phone : 90-232-2820265

35410 Gaziemir-Izmir Turkey

Fax

: 90-232-2820109

Web

: www.innovile.com

Form ID: INN-F-002-REV00_08.10.2010

IKVM.NET Bytecode Compiler (ikvmc.exe)


The ikvmc tool converts Java bytecode to .NET dll's and exe's.
Usage
ikvmc [ options ] classOrJarfile [ classOrJarfile ... ]
Options
See below.
classOrJarfile
Name of a Java .class or .jar file. May contain wildcards (*.class).

Options
Option

Description

-out:outputfile

Specifies the name of the output file. Should have a .dll


extension (if -target is library) or an .exe extension (if -target is
exe or winexe). In most cases, if you omit this option, ikvmc
will choose an output name based on the -target and the name of
the input files. However, if you specify input files using
wildcards, you must use this option to specify the output file.

-assembly:assembly-name

Specifies the name of the generated assembly. If omitted, the


assembly name is (usually) the output filename.
Specifies whether to generate an .exe or .dll. target-type is one
of

-target:target-type

exe - generates an executable that runs in a Windows


command window
winexe - generates an .exe for GUI applications
library - generates a .dll
module - generates a .netmodule

On Linux, there is no difference between exe and winexe.


-keyfile:keyfilename

Uses keyfilename to sign the resulting assembly.

-version:M.m.b.r

Specifies the assembly version.

-main:classname

Specifies the name of the class containing the main method. If


omitted and the -target is exe or winexe, ikvmc searches for a
qualifying main method and reports if it finds one.

-reference:library-filespec

If your Java code uses .NET API's, specify the dll's using this
option. This option can appear more than once if more than one
library is referenced. Wildcards are permitted (e.g. c:\libs\*.dll).
Form ID: INN-F-002-REV00_08.10.2010

-recurse:filespec

Processes all files matching filespec in and under the directory


specified by filespec. Example: -recurse:*.class

-nojni

Do not generate JNI stub for native methods

-resource:name=path

Includes path as a Java resource named name

-exclude:filename

filename is a file containing a list of classes to exclude

-debug

Generates debugging information in the output. Note that this is


only helpful if the .class files contain debug information
(compiled with the javac -g option).

-srcpath:path

Specifies the location of source code. Use with -debug. The


package of the class is appended to the specified path to locate
the source code for the class.

-Xtrace:name

Displays all tracepoints with name

Builds method trace into the specified output method.


Xmethodtrace:methodname
Notes
The ikvmc tool generates .NET assemblies from Java class files and jar files. It converts the
Java bytecodes in the input files to .NET CIL. Use it to produce

.NET executables (-target:exe or -target:winexe)


.NET libraries (-target:library)
.NET modules (-target:module)

Java applications often consist of a collection of jar files. ikvmc can process several input jar
files (and class files) and produce a single .NET executable or library. For example, an
application consisting of main.jar, lib1.jar, and lib2.jar can be converted to a single main.exe.
When processing multiple input jar files that contain duplicate classes / resources, ikvmc will
use the first class / resource it encounters, and ignore duplicates encountered in jars that
appear later on the command line. It will produce a warning in this case. Thus, order of jar
files can be significant.

When converting a Java application with ikvmc, for best results, list the jars on the
ikvmc command line in the same order that they appear in the Java application's classpath.

Form ID: INN-F-002-REV00_08.10.2010

Example EnumParser

1)

Firstly,Run the following command from console;

ikvmc.exe ENMPRSMERGED.jar -target:library -out:ENMPRSMERGED.dll


then add ENMPRSMERGED.dll into reference in the Project.

static void ThreadGroup1()


{
ParserInfo pr = new ParserInfo();
pr.setBackupCompressionFlag(false);
pr.setInputDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS\\input");
pr.setOutputDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS\\output
");
pr.setBackupDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS\\backup
");
pr.setTemplateDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS\\conf
");
pr.setLogDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS\\log");
pr.setTempDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS\\temp");
pr.setErrorDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS\\error");
pr.setDelimeter(",");
pr.setPlatform("W");
pr.setParserVersion("MSC");
pr.setInstanceId(1);
pr.setLowerLimit(0);
pr.setUpperLimit(1000000);
pr.setType("ENUM");
pr.setDataInterval(15);
pr.setMaxThreads(10);
pr.setLogLevel(1);
pr.setBackupAfterParse(true);
pr.setInputFileMask("*");
pr.setOutputHasHeader(true);
pr.setFilenameAsColumn(false);
Console.WriteLine(pr);
AssignJob EnumParser = new AssignJob(pr);
// EnumParser.PrintConsole();
if (EnumParser.checkMaskValidity())
{
if (EnumParser.setValidFileList())
{
EnumParser.run();
}
else
{
Form ID: INN-F-002-REV00_08.10.2010

Console.WriteLine("ENUM Files could not be loaded on account of


fetching file list");
}
}
else
{
Console.WriteLine("ENUM Files could not be loaded on account of
validation of File Mask");
}

}
static void ThreadGroup2()
{
ParserInfo pr = new ParserInfo();
pr.setBackupCompressionFlag(false);
pr.setInputDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS2\\input");
pr.setOutputDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS2\\output");
pr.setBackupDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS2\\backup");
pr.setTemplateDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS2\\conf");
pr.setLogDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS2\\log");
pr.setTempDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS2\\temp");
pr.setErrorDirectory("C:\\Users\\nnobarisi\\Desktop\\ENUMINPUTS2\\error");
pr.setDelimeter(",");
pr.setPlatform("W");
pr.setParserVersion("MSC");
pr.setInstanceId(1);
pr.setLowerLimit(0);
pr.setUpperLimit(1000000);
pr.setType("ENUM");
pr.setDataInterval(15);
pr.setMaxThreads(10);
pr.setLogLevel(1);
pr.setBackupAfterParse(true);
pr.setInputFileMask("*");
pr.setOutputHasHeader(true);
pr.setFilenameAsColumn(false);

Console.WriteLine(pr);
AssignJob EnumParser = new AssignJob(pr);
//EnumParser.PrintConsole();
if (EnumParser.checkMaskValidity())
{
if (EnumParser.setValidFileList())
{
EnumParser.run();
}
else
{

Form ID: INN-F-002-REV00_08.10.2010

Console.WriteLine("ENUM Files could not be loaded on account of


fetching file list");
//AssignJob.sendEvent("ENUM Files could not be loaded on account
of fetching file list", Definitions.ERROR, 10161);
}
}
else
{
Console.WriteLine("ENUM Files could not be loaded on account of
validation of File Mask");
}

new Thread(new ThreadStart(ThreadGroup1)).Start();


new Thread(new ThreadStart(ThreadGroup2)).Start();

Form ID: INN-F-002-REV00_08.10.2010

jar2ikvmc
Summary
The tool helps converting big bundles of Java jar files to .NET dlls using IKVM.NET.
Jar2ikvmc uses JarAnalyser to detect dependencies between jar files and then generates command-line
script for ikvmc.exe.
Example
Here is sample results of program running using jar files for JFreeChart:
Generated Script
ikvmc swtgraphics2d.jar -target:library
ikvmc servlet.jar -target:library
ikvmc junit.jar -target:library
ikvmc jcommon-1.0.10.jar -target:library
ikvmc gnujaxp.jar -target:library
ikvmc jfreechart-1.0.6.jar -target:library -r:jcommon-1.0.10.dll -r:servlet.dll r:gnujaxp.dll
ikvmc jfreechart-1.0.6-swt.jar -target:library -r:jfreechart-1.0.6.dll -r:jcommon1.0.10.dll
ikvmc jfreechart-1.0.6-experimental.jar -target:library -r:jfreechart-1.0.6.dll r:jcommon-1.0.10.dll
ikvmc itext-2.0.2.jar -target:library -r:gnujaxp.dll

Dependencies

Other Third Party Bridge Tools


jni4net
bridge between Java and .NET (intraprocess, fast, object oriented, open-source)

Example:
.NET/C# to Java
// create bridge, with default setup
// it will lookup jni4net.j.jar next to jni4net.n.dll
Bridge.CreateJVM(new BridgeSetup(){Verbose=true});
// here you go!
java.lang.System.@out.println("Hello Java world!");
// OK, simple hello is boring, let's play with Java properties
// they are Hashtable realy
Properties javaSystemProperties = java.lang.System.getProperties();
// let's enumerate all keys.
// We use Adapt helper to convert enumeration from java o .NET
foreach (java.lang.String key in
Adapt.Enumeration(javaSystemProperties.keys()))
{
java.lang.System.@out.print(key);

Form ID: INN-F-002-REV00_08.10.2010

// this is automatic conversion of CLR string to java.lang.String


java.lang.System.@out.print(" : ");
// we use the hashtable
Object value = javaSystemProperties.get(key);
// and this is CLR ToString() redirected to Java toString() method
string valueToString = value.ToString();
java.lang.System.@out.println(valueToString);
}
// Java output is really Stream
PrintStream stream = java.lang.System.@out;
// it implements java.io.Flushable interface
Flushable flushable = stream;
flushable.flush();
}

Java to .NET

// it will lookup jni4net.n.dll next to jni4net.j.jar


Bridge.setVerbose(true);
Bridge.init();
// here you go!
Console.WriteLine("Hello .NET world!\n");

// OK, simple hello is boring, let's play with


System.Environment
// they are Hashtable realy
final IDictionary variables =
system.Environment.GetEnvironmentVariables();
// let's enumerate all keys
final IEnumerator keys = variables.getKeys().GetEnumerator();
while (keys.MoveNext()) {
// there hash table is not generic and returns
system.Object
// but we know is should be system.String, so we could
cast
final system.String key = (system.String)
keys.getCurrent();
Console.Write(key);
// this is automatic conversion of JVM string to
system.String
Console.Write(" : ");
// we use the hashtable
Object value = variables.getItem(key);
// and this is JVM toString() redirected to CLR
ToString() method
String valueToString = value.toString();
Console.WriteLine(valueToString);
}
// Console output is really TextWriter on stream
final TextWriter writer = Console.getOut();
writer.Flush();
Form ID: INN-F-002-REV00_08.10.2010

Form ID: INN-F-002-REV00_08.10.2010

You might also like