java.lang.IllegalArgumentException:名称 [英] java.lang.IllegalArgumentException: name

查看:531
本文介绍了java.lang.IllegalArgumentException:名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在eclipse中工作正常,但是当我创建jar并运行它时,会出现这个异常.这是我正在使用的非Web Spring Boot应用程序,应该以独立jar的形式运行"

"it work fine in eclipse but when i create jar and run it will give me this exception.This is non web spring boot application i am using which i supposed to run as standalone jar"

java.lang.IllegalArgumentException: name
                    at sun.misc.URLClassPath$Loader.findResource(Unknown Source) ~[na:1.8.0_171]



at sun.misc.URLClassPath.findResource(Unknown Source) ~[na:1.8.0_171]
                    at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_171]
                    at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_171]
                    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_171]
                    at java.net.URLClassLoader.findResource(Unknown Source) ~[na:1.8.0_171]
                    at org.springframework.boot.loader.LaunchedURLClassLoader.findResource(LaunchedURLClassLoader.java:58) ~[extension-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
                    at java.lang.ClassLoader.getResource(Unknown Source) ~[na:1.8.0_171]
                    at java.net.URLClassLoader.getResourceAsStream(Unknown Source) ~[na:1.8.0_171]

推荐答案

这可能有点晚了,但是最近我在Windows上遇到了同样的错误,并用肮脏的hack修复了该错误.我希望将来寻找相同问题的人会发现这对他们的问题有用.

It might be a bit late but I encounted the same error recently on Windows and fixed it with a dirtly hack. I hope future people looking for the same question will find this useful for their problem.

首先,以不同的方式使"Eclipse"运行" Spring Boot应用程序.它不执行java -jar foo.jar,而是直接引用所有类和库,即java -classpath xxx/target/classes:a.jar:b.jar....因此,在eclipse中运行的soetime的行为会有所不同.

First of all, eclipse "run" your Spring Boot app in a different way. It does not do java -jar foo.jar but reference all classes and libraries directly i.e. java -classpath xxx/target/classes:a.jar:b.jar.... So soetimes running in eclipse will behave differently.

我想您遇到了与我相同的情况,即您拥有一个Spring Boot应用程序,并且希望将其打包为可执行jar,并且您正在引用外部依赖关系,并且无法修复这些外部依赖关系内部出错的地方.

I suppose you are running into the same situtation as I did which is you have a Spring Boot app and you want to package it as executable jar and you are referencing external dependencies and could not fix what went wrong inside those external dependencies.

我发现我正在使用的外部库中的某个地方正在生成一个临时文件,并使用Java的ClassLoader进行读取,但是Spring Boot的LaunchedURLClassLoader无法处理Windows的"x:\"语法

What I found out was that somewhere inside the external library I was using is generating a temporary file and reading it using Java's ClassLoader but Spring Boot's LaunchedURLClassLoader could not handle Windows's "x:\" syntax

就我而言,是

URL findResource(name): C:\foo\bar.xml

因此,解决方法是构建自己的LaunchedURLClassLoader并将其替换到可执行jar中.如果打开可执行罐的包装,您将在org\springframework\boot\loader下找到LaunchedURLClassLoader.class.这个想法是将C:\foo\bar.xml改成file:///C:/foo/bar.xml,这样Java ClassLoader就会知道如何处理该文件.

So the hack is to build your own LaunchedURLClassLoader and replace it in the executable jar. If you unpack the excutable jar, you will find LaunchedURLClassLoader.class under org\springframework\boot\loader. The idea is to reaplce C:\foo\bar.xml to file:///C:/foo/bar.xml so Java ClassLoader will know how to deal with the file.

...

package org.springframework.boot.loader;
...

public class LaunchedURLClassLoader extends URLClassLoader {

    ...

    @Override
    public URL findResource(String name) {

        // This is where I added the dirty hack
        if (name.contains(":\\") && !name.startsWith("file:///")) {
            System.out.println("URL findResource(name): " + name); // just for you to debug
            name = "file:///" + name.replace("\\", "/");
            System.out.println("URL findResource(name): Reaplced to " + name); // just for you to debug
        }

        Handler.setUseFastConnectionExceptions(true);
        try {
            return super.findResource(name);
        }
        finally {
            Handler.setUseFastConnectionExceptions(false);
        }
    }
    ....
}

这篇关于java.lang.IllegalArgumentException:名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆