In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to view java class under jar package", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how to view java classes under jar package"!
View java classes in that jar package
java adds classpath
linux example: add-classpath xxx/xxx.jar:xxx/xxx.jar if windows; split if mac and linux: split
Java compilation
javac -d . xxx.java dot represents the current directory if not registered can not add-d . Example javac -d . Test.java
run
java com.dxz.chama.javaagent.asm.Test If you add class resources run java -classpath xxx/xxx.jar:xxx/xxx.jar com.dxz.chama.javaagent.asm.Testpackage com.dxz.chama.javaagent.asm;import java. net.URL;/** * description: Find a class under that jar package * * @author: he QQ: 905845006 * @email: 905845006@qq.com * @date: 2020/8/15 5:39 PM */public class Test { public static void main(String args[]) { Class targetclass = String.class; String className = targetclass.getName(); className= "sun.net.www.protocol.http.HttpURLConnection"; className = className.replace('. ', '/'); String resource = "/" + className + ".class"; URL url = targetclass.getResource(resource); System.out.println(url.getFile()); //Output result: // file:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/rt.jar!/ sun/net/www/protocol/http/HttpURLConnection.class }} View java class package com.dxz.chama.javaagent.asm under jar package;/** * description: * * @author: he QQ: 905845006 * @email: 905845006@qq.com * @date: 2020/8/15 3:11 PM */import java.io.File;import java.io.IOException;import java.lang.reflect.Method;import java.net.URL;import java.net.URLClassLoader;import java.util.Enumeration;import java.util.jar.JarEntry;import java.util.jar.JarFile;public class jarTest { public static void getJarName(String jarFile) throws Exception { getJarName(jarFile,null); } public static void getJarName(String jarFile, String clazz) throws Exception { try { //Create a new File instance by converting a given pathname string to an abstract pathname File f = new File(jarFile); URL url1 = f.toURI().toURL(); URLClassLoader myClassLoader = new URLClassLoader(new URL[]{url1}, Thread.currentThread().getContextClassLoader()); //Get all classes through jarFile and JarEntry JarFile jar = new JarFile(jarFile); //Return an enumeration of zip file entries Enumeration enumFiles = jar.entries(); JarEntry entry; System.out.println(enumFiles); //Test whether this enumeration contains more elements while (enumFiles.hasMoreElements()) { entry = (JarEntry) enumFiles.nextElement(); if (entry.getName().indexOf("META-INF")
< 0) { String classFullName = entry.getName(); if (!classFullName.endsWith(".class")) { classFullName = classFullName.substring(0, classFullName.length() - 1); } else { //去掉后缀.class String className = classFullName.substring(0, classFullName.length() - 6).replace("/", "."); if (className.equals("module-info")) { System.out.println("is jdk9 continue"); continue; } //如果传入指定类 只查看指定类 if (clazz != null && !className.equals(clazz)) { continue; } System.out.println("className:"+className); Class myclass = myClassLoader.loadClass(className); //打印类名 System.out.println("*****************************"); System.out.println("全类名:" + className); //得到类中包含的属性 Method[] methods = myclass.getMethods(); for (Method method : methods) { String methodName = method.getName(); System.out.println("方法名称:" + methodName); Class[] parameterTypes = method.getParameterTypes(); for (Class clas : parameterTypes) { // String parameterName = clas.getName(); String parameterName = clas.getSimpleName(); System.out.println("参数类型:" + parameterName); } System.out.println("=========================="); } } } } } catch (IOException e) { e.printStackTrace(); } } /** * 这些默认方法不打印 */ private static String DEFAULT_METHOD = "waitequalsnotifynotifyAlltoStringhashCodegetClass"; //必须传1个参数,指定的jar包,如果传2个参数,第一个为jar包,第二个为需要找的类 public static void main(String[] args) throws Exception { //jar包所在路径 /*getJarName("F:\\user.jar"); getJarName("F:\\role1.jar"); getJarName("F:\\role2.jar"); */ //getJarName("F:\\UserInfo.jar"); String jarz = null; String clazz = null; if(args.length>1){ jarz = args[0]; clazz = args[1]; }else{ jarz = args[0]; } if (jarz != null) { getJarName(jarz); } //org.apache.tools.ant.util.ResourceUtils$ResourceSelectorProvider if (jarz != null && clazz != null) { System.out.println("Sample class format: org.apache.tools.ant.util.ResourceUtils$ResourceSelectorProvider"); getJarName(jarz, clazz); } //Testing// getJarName("/Users/heliming/IdeaProjects/bugokhttp/asm6/target/asm6-1.0-SNAPSHOT.jar"); }}
Additional:
ClassNotFoundException explanation:
ClassNotFoundException is thrown when an application tries to load a Class file using the class loader, if the specified class is not found in the classpath. In general, when we load a class at runtime using Class.forName() or ClassLoader.loadClass and ClassLoader.findSystemClass(), the JVM throws a ClassNotFoundException if the class is not found.
NoClassDefFoundError explains:
When the JVM loads a class, if the class is available at compile time, but the class definition cannot be found at runtime, the JVM throws a NoClassDefFoundError error. For example, when we are running an instance of a new class, if the class is not found, we will throw a NoClassDefFoundError error. At this point, I believe everyone has a deeper understanding of "how to view java classes under jar packages", so you may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.