1

I am using ASM 5.2 for an example project and I want to compile the project which has as dependacy the ASM library.

The java file i am trying to compile is the following:

import java.io.FileInputStream;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;

import static org.objectweb.asm.Opcodes.ASM5;


public class Parser2 {

    public static void main(final String args[]) throws Exception {
        FileInputStream inputFile = new FileInputStream("/src/Test.class");
        ClassReader reader = new ClassReader(inputFile);

        GraphClass gc = new GraphClass();

        reader.accept(gc, 0);

    }
}

class GraphClass extends ClassVisitor {
    public GraphClass() {
        super(ASM5);
    }

    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        GraphMethod newVisitor = new GraphMethod(name);
        return newVisitor;
        //return super.visitMethod(access, name, desc, signature,exceptions);
    }
}

class GraphMethod extends MethodVisitor{
    public String MthName;

    public GraphMethod(String name) {
        super(ASM5);
        MthName = name;
    }

    public void visitMethodInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor, boolean isInterface) {
        System.out.println(owner + "." + MthName + " ===> " + owner + "." + name);

    }
}

I compile it with

javac -classpath asm-all-5.2.jar Parser2.java

which creates the class files GraphClass.class, GraphMethod.class and Parser2.class.

Then upon executing the Parser2 the following error comes up.

$ java Parser2                  
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        ... 7 more

Why does it say it cannot find the ClassVisitor?

java version in use openjdk version "1.8.0_252"

kampias
  • 459
  • 1
  • 10
  • 21
  • Does ASM have a runtime library requirement different to the compiler requirements? https://stackoverflow.com/questions/1798724/asm-jar-why-my-java-project-has-a-dependency-on-this - – JGFMK Aug 30 '20 at 09:45
  • no it shouldnt I believe. – kampias Aug 30 '20 at 09:50
  • How are you invoking your code? via java command, ant, maven etc? Are you including the asm.jar in your classpath? – JGFMK Aug 30 '20 at 09:53
  • May be related.. https://stackoverflow.com/questions/54245536/the-type-org-objectweb-asm-classvisitor-cannot-be-resolved-it-is-indirectly-ref – JGFMK Aug 30 '20 at 09:55
  • I just downloaded the jar you mentioned from mvncentral https://repo1.maven.org/maven2/org/ow2/asm/asm-all/5.2/asm-all-5.2.jar When I expanded the jar I can see the class in that version. See this image https://i.stack.imgur.com/CqOfp.png – JGFMK Aug 30 '20 at 10:02
  • @JGFMK Regarding how i am invoking the code, it is mentioned in the question. After compilation i simply run `java Parse2`. Also yes I know that the jar file contains the class. The question is why upon running the code it complains it cant find ClassVisitor? – kampias Aug 30 '20 at 10:44
  • https://stackoverflow.com/questions/219585/including-all-the-jars-in-a-directory-within-the-java-classpath `java -cp asm-all-5.2.jar Parser2` .. Something like that... – JGFMK Aug 30 '20 at 12:58
  • @JGFMK `javac -cp "asm-all-5.2.jar:*" Parser2.java` seems to produce the exact same error – kampias Aug 30 '20 at 13:03
  • Are you on Windows or Unix based OS? (Only It actually says drop the .jar in the link.. And semi-colon or colon depending on the OS) The dot would represent the current directory.. So if your jar is in a lib folder that would need to be factored in. I normally let the IDE take care of that stuff. ;-) – JGFMK Aug 30 '20 at 13:22
  • As well as the OS, tag the question with the java version you are using - just in case there are some nuances. – JGFMK Aug 30 '20 at 13:28
  • @JGFMK I am on Linux and everything seems correct and compilation does not show an error. – kampias Aug 30 '20 at 14:31
  • It would not be the compilation that is the cause of the issue. It's how you are using the java command to run the program where the issue lies. What folder does Parser2.class reside in relative to the jar? `java -cp asm-all-5.2.jar Parser2 ` should do it if class file and jar are co-located in same folder.. Otherwise . gets used for relative to current folder.. so if jar was in lib folder and class in current.. you'd do `java -cp ./lib/asm-all-5.2.jar Parser2` https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html#BABDJJFI – JGFMK Aug 30 '20 at 15:31
  • @JGFMK all files are in the same folder – kampias Aug 30 '20 at 15:32
  • Have you tried running it via an IDE and adding jar. Check out some of the Youtube videos using something like Spring Tool Suite. – JGFMK Aug 30 '20 at 15:34
  • this is in a headless server so no IDE there. – kampias Aug 30 '20 at 15:34
  • https://asm.ow2.io/versions.html Had wondered if version of asm was not compatible with Java version you had. Not sure. I'd see if they have a github repo or something like that where you could file the issue. (On surface of it supposed to work from Java 1.5+) In their guide, they talk about using the asm-util.jar https://asm.ow2.io/asm4-guide.pdf – JGFMK Aug 30 '20 at 15:47

0 Answers0