您的位置:首页 > 其它

IKVM.NET_第六篇_用户指南_教程

2010-07-04 15:52 204 查看
本文内容如下:
1) 配置环境
2) 动态运行Java应用程序
3) 把Java应用程序转换成.NET
4) 在Java中开发.NET应用程序
+++ 配置环境
这个文档包含如何在Windows平台和Linux平台上进行配置的方法。假设Windows用户使用.NET SDK,Linux用户使用Mono SDK。在配置之前,将下面的路径添加到系统环境变量PATH里,如下:
1) 包含IKVM可执行程序的目录路径;
2) 包含C#编译器的目录路径(Windows是csc,Mono是mcs)。Windows平台上它在C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322;
3) 包含Java编译器(javac 或 jikes)的目录路径。
+++动态运行Java应用程序
IKVM.NET包含一个用C#实现的Java虚拟机。看一下IKVMROOT/samples/hello这个目录,编译里边的例子,命令如下:
javac Hello jar cfm hello.jar manifest.mf Hello.class
现在使用IKVM虚拟机运行该应用程序,命令如下:
ikvm Hello
首先虚拟机查找Hello.class文件。如果找到,就装载它并动态执行字节码。
If you experience problems, check the following:
如果出现问题,请按下面检查:
1) 检查类名大小写是否正确;
2) 若ikvm报告ClassNotFoundException异常,检查是否设置了CLASSPATH环境变量。如果已经设置了,清除重设一下,增加当前目录到CLASSPATH,以便ikvm就能找到当前目录中的类。
You can also execute Java applications in a jar file. Try it out:
你也可以在一个jar包中,执行Java应用程序,命令如下:
ikvm -jar hello.jar
+++把Java应用程序转换成.NET
IKVM.NET includes ikvmc, a utility that converts Java .jar files to .NET .dll libraries and .exe applications. In this section, you'll convert a Java application to a .NET .exe.
IKVM.NET包含一个ikvmc.exe的应用程序,它是把Java .jar包转换成.NET .dll库或.exe应用程序的工具。在这小节中,介绍如何进行这种转换。看一下IKVMROOT/samples/hello,命令如下:
ikvmc hello.jar
命令完成后,你会在当前目录中发现hello.exe文件。在Windows / .NET Framework环境,执行“hello.exe”;在Linux / Mono环境,执行“mono hello.exe”。
+++在Java中开发.NET应用程序
在这小节中,你将学习如何在Java中开发.NET应用程序。
看一下例子IKVMROOT/samples/usenetapi/ShowDir.java。这是一个用.NET API写的Java应用程序,用来显示当前目录的文件列表。注意:代码顶部的imports语句——包名是以“cli.*”开始的。在Java API中是没有包的,而是“pseudo”包packages,映射到.NET命名空间。
++ 步骤1:创建Java存根stubs
IKVM不包含Java编译器,因此,使用Java编译器编译ShowDir.jar。因为,Java编译器只能使用Java API,而不是.NET API编译应用程序,因此,我们必须骗过Java编译器,让它相信的确有一个名为“cli.System.IO”的Java包。ikvmstub应用程序就能帮助我们做到这点,它能从.NET DLL文件生成Java jar文件。由ikvmstub生成的jar文件包含与.NET类对应的Java类和接口,但是不包含任何实际代码。它们包含只是满足Java编译器,检查Java应用程序。命令如下:
ikvmstub mscorlib.dll
注意:在Linux Mono,你必须输入全名,命令如下:
ikvmstub /usr/lib/mscorlib.dll
命令完成后,你会在当前目录中发现名为mscorlib.jar的jar文件。
++ 步骤2:编译Java代码
现在编译Java代码。如果你使用javac,命令如下:
javac -classpath mscorlib.jar ShowDir.java
命令完成后,你会在当前目录下发现ShowDir.class文件。
++ 步骤3:创建.NET可执行程序
现在把Java的class文件转换成.NET应用程序,命令如下:
ikvmc ShowDir.class
完成命令后,你会在当前目录下发现showdir.exe文件。在Windows .NET下,记得将IKVM dll文件复制到当前目录。

IKVM.NET Tutorial
1) Setup your Environment
2) Run a Java Application Dynamically
3) Convert a Java Application to .NET
4) Develop a .NET Application in Java
+++ Setup your Environment
This tutorial includes information for both the Windows and Linux platforms. It assumes that Windows users will be using the .NET SDK, and Linux users will be using the Mono SDK.
This tutorial references files in the samples distribution available on the download page. Before you begin, prepare your environment by adding the following to your PATH environment variable:
1) The directory containing the IKVM executables
2) The directory containing the C# compiler (Windows: csc / Mono: mcs). On Windows, this is typically C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322.
3) The directory containing a Java compiler (javac or jikes)
+++ Run a Java Application Dynamically
IKVM.NET includes a Java Virtual Machine implented in C#. To try it out, navigate to IKVMROOT/samples/hello and compile the sample application:
javac Hello jar cfm hello.jar manifest.mf Hello.class
Now, to run the application using the IKVM Virtual Machine, enter the following:
ikvm Hello
This command starts the virtual machine, which searches for a file named Hello.class. When it finds it, it loads it and executes the bytecodes dynamically. You should be prompted to enter your name, and see a brief greeting.
If you experience problems, check the following:
1) Check your capitalization: ikvm, like java, requires you to capitalize class names correctly.
2) If ikvm reports a ClassNotFoundException, check whether the CLASSPATH environment variable is set. If so, try clearing the CLASSPATH or adding the current directory to it so ikvm can find the class in the current directory.
You can also execute Java applications in a jar file. Try it out:
ikvm -jar hello.jar
+++ Convert a Java Application to .NET
IKVM.NET includes ikvmc, a utility that converts Java .jar files to .NET .dll libraries and .exe applications. In this section, you'll convert a Java application to a .NET .exe.
Navigate to IKVMROOT/samples/hello and enter the following:
ikvmc hello.jar
After the command completes, you should find a hello.exe file in the current directory. To execute it:
1) Windows / .NET Framework:
Try running hello.exe. If you get a FileNotFound exception when the .NET runtime attempts to load the referenced IKVM.OpenJDK.ClassLibrary.dll, remember that the .NET Framework expects to find referenced dll's in the application directory or in the Global Assembly Cache. Either install the dll's in the Global Assembly Cache, or copy them to the application directory.
2) Linux / Mono:
Run it using the following command:
mono hello.exe
+++ Develop a .NET Application in Java
++ In this section, you will learn the steps needed to develop .NET applications in Java.
++ To begin, open a command window and navigate to IKVMROOT/samples/usenetapi. Take a look at ShowDir.java -- this is a Java application that uses the .NET API to display a list of files in the current directory. Notice the imports at the top -- the package names begin with cli.*. These are not packages in the Java API; rather, they are "pseudo" packages that map to .NET namespaces. For more information on this, see the Developer's Guide.
Step 1: Generate Java stubs
IKVM does not come with a Java compiler, so we will compile ShowDir using a standard Java compiler. Since Java compilers can only compile applications that use Java API's, not .NET API's, we have to fool the Java compiler into believing that there is really a Java package named cli.System.IO. The ikvmstub application helps us do this. It generates Java jar files from .NET dll's. The jar files generated by ikvmstub contain Java classes and interfaces that correspond to .NET classes, but don't contain any real code. They contain just enough to satisfy the Java compiler, and allow it to type check the Java application.
Type the following:
ikvmstub mscorlib.dll
Note: On a Linux Mono installation, you will have to type the full pathname to mscorlib.dll, like this:
ikvmstub /usr/lib/mscorlib.dll
After the command completes, you should find a file named mscorlib.jar in the current directory.
Step 2: Compile the Java source code
Now, we'll compile the Java source code. If you're using javac, type the following:
javac -classpath mscorlib.jar ShowDir.java
(Substitute jikes for javac if you're using that tool.)
After the command completes, you should find ShowDir.class in the current directory.
Step 3: Generate a .NET executable
Now, we'll convert the Java class file to a .NET application. Type the following:
ikvmc ShowDir.class
After the command completes, you should find ShowDir.exe in the current directory. You should be able to execute it successfully. (On Windows .NET, remember to copy the IKVM dll's to the current directory.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: