Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Seems that you are trying to compile Java source file into a class file. That can be done with the Java compiler - the <a href="http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html" rel="nofollow">javac</a> command line program or the tools in the <a href="http://docs.oracle.com/javase/6/docs/api/javax/tools/package-summary.html" rel="nofollow">javax.tools package</a>.</p> <p>ASM is for a different purpose. ASM can be used to create class files on-the-fly, without any source code. Read <a href="http://asm.ow2.org/" rel="nofollow">ASM's documentation</a> to learn about Java bytecode and how to produce and read it with ASM.</p> <p>Here is how a file is compiled using javax.tools package. Or then you could invoke the command line tools using <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html" rel="nofollow">Process</a>. Check the documentation for additional arguments - what classpath to use, where to write the files etc.</p> <pre><code>import javax.tools.JavaCompiler; import javax.tools.ToolProvider; public class TestingCompile { public static void main(String[] args) { JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); int result = javac.run(null, null, null, "C:\\path\\to\\Testing.java"); if (result != 0) { throw new RuntimeException("compile failed: exit " + result); } } } </code></pre> <p>Here is how to create the same class file using ASM, without using the source file. I'm quite sure this is not what you want to be doing - otherwise you wouldn't have had to ask the question. ;)</p> <p>This is just the output of <a href="http://asm.ow2.org/asm33/javadoc/user/org/objectweb/asm/util/ASMifierClassVisitor.html" rel="nofollow">ASMifierClassVisitor</a>, so the bytes would still need to be written to a file or loaded dynamically into the class loader. I used the <code>-debug</code> argument so that ASMifier would show also the source file name and line numbers (the <code>visitSource</code>, <code>visitLineNumber</code> and <code>visitLocalVariable</code> calls are optional, so you could omit them and the related labels if the debug information is not needed).</p> <pre><code>import org.objectweb.asm.*; public class TestingDump implements Opcodes { public static byte[] dump() throws Exception { ClassWriter cw = new ClassWriter(0); FieldVisitor fv; MethodVisitor mv; AnnotationVisitor av0; cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Testing", null, "java/lang/Object", null); cw.visitSource("Testing.java", null); { mv = cw.visitMethod(ACC_PUBLIC, "&lt;init&gt;", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(1, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "&lt;init&gt;", "()V"); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "LTesting;", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(3, l0); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Works!"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLineNumber(4, l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l2, 0); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload