reading a file from a relative path in a maven project

sonal

I have my java file in

/src/main/java/Test.java

and I want to read a property file/XML file from

/src/main/resources/plugin.properties and/or

/src/main/resources/templates/basic_java.xml

When I try to read these files from Test.java (after deployment of course), it is not able to do so and giving the error

java.io.FileNotFoundException: /target/plugin.properties (No such file or directory) at java.io.FileInputStream.open0(Native Method)

Though I can find the plugin.properties and basic_java.xml under /target/classes folder, yet it cannot read these files.

I have tried multiple solutions including :

1. getClass().getResource("Test.java");
2. new File(".").getCanonicalPath() + "target//plugin.properties";
3. ((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs()
4. ((URLClassLoader) Thread.currentThread().getContextClassLoader()).getResourceAsStream("Test.java")
5. Thread.currentThread().getContextClassLoader().getSystemResources("Test.java");
6. new FileInputStream("src\\main\\resources\\plugin.properties");
7. new FileInputStream("resources\\plugin.properties");
8. new FileInputStream("src\\main\\resources\\plugin.properties");
9. new FileInputStream("\\plugin.properties");
10. new FileInputStream("\\classes\\plugin.properties");

but no luck.

Essex Boy

You need something like this:

    InputStream inputStream = getClass().getResourceAsStream("/plugin.properties");
    String content = IOUtils.toString(inputStream);

IOUtils is is from apache commons

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

This will open an input stream from the classpath.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reading a file from a relative path

From Java

Reading file using relative path in python project

From Dev

Reading file using relative path in python project

From Dev

Trouble reading/writing file from relative path

From Dev

Maven Project : How to specify a relative path in a *.ini file?

From Dev

In maven project how i can specify a relative path to file

From Dev

Path relative to multi-project build file

From Dev

Can the required path be from project root and not relative?

From Dev

list full path of file without tying it from relative relative path

From Dev

Reading file from PATH of URI

From Dev

Reading Path From Config File

From Dev

Get relative path from a file in Qt

From Dev

Load file from resources by relative path (Spring)

From Dev

Creating dir and loading file from relative path

From Dev

Load file from resources by relative path (Spring)

From Dev

Get relative path from jar file

From Dev

How to determine a file path in Maven project?

From Dev

node.js get relative to project/src path of file

From Dev

How to refer to a relative path of a YAML file in a Google App Engine project?

From Dev

Reading a CSV file from an unknown path, php

From Dev

Reading file from path in Swing in different environments

From Dev

Wrong file path while reading from UI

From Dev

Reading plist file from iOS project bundle

From Dev

Save log file in a relative path from .properties file

From Dev

Java relative path to file

From Dev

Load file with a relative path

From Dev

Open file with relative path

From Dev

Relative path in a HTML file

From Dev

Relative path in configuration file