Home java where the file must be for GetClassLoader (). GetResourceAssstream?

where the file must be for GetClassLoader (). GetResourceAssstream?

Author

Date

Category

Hello.

The need to make information on the database connections in a separate file.
I tried to use this option, but I still can still understand where the file db.properties

should physically lie

Properties Prop = New Properties ();
Inputstream inputstream = fetchdata.class.getclassloader (). GetResourceAssstream ("/ db.properties");
Prop.Load (InputStream);
STRING DRIVER = PROP.GETPROPERTY ("DRIVER");
STRING URL = PROP.GETPROPERTY ("URL");
STRING USER = PROP.GETPROPERTY ("User");
STRING PASSWORD = PROP.GETPROPERTY ("PASSWORD");

tried to place /web-inf/classes/db.properties
does not find.
I tried to specify the absolute path – the result is also negative.

where should it be?


Answer 1, Authority 100%

Example 1. Use the getclass () design. GetResource ("/ Images / logo.png") . Since the name begins with the ‘/’ symbol – it is considered absolute. Resource search is as follows:

To the path from Classpath C: \ Work \ MyProject \ Classes attributed the resource name /images/logo.png , as a result of which the file C: \ Work \ MyProject \ Classes \ Images \ logo.png . If the file is found – the search stops. Otherwise:
In the jar file C: \ lib \ lib.jar Looking for a file /images/logo.png , and the search is carried out from the root of the JAR file.

Example 2. We use the getclass () design. GetResource ("Res / Data.txt") . Since the name does not start with the ‘/’ symbol – it is considered relative. Resource search is as follows:

To the path from Classpath C: \ Work \ MyProject \ Classes attributed to the current class of the class where the code is located – / RU / Skipy / Test – and then the name Resource res / data.txt , as a result of which the C: \ work \ MyProject \ Classes \ Ru \ SkiPy \ Test \ data.txt is searched for. If the file is found – the search stops. Otherwise:
In the jar-file C: \ lib \ lib.jar Looking for a file /ru/skipy/test/res/data.txt (the name of the package of the current class plus the resource name), And the search is conducted from the root of the JAR file.

In your case, you need to write getresourceasstream ("db.properties") without specifying the ‘/’ symbol, so that the file finds in the Project \ src \ main \ resources \ folder
But you can put db.properties in project \ db.properties and already specify as in your example

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions