java classLoader.getResources(path) 在jar包内运行 file:/D:/File/myself/project/gitee/selenium-demo/target/selenium-demo-1.0-SNAPSHOT.jar!/com/qjc/tset 为什么会有感叹号
时间: 2023-07-12 18:04:52 浏览: 143
在jar包内运行java classLoader.getResources(path)方法时,如果path指定的资源文件位于jar包中,那么返回的URL会包含一个感叹号(!)。感叹号的作用是将URL分成两部分,前半部分是jar包的路径,后半部分是资源文件的路径。例如,在你的jar包中,路径为`/com/qjc/test`的资源文件的URL可能是这样的:`file:/D:/File/myself/project/gitee/selenium-demo/target/selenium-demo-1.0-SNAPSHOT.jar!/com/qjc/test`。其中,`file:/D:/File/myself/project/gitee/selenium-demo/target/selenium-demo-1.0-SNAPSHOT.jar`是jar包的路径,`/com/qjc/test`是资源文件的路径。这种URL的格式也被称为"jar URL"。我们可以通过Java的JarURLConnection类来处理这种URL,获取jar包中的资源文件。
相关问题
/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=63141:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/cat.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/crs-agent.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/jre/lib/rt.jar:/Users/shenglei/Library/Mobile Documents/com~apple~CloudDocs/💻Code/🍀/i/out/production/i Task1 Exception in thread "main" java.lang.IllegalArgumentException: Error decoding percent encoded characters at sun.net.www.ParseUtil.decode(ParseUtil.java:216) at sun.misc.URLClassPath$FileLoader.<init>(URLClassPath.java:1366) at sun.misc.URLClassPath$3.run(URLClassPath.java:574) at sun.misc.URLClassPath$3.run(URLClassPath.java:569) at java.security.AccessController.doPrivileged(Native Method) at sun.misc.URLClassPath.getLoader(URLClassPath.java:568) at sun.misc.URLClassPath.getLoader(URLClassPath.java:533) at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:498) at sun.misc.URLClassPath.getResource(URLClassPath.java:252) at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:363) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:362) at java.lang.ClassLoader.loadClass(ClassLoader.java:419) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) at java.lang.ClassLoader.loadClass(ClassLoader.java:352) at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304) at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401) FATAL ERROR in native method: processing of -javaagent failed 进程已结束,退出代码134 (interrupted by signal 6: SIGABRT)
这段代码是Java程序的运行日志,看起来是在运行时出现了错误。具体来说,它报告了一个IllegalArgumentException异常,这可能是由于URL中使用了不正确的百分号编码字符而引起的。最后,它还显示了一个FATAL ERROR,说明Java代理程序处理失败。这个程序可能是使用了IntelliJ IDEA开发的。
[root@localhost jx]# java -cp haikongweishi-1.0-SNAPSHOT.jar:/home/jx/libs client.LinuxClientDemo Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Library at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:756) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:468) at java.net.URLClassLoader.access$100(URLClassLoader.java:74) at java.net.URLClassLoader$1.run(URLClassLoader.java:369) at java.net.URLClassLoader$1.run(URLClassLoader.java:363) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:362) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at client.LinuxClientDemo.<clinit>(LinuxClientDemo.java:8) Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
这是一个 Java 命令启动的客户端应用程序,但在运行过程中出现了异常。异常信息显示缺少 com.sun.jna.Library 这个类。这个类是 JNA(Java Native Access)库的一部分,而该应用程序依赖于该库文件。因此,需要将 JNA 库文件添加到类路径中,可以尝试以下命令:
```
java -cp haikongweishi-1.0-SNAPSHOT.jar:/home/jx/libs:/path/to/jna.jar client.LinuxClientDemo
```
其中,/path/to/jna.jar 是 JNA 库文件所在的路径。添加 JNA 库文件后,再次运行该命令即可。
阅读全文