1. Steps to reproduce
1) Write the java class to be compiled
package f_asm_and_javassist; import jdk. internal .org.objectweb.asm.* ; import java.io. * ; import static jdk. internal .org.objectweb.asm.Opcodes.ASM5; /* * * @Author zhangboqing * @Date 2020/3/26 */ public class AsmDemo { // Methods and fields to access the class public static void main(String[] args) { byte [] bytes = getBytes(); // Byte array of the MyMain.class file ClassReader cr = new ClassReader(bytes); ClassWriter cw = new ClassWriter( 0 ); ClassVisitor cv = new ClassVisitor(ASM5, cw) { @Override public FieldVisitor visitField( int access, String name, String desc, String signature, Object value) { System. out .println( " field: " + name); return super.visitField(access, name, desc, signature, value); } @Override public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions) { System. out .println( " method: " + name); return super.visitMethod(access, name, desc, signature, exceptions); } }; cr.accept(cv, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); } private static byte [] getBytes() { StringBuilder sb = new StringBuilder(); try (FileInputStream fileInputStream = new FileInputStream( new File( " MyMain.class " )); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) { byte [] buffer = new byte [ 1024 * 8 ]; while (bufferedInputStream.available()> 0 ) { int length = bufferedInputStream.read(buffer); sb.append( new String(buffer, 0 ,length) ); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sb.toString().getBytes(); } }
2) Use javac to compile in the current directory
javac -d. AsmDemo.java
Prompt the following error:
➜ f_asm_and_javassist git:(master) ✗ javac- d. AsmDemo.java AsmDemo.java: 3 : error: package jdk. internal .org.objectweb.asm does not exist import jdk. internal .org.objectweb.asm.* ; ^ AsmDemo.java: 7 : error: package jdk. internal .org.objectweb.asm does not exist import static jdk. internal .org.objectweb.asm.Opcodes.ASM5; ^ AsmDemo.java: 7 : error: static import only from classes and interfaces import static jdk. internal .org.objectweb.asm.Opcodes.ASM5; ^ AsmDemo.java: 18 : error: cannot find symbol ClassReader cr = new ClassReader(bytes); ^ symbol: class ClassReader location: class AsmDemo AsmDemo.java: 18 : error: cannot find symbol ClassReader cr = new ClassReader(bytes); ^ symbol: class ClassReader location: class AsmDemo AsmDemo.java: 19 : error: cannot find symbol ClassWriter cw = new ClassWriter( 0 ); ^ symbol: class ClassWriter location: class AsmDemo AsmDemo.java: 19 : error: cannot find symbol ClassWriter cw = new ClassWriter( 0 ); ^ symbol: class ClassWriter location: class AsmDemo AsmDemo.java: 20 : error: cannot find symbol ClassVisitor cv = new ClassVisitor(ASM5, cw) { ^ symbol: class ClassVisitor location: class AsmDemo AsmDemo.java: 20 : error: cannot find symbol ClassVisitor cv = new ClassVisitor(ASM5, cw) { ^ symbol: class ClassVisitor location: class AsmDemo AsmDemo.java: 20 : error: cannot find symbol ClassVisitor cv = new ClassVisitor(ASM5, cw) { ^ symbol: variable ASM5 location: class AsmDemo AsmDemo.java: 33 : error: cannot find symbol cr.accept(cv, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); ^ symbol: variable ClassReader location: class AsmDemo AsmDemo.java: 33 : error: cannot find symbol cr.accept(cv, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); ^ symbol: variable ClassReader location: class AsmDemo 12 errors
2. the solution
This is the limitation of javac. By default, javac will not read classes from rt.jar. It is read from a symbol file that contains only standard APIs and some internal APIs (such as com.sun., com.oracle. and sun. *).
To disable this mechanism, you can use javac -XDignore.symbol.file=true
With maven, you can use:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgument>-XDignore.symbol.file</compilerArgument> </configuration> </plugin>
The above problem can be successfully executed with the following command:
javac -XDignore.symbol.file=true -d. AsmDemo.java
For the class to be package name, you need to use -d., which means that the package path is automatically generated in the current directory
Read More:
- [Solved] Error: (3, 31) Java: package jdk.nashorn.internal.ir does not exist
- [Solved] Invalid Gradle JDK configuration found. Open Gradle Settings Change JDK location
- Maven error: Could not find artifact jdk.tools [How to Solve]
- JAVA Error: package org.apache.commons.codec.binary does not exist
- [Solved] Error:(3, 46) java: Program Package org.springframework.context.annotation does not exist
- [Solved] Error: (4, 52) Java: package org springframework. beans. factory. Annotation does not exist
- Java error: unable to find or load main class (package name in source file)
- Maven error: package org.springframework.context does not exist
- [Solved] The war package Error: The reason why the XSD file could not be found
- [Solved] spring source code compile error: target package does not exist
- Notes for migrating Oracle jdk8 to openjdk11
- [Solved] IDEA Add maven Project Error: Error:(3,21)java: Package javax.servletdoes not exist
- [Solved] Java Error: Must declare a named package because this compilation unit is associated to the named module
- [Solved] Could not find resource COM / atguigu / Dao / studentdao.xm, the mapper file for storing SQL statements could not be found and an error occurred
- Linux: How to Solve jdk11+easy poi Export Excel Error
- [Solved] JDK Install Error /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
- [Solved] Idea Build error. The symbol — Lombok cannot be found
- Error java JDK isn‘t specified for module [How to Solve]
- How to Solve JAR pack error: Error resolving template [/userInfo], template might not exist or might not be accessib
- [Solved] Error resolving template [x] template might not exist or might not be accessible by any of the con