【Javac编译异常】javac编译提示jdk中的包找不到的问题error: package jdk.internal.org.objectweb.asm does not exist 和 error: cannot find symbol
一、复现步骤
1)编写待编译的java类
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 { //访问类的方法和字段 public static void main(String[] args) { byte[] bytes = getBytes(); // MyMain.class 文件的字节数组 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)在当前目录使用javac编译
javac -d . AsmDemo.java
提示如下错误:
➜ 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
二、解决办法
这就是javac的限制。默认情况下,javac不会从rt.jar中读取类。它从一个符号文件中读取,该文件只包含标准API和一些内部API(例如com.sun.,com.oracle.和sun . *)。
要禁用此机制,可以使用 javac -XDignore.symbol.file=true
使用maven可以用:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgument>-XDignore.symbol.file</compilerArgument> </configuration> </plugin>
上述问题采用下面命令执行就可以成功了:
javac -XDignore.symbol.file=true -d . AsmDemo.java
待包名的类,需要使用 -d .,表示在当前目录自动生成包路径
你投入得越多,就能得到越多得价值