score:138
If the file is placed under target/classes after compiling, then it is already in a directory that is part of the build path. The directory src/main/resources is the Maven default directory for such resources, and it is automatically placed to the build path by the Eclipse Maven plugin (M2E). So, there is no need to move your properties file.
The other topic is, how to retrieve such resources. Resources in the build path are automatically in the class path of the running Java program. Considering this, you should always load such resources with a class loader. Example code:
String resourceName = "myconf.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
props.load(resourceStream);
}
// use props here ...
score:0
I believe running from Eclipse, if you're using "myconf.properties" as the relative path, You file structure should look somehting like this
ProjectRoot
src
bin
myconf.properties
Eclipse will look for the the file in the project root dir if no other dirs are specified in the file path
score:1
I use something like this to load properties file.
final ResourceBundle bundle = ResourceBundle
.getBundle("properties/errormessages");
for (final Enumeration<String> keys = bundle.getKeys(); keys
.hasMoreElements();) {
final String key = keys.nextElement();
final String value = bundle.getString(key);
prop.put(key, value);
}
score:3
Using ClassLoader.getSystemClassLoader()
Sample code :
Properties prop = new Properties();
InputStream input = null;
try {
input = ClassLoader.getSystemClassLoader().getResourceAsStream("conf.properties");
prop.load(input);
} catch (IOException io) {
io.printStackTrace();
}
score:5
Right click the Resources
folder and select Build Path > Add to Build Path
score:9
If it is simple application then getSystemResourceAsStream can also be used.
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties"))..
score:15
I think you need to put it under src/main/resources
and load it as follows:
props.load(new FileInputStream("src/main/resources/myconf.properties"));
The way you are trying to load it will first check in base folder of your project. If it is in target/classes
and you want to load it from there do the following:
props.load(new FileInputStream("target/classes/myconf.properties"));
Source: stackoverflow.com
Related Query
- Cannot load properties file from resources directory
- Loading properties file from resources directory (IDE vs runnable JAR)
- How to load a properties file from the root directory?
- Eclipse cannot load resources directory (Build Path Problem)
- referencing a directory from a different project in the properties file of another project
- Could not find or load main class- I cannot run my .jar file created from Ant
- Cannot find or load main class from .jar file
- Scala Eclipse project - unable to read text file from resources directory
- Cannot retrieve all 2D array data from Java properties file
- adb: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
- Gradle compileJava cannot find symbol from source file
- Cannot load resources in Annotation Processor (Not on classpath)
- Howto access properties file from Java EE web application?
- Eclipse load data from 2.3 to 4.4 every time you open an xml file
- Cannot start Android Emulator: "Unable to load VM from snapshot"
- Failed to load properties file for android project
- Cannot run program "adb": error=2, No such file or directory while executing through eclipse
- Ruby selenium cannot load such file -- selenium-webdriver (LoadError)
- How to get Ant to copy properties file to classes directory
- libjpeg.so.62: cannot open shared object file: No such file or directory
- How to read from a properties file in Eclipse Java Dynamic Web Project?
- Referencing relative path resources from Android NDK manifest file
- eclipse cannot import project from open file system?
- Log4j cannot find properties file in a simple application
- Unable to read properties file from java
- JavaFX - Cannot load image from within project
- Generate code from properties file
- Cannot stat `./libs/armeabi-v7a/gdb.setup': No such file or directory when running ndk-gdb
- Blackberry simulator won't load cod file from eclipse
- properties file not found after migrating Android project from eclipse to Android Studio
More Query from same tag
- How do I work with PDT and WAMP
- Error parsing XML: not well-formed (invalid token)
- Java applet throwing Exception In Initializer Error
- Android: cannot import BaseGameUtils successfully in Eclipse
- How to use javassist to change the eclipse's compilation?
- Android - Eclipse: Importing package from one project to another
- Saving an int variable "highscore" with sharedpreferences
- Adding Builders to Project in Eclipse and Incremental-ness
- JSP errors, disable in Eclipse Juno
- Eclipse ignores failOnMissingWebXml user property
- Window 10 Getting tomcat 9 to work on https
- Using Hibernate @Column annotation without getters and setters methods declaration
- ANTLR: loop did not match anything at input
- Eclipse debugging: on breakpoint jumps to code after minutes
- Excluding a library at runtime in eclipse
- Rascal Eclipse plugin installation error: "Rascal needs a Java Development Kit (JDK)"
- Eclipse cannot build, but maven can since moving from Java 8 to Java 11
- How can I get intellisense in PHP/Eclipse on custom objects pulled out of array in foreach loop?
- Multiple editor areas in custom Eclipse perspective
- Problema with the java build path with my private lib
- What Eclipse metadata files should be in the repository?
- bitbucket git push not permitted
- UnsupportedOperationException on Endpoint.publish() with Jetty
- glut library for Eclipse
- What Java GUI library IDEA IntelliJ uses?
- Working with Eclipse on two computers
- Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test failed: There was an error in the forked process
- Registering a view in fragment.e4xmi when using Legacye4.xmi
- Can android layout files be put in to different folders or groups
- Output is sent to console instead of REPL when using threads in Eclipse/CounterClockWise