-1

My project has the following structure:

src/test/resources/utility/configuration.xml

I am trying to get the file location in the system using ClassLoader

public File getFileFromResources(String fileName){

        ClassLoader classLoader = getClass().getClassLoader();

        URL resource = classLoader.getResource(fileName);
        if(resource == null){
            throw new IllegalArgumentException("file not found");
        }else{
            return new File(resource.getFile());
        }

Calling method:

File file = getFileFromResources("Utility/ConfigurationXML.xml");
strFilePath = file.getAbsolutePath();

which gives me the absolute path of the file, which is in the target folder.

The above code works fine in eclipse IDE, but when I created the project JAR, getFileFromResources() returns me

file:/C:/Users/UserName/Desktop/ExecutableJAR/TestJAR-tests.jar!/Utility/ConfigurationXML.xml

I am using Maven to build my project.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
ashish
  • 11
  • 1
  • And *file:/C:/Users/UserName/Desktop/ExecutableJAR/TestJAR-tests.jar!/Utility/ConfigurationXML.xml* is not fine, because...? You may want to add an actual question. – tevemadar Dec 04 '19 at 07:53
  • 1
    Resources are not files. They are inside the JAR or WAR file. You already have code to get a URL to the resource, and you can get an input stream from that. That's all you need. – user207421 Dec 04 '19 at 07:56
  • In addition to @user207421’s comment, you can convert a `URL` to a `Path` if an appropriate `FileSystem` is installed. That’s the case for ordinary files (the default file system), jar/zip files, and module images, though in case of jar/zip files you’d have to create the file system explicitly first. See also [this answer](https://stackoverflow.com/a/36021165/2711488). But for most purposes, opening the input stream or just passing the `URL` directly to, e.g. the XML parser, is already sufficient. – Holger Dec 04 '19 at 14:08

3 Answers3

1

As your path indicates, the respurce is part of yout test resources. Test resources can't be accessed by production code (while test code can access production resources). If you need this particular resource in production, then you need to move it to your production resources folder hierarcy (that would usually be src/main/resources/utility/configuration.xml.

Test Resources (and classes) are not included in a JAR file because that's considered production code.

Nicktar
  • 5,548
  • 1
  • 28
  • 43
  • While compilation the entire elements inside src/test/resources is copied to target folder and I am able to compile from there, since the absolute path returned is that of target folder but the same isn't true when JAR is created. Is there any other way than moving files into src/ main/ – ashish Dec 04 '19 at 09:13
  • 1
    There is no other way and any other way would be wrong anyway. There is no place for test resources in a productions environment. They just don't belong there. – Nicktar Dec 04 '19 at 09:26
0

As @Nicktar stated that "Test resources can't be accessed by production code"

There's no standard for anything at the root level called resources.

src/main/resources is normally where you put non-Java artifacts that should be moved into the normal package hierarchy, like XML config files loaded as classpath resources.

Nicktar
  • 5,548
  • 1
  • 28
  • 43
Singh123
  • 216
  • 3
  • 10
-1

I have get the same problem wene using netbeans (it does'n work from command line java -jar jarname.jar)
I end up puting my ressource file in a folder in the root of the project then after building the jar I move that folder to the same plase as the jar file.
and it work

elouanesbg
  • 66
  • 1
  • 8
  • It only works if that location is the current working directory when the JAR file is invoked. – user207421 Dec 04 '19 at 08:06
  • But it isn't what you said, and it's an important qualification to your answer. – user207421 Dec 04 '19 at 08:23
  • ......after building the jar I move that folder to the same plase as the jar file – elouanesbg Dec 04 '19 at 08:35
  • The current working directory isn't the same thing as the location of the JAR file. – user207421 Dec 04 '19 at 08:51
  • no in development mode with netbeans you put the resource in the *current working directory * in production mode you move it to the jar location. – elouanesbg Dec 04 '19 at 08:55
  • And thatt doesn't necessarily work, because, for the second time, the current working directory isn't the same thing as the location of the JAR file. You don't seem to appreciate this point. You can run the JAR file from anywhere. – user207421 Dec 04 '19 at 09:08
  • in my case the jar file is not a standalone application it has to be set in the same directory with the resource folder – elouanesbg Dec 04 '19 at 11:57
  • And, for the third time, neither of those is the same thing as the current working directory. If there is something else about your application that makes this always work you haven't stated it. – user207421 Dec 05 '19 at 00:49
  • try this in IDE (netbeans) then from command line (java -jar demo.jar) https://github.com/elouanesbg/demo – elouanesbg Dec 05 '19 at 09:12