无法从绝对路径加载xml文件 [英] Unable to load xml file from a absolute path

查看:174
本文介绍了无法从绝对路径加载xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个maven项目A.一旦我做了mvn clean install,项目就会构建,并且jar可以在我的本地存储库中获得,即
c:\ storage \ comcom \stackoverflow \ A \ A .jar



A.jar包含这些类使用的几个类和xml文件



来自另一个maven项目我正在调用类中使用一个XML文件的方法之一。



代码是:

  public File xmlFilder(String xmlAbsolutePath){
File f = new File(xmlAbsolutePath);
返回f;
}

第二个项目位于我的D盘中。在获得绝对路径时,我得到 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b

但如果我在第3行添加以下代码,我得到如下:

  public File xmlFilder (String xmlAbsolutePath){

File f = new File(xmlAbsolutePath);

System.out.println(AbsolutePath ----+ f.getAbsolutePath()); ---> D:\ com\stackoverflow\A\res\p.xml

System.out.println(getPath ------------+ f .getPath()); ---> \ com; \\ stackoverflow \A\res\p.xml

System.out.println(exists --------------+ f .exists()); ---->假

返回f;

}

任何人都可以让我知道我哪里出错了。为什么不从C驱动器中存在的存储库中选择xml。

解决方案

如果A类(在你的A.jar中捆绑)想要从jar加载xml并尝试要以第一个代码示例中显示的方式访问它,那么它不会访问jar中的 p.xml 。它访问该位置的文件



如果是这种情况,很容易测试。创建jar后,从文件系统中临时删除 p.xml 并运行A类。它将无法加载它。



当一个类需要加载捆绑在jar中的资源时,它不会使用 File 但是类加载器。



根据您提供的名称,我假设您的A类在包中: com.stackoverflow。 A
xml文件位于软件包 com.stackoverflow.A.res



以下所有方法都将使用您的xml加载流

  InputStream is = null; 

//使用class - 相对于类的位置,因为path不以/开头
= SimpleWriter.class.getResourceAsStream(res / p.xml);

//使用class - 绝对路径,因为路径以/开头
= SimpleWriter.class.getResourceAsStream(/ com / stackoverflow / A / res / p.xml);

//使用classloader - path是* always * absolute。请注意,缺少前导/
= SimpleWriter.class.getClassLoader()。getResourceAsStream(com / stackoverflow / A / res / p.xml);


There is a maven project A. Once I do mvn clean install, the project builds and the jar is available at my local repository ie c:\repository\com\stackoverflow\A\A.jar

A.jar contains couple of classes and xml files used by these classes

From another maven project I am invoking one of the methods in the class which uses one XML file.

Code is:

public File xmlFilder(String xmlAbsolutePath) {
  File f = new File(xmlAbsolutePath);
  return f;
}

The second project is located in my D drive. On getting the absolute path, I am getting as \com\stackoverflow\A\res\p.xml which is the correct absolute path.

But If I add the following code at line 3 , I am getting as follows:

public File xmlFilder(String xmlAbsolutePath) {

  File f = new File(xmlAbsolutePath);

  System.out.println("AbsolutePath----"+f.getAbsolutePath());  ---> D:\com\stackoverflow\A\res\p.xml

  System.out.println("getPath------------"+f.getPath());       ---> \com\stackoverflow\A\res\p.xml 

  System.out.println("exists--------------"+f.exists());       ----> false

return f;

}

Can anyone please let me know where I am going wrong. Why it is not picking the xml from the repository which is present in C drive.

解决方案

If the A class (that is bundled in your A.jar) wants to load the xml from the jar and it tries to access it in the way shown in your first code example then it does not access p.xml inside your jar. It accesses the file at that location.

It is easy to test if this is your case. After your jar is created, temporarily delete p.xml from the file system and run A class. It will fail to load it.

When a class needs to load a resource that is bundled in a jar then it does not use File but the classloader.

From the names you provide, I assume that your class A is in package: com.stackoverflow.A The xml file is in the package com.stackoverflow.A.res

All the approaches below will load a stream with your xml

InputStream is = null;

// Using class - relative to the class location because path does not start with "/"
is = SimpleWriter.class.getResourceAsStream("res/p.xml");

// Using class - absolute path because path starts with "/"
is = SimpleWriter.class.getResourceAsStream("/com/stackoverflow/A/res/p.xml");

// Using classloader - path is *always* absolute. Note that leading "/" is missing
is = SimpleWriter.class.getClassLoader().getResourceAsStream("com/stackoverflow/A/res/p.xml");

这篇关于无法从绝对路径加载xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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