• IKVM.NET_06_用户指南_教程


    本文内容

    • 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 文件。如果找到了,就装载,并动态地执行字节码。在命令提示符下输入你名字,会看到一个简短的问候。

    如果遇到问题,请按下面检查:

    1) 检查类名大小写是否正确。

    2) 若 ikvm 报告 ClassNotFoundException 异常,检查是否设置了 CLASSPATH 环境变量。如果已经设置了,清除 CLASSPATH,添加当前目录,这样,ikvm 就能找到当前目录中的类。

    你也可以执行在一个 jar 包的 Java 应用程序,命令如下:

    ikvm -jar hello.jar

    把一个 Java 应用程序转换成 .NET

    IKVM.NET 包含 ikvmc 工具,可以把 Java .jar 文件转换成 .NET 程序集(.dll)和(.exe )可执行程序。本节将把一个 Java 应用程序转换成一个 .NET 应用程序。

    看一下 IKVMROOT/samples/hello,命令如下:

    ikvmc hello.jar

    命令完成后,你会在当前目录中发现 hello.exe 文件。如果想执行它:

    1) Windows / .NET Framework环境

    试着运行 "hello.exe"。当 .NET 运行时尝试加载 IKVM.OpenJDK.ClassLibrary.dll,如果抛出 FileNotFound 异常,那是,.NET Framework 会在应用程序目录或是全局缓冲查找dll文件。因此,你要么把 dll 文件安装在全局缓冲区,要么放在应用程序目录。

    2) 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文件复制到当前目录。

    +++ 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.)

  • 相关阅读:
    postfix 邮件中继配置
    shell脚本网络流量实时查看
    zabbix配置邮件报警(第四篇)
    pptp服务故障
    Centos6.7 ELK日志系统部署
    rrdtool 实践
    Centos6.7安装Cacti教程
    Nagios事件机制实践
    Nrpe 插件安装教程
    如何查找一个命令由哪个rpm安装&&rpm 的相关查询方法
  • 原文地址:https://www.cnblogs.com/liuning8023/p/2155810.html
Copyright © 2020-2023  润新知