hibernate.cfg.xml not found, but i wrote it [duplicate] - java

Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
My configuration file in src/. still i got this error. can some one spot my mistake.

You're using maven with the standard directory layout, so it'll compile the stuff it finds under src/main/java, and copy the stuff it finds under src/main/resources. Your config file is not in any type of source, resource, or test path. It's close, but it's not in any of them, and it needs to be for maven to do something with it.
In order for you to load it, it needs to be copied to your target directory, so it must be in one of the directories maven works with. In this case it needs to be in src/main/resources so that it's copied to the target/classes directory.
Now, I have no idea what that configuration object is, or where it came from, but the config file when it's in the right place will be a resource on the classpath, not a file on the filesystem, so you may have to change the way you're loading it. I'd try what you have first, and it that doesn't work try 'classpath:hibernate.cfg.xml' and see how you go. We'd need a lot more information to fix this for you if it doesn't work.

Related

How to properly get resource Java project

In my project i currently have a setup using eclipse
But when i try to load the file "bg.png" by calling
getClass().getResource("/res/bg.png") or getClass().getResourceAsStream("/res/bg.png") I get a NPE
Can anyone tell me whats happening here? I never really thought there was much difference between how both methods locate their files
TIA
getClass().getResource[AsStream]() uses the class loader to load resources: the same mechanism as the one used to load class files based on the classpath.
So, to be able to load the resource, it must be in a jar file or under a directory that is part of the classpath. That is not the case here.
Move the res directory to the src directory: the file will then be in an Eclipse source directory, and Eclipse will "compile" it by simply copying the file to its bin/classes/whatever destination directory, which is in the classpath when running the application.

How do I use a properties file in Java?

In the web application, which I am going to build, I need some SQL queries, which I can store in a properties file, and get them into my Java code, and then execute.
I have found many suggestions on the internet, but nothing helped me. Some of them are
this.getClass().getClassLoader().getResourceAsStream("resources/file.properties");
this.getClass().getResourceAsStream("resources/file.properties");
I have kept the properties file in resources, and in the same directory in which my Java file is present. Nothing worked. I am using Eclipse and Struts2.
Make sure that your properties files goes to WEB-INF\classes, where your struts.xml is. This folder is on the classpath. Then
this.getClass().getClassLoader().getResourceAsStream("file.properties");
should work as expected. Or of course, if you create a resources folder in classes, the aforementioned code should work too.
If by resources folder you mean src/main/resources, then it is managed by Maven, and it is copied directly to WEB-INF/classes. So you do not need to specify the resorces folder in the method.

How to correctly import files in a maven project structure?

I have a configuration file in xml format, that I need to load into my java code. While testing, I have imported it through it's absolute URL, but now I am about to compile and deploy the project as a jar, and that won't work anymore.
From previous experience, I think the right way to do this, is to use the ClassLoader, but I'm having some difficulties. Maybe because of my project setup, I do not know. I think I would be able to make this work, as I ahve done in the past, but I really want to make sure I do it the standard, conventional and/or correct way, so that I do not need to experiment every single time this comes up.
Here is the code I've tried to implement: http://www.mkyong.com/java/java-read-a-file-from-resources-folder/
However, this code:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
I could not use, due to needing the file in a static method of an abstract class. Therefor I switched it out with the following code:
ClassLoader classLoader = XmlConfigLoader.class.getClassLoader();
File file = new File(classLoader.getResource("configuration.xml").getFile());
The XmlConfigLoader is the containing Class. "configuration.xml" is located in the src/main/resources-folder, just as "file/test.xml" is in the example
I've run the code in debug-mode, and found that the file has the wrong path. Instead of looking in src/main/resources, it points to target/classes
Is there a setting option for default resource folder that I need to set?
Is src/main/resources the conventional place to store files like this?
Is my way of loading the ClassLoader correct in this setting?
As an additional info, this is a Maven project.
UPDATE:
My Current code actually works perfectly, apart from a single bug. The file is automatically transferred to the target/classes folder at compile time. However, whitespaces in the url are replaced by %20, and I have to manually change them back in order to make the system find the file. I am sure there is a better solution to this. Anyone?
It makes sense that the file has the path "target/classes", as this is the Class-Path in your jar's manifest file. If you want to get it to look somewhere else, edit the manifest file and append the resource classpath to it.
There are more details on how to alter your jar's classpath over at Setting classpath for a JAR
As you are using maven, the easiest thing to do is to put you resource file into the src/main/resources/META-INF directory, and maven will sort it out for you. See http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

Cannot read resource from Jar File

I am having trouble figuring out this problem and I have tried almost everything.
I would like my program to read two resources from Jar File, and it will run fine through eclipse. However it gives nullpointer exception when I run the jar from command prompt.
Structure:
src/main/java/App.java
src/main/resources/properties/application.properties
src/main/resources/spring/applicationContext.xml
Code:
To read application.properties
properties.load(App.class.getResourceAsStream("/properties/application.properties"));
To read applicationContext.xml
context = new ClassPathXmlApplicationContext("/spring/applicationContext.xml");
Error:
Error while loading
application.properties java.lang.NullPointerException
What could I be doing wrong?
Assuming that these are in a jar file that is available on the classpath at runtime, the first, getResourceAsStream() example is probably failing because you haven't specified the full path within the jar file. (Also, a common failure it to leave off the leading /, but you seem okay there.) E.g.:
properties.load(App.class.getResourceAsStream("/src/main/resources/properties/application.properties"));
However, if you are unsure where (what path) the files have been given in the jar file that was built, try
jar -tvf <your-jar>.jar
to see what is actually in it.
Can't comment on ClassPathXmlApplicationContext; not a Spring developer.
in your structure you show where the .java is, but you´re missing the .class. The path where it resides is the one the classloader will use. There should be your resources
EDIT: to clarify a bit. In your post you mention the path to App.java. but the classloader only cares about App.class, so let´s say you have
\src
|...(App.java and whatever, these will be ignored)
\bin
|
|--App.class
|
\bin\resources\...(properties and whatever, these will be read)
you should make sure you have properties and spring directories under bin.
If you only have the .java in src it will be ignored by the classloader.

Jar cannot find the path to the file inside

In my Maven project I have a properties file that has a property for a location of keystore file file=filename.p12 (I think the file type doesn't really matter now).
The problem I have that when i built it with maven, I see that the file is inside the root of jar and when i run java -jar the-jar-file.jar I get the IO exception that the filename.p12 is not found.
Everything runs fine in Eclipse, it finds the file and the application runs. Not to confuse somebody, I keep a copy of that filename.p12 as well in src/main/resources folder so that the paths are resolved running in Eclipse and standalone. But this is going to be my other question.
What I can't do is to get the filename.p12 as a resource, because I have external jar that gets as argument my properties file and then handles that properties file itself where the row file=filename.p12 is. Why is the file not found inside the jar, even though I see it's there? My other property files that I have open with Spring's ClassPathResource run just fine.
In order to access internal/embedded resources you need to use Class#getResource or Class#getResourceAsStream depending on your needs

Categories