从JAR运行应用程序时,URI不是分层异常 [英] URI is not hierarchical exception when running application from JAR

查看:262
本文介绍了从JAR运行应用程序时,URI不是分层异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的Java控制台应用程序导出到Jar文件,但是当我运行jar并调用在JSON文件中解析的代码时,我得到一个 java.lang.IllegalArgumentException

I've exported my Java console application to a Jar file, but when I run the jar and call code that parses in a JSON file I get a java.lang.IllegalArgumentException

当我将程序作为JAR运行时,是否有人知道为什么会抛出异常?从Eclipse运行应用程序时,解析工作正常。

Does anyone know why the exception is being thrown when I run the program as a JAR? The parsing works fine when the application is run from Eclipse.

这是我执行jar文件并调用解析JSON文件的代码时输出的确切错误:

This is the exact error that is output when I execute the jar file and call the code that parses the JSON file:

Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierar
chical
        at java.io.File.<init>(Unknown Source)
        at gmit.GameParser.parse(GameParser.java:44)
        at gmit.Main.main(Main.java:28)

这是在我的GameParser类中解析的方式:

This is how the parsing is being done in my GameParser class:

public class GameParser {
    private static final String GAME_FILE = "/resources/game.json";
    private URL sourceURL = getClass().getResource(GAME_FILE); 
    private int locationId;

    private List<Location> locations;
    private List<Item> items;
    private List<Character> characters;

    public void parse() throws IOException, URISyntaxException {
        ObjectMapper mapper = new ObjectMapper();
       mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        try {                   
            // read from file, convert it to Location class
            Location loc = new Location();
            loc = mapper.readValue(new File(sourceURL.toURI()), Location.class);
            Item item = mapper.readValue(new File(sourceURL.toURI()), Item.class);
            GameCharacter character = mapper.readValue(new File(sourceURL.toURI()), GameCharacter.class);

            // display to console
            System.out.println(loc.toString());
            System.out.println(item.toString());
            System.out.println(character.toString());
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是文件夹结构我的项目:

This is the folder structure of my project:

推荐答案

调用 getClass()。getResource(GAME_FILE); 将返回URL相对到这个类。如果从JAR文件执行程序,它将返回指向JAR文件的URL。

The call getClass().getResource(GAME_FILE); will return a URL relative to this class. If you are executing your program from a JAR file, it will return a URL pointing to a JAR file.

java中的文件只能表示直接文件系统文件,不是zip / jar档案中的那些。

要修复它:


  1. 尝试使用 getClass()。getResourceAsStream()并使用它代替文件 s或

  2. 将文件解压缩到某个目录并使用文件,就像你现在尝试的一样。

  1. Try to use getClass().getResourceAsStream() and use that instead of Files or
  2. extract the files into some directory and use File in the same way as you are trying now.

这篇关于从JAR运行应用程序时,URI不是分层异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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