我正在处理一个项目,该项目要求我在go上生成一个java ".class“文件,以后可以在JVM上编译该文件。在学习并使用了Microsoft IL(也是一种基于堆栈的中间编程语言)之后,以下是我面临的问题:
我引用过“Joshua为Java™虚拟机编程”,但它并没有达到我的目的,因为我已经了解到了™指令集。
有人能帮我吗??所有的帮助都将受到高度的感谢。生成一个简单的类文件的示例将非常有用,因为我还无法找到一个1。
发布于 2009-11-25 18:18:49
使用转换为使用ASM字节码库与.NET一起使用IKVM Java到.NET编译器的示例
hello.cs:
using System;
using System.IO;
using org.objectweb.asm;
namespace test.helloWorld
{
public class helloDump
{
public static byte[] dump ()
{
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(Opcodes.__Fields.V1_6, Opcodes.__Fields.ACC_PUBLIC + Opcodes.__Fields.ACC_SUPER, "hello", null, "java/lang/Object", null);
mv = cw.visitMethod(Opcodes.__Fields.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.__Fields.ALOAD, 0);
mv.visitMethodInsn(Opcodes.__Fields.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(Opcodes.__Fields.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.__Fields.ACC_PUBLIC + Opcodes.__Fields.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
mv.visitCode();
mv.visitFieldInsn(Opcodes.__Fields.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("Hello World!");
mv.visitMethodInsn(Opcodes.__Fields.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitInsn(Opcodes.__Fields.RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
public static void Main(string[] args)
{
FileStream helloWorldFile = new FileStream("hello.class", FileMode.Create);
byte[] helloWorldClass = dump();
helloWorldFile.Seek(0, SeekOrigin.Begin);
helloWorldFile.Write(helloWorldClass, 0, helloWorldClass.Length);
}
}
}命令:
$ ikvmc -out:org.objectweb.asm.dll -target:library -version:3.2.0.0 asm-3.2.jar
$ mcs -r:org.objectweb.asm.dll hello.cs
$ mono hello.exe
$ ls hello.class
$ java hello发布于 2009-11-25 16:19:41
您可能需要查看ASM字节码库。很受欢迎。很多JVM语言都使用它: Clojure、Groovy、Jython、JRuby。
但是,我确实同意其他后置器的观点,即生成javac源代码并用.class编译它以获得.class文件可能更简单。这在很大程度上取决于你需要做什么。
发布于 2009-11-25 16:12:02
你看过BCEL了吗?
图书馆
现在和JRE捆绑在一起了。
https://stackoverflow.com/questions/1797996
复制相似问题