I am using hibernate and put xml mapping files in src/hib directory.
but when debug application eclipse run directory is bin.
Is possible to forse Eclipse also copy .xml files of mapping also to bin/hib directory?
Thanks.
Eclipse should do it automatically. The problem is that Hibernate, by default, loads its config file from the default package, and not from the hib package.
Notice that it doesn't load it by opening a file, from the current directory. It loads it as a resource from the classpath. Even if the config file ends up in a jar file, Hibernate will be able to load it, provided it is at the expected location in the package tree (i.e. at the root of the tree).
Related
We have a jar which needs to read a properties file. The properties file needs to edited without rebuilding the application, therefore it is excluded from the build.
In order for the application to see the properties file, it is placed it a folder which is on the java classpath. The format of the Windows command line used to run the application is as follows:
java -cp application.jar;.\lib_folder\*; com.company.Start
Java correctly picks up all the jar files in the lib_folder. The properties file is placed in the same folder, but the application cannot see it and is throwing a FileNotFoundException. Are we doing anything obviously wrong?
Thanks very much
The class resolution happens starting from the path given in cp. If you provide .\lib_folder in the classpath without the star, and the properties file is inside this folder, then it will be picked up. Currently the properties file itself is in the classpath.
it is placed it a folder which is on the java classpath
Your class path only contains JARs, the notation .\lib_folder\* means every jar from a folder, but not the folder itself.
The properties file is placed in the same folder, but the application cannot see it
The folder itself need to be added, however I suspect you shouldn't be adding files to the "lib" directory and you should be using a different directory like "config"
and is throwing a FileNotFoundException.
When you try to obtain a Resource you get null not this exception. Try the following
InputStream is = getClass().getClassLoader().getResourceAsStream("resource.properties");
This returns null if the file is not found.
Are we doing anything obviously wrong?
FileInputStream looks for a direct file, it doesn't use the classpath.
If you're using ResourceBundle to read the property files, then just add the top level project containing the .properties files to the classpath.
I have implemented log4j in my web application project. Project is done using net beans,using tomcat 7.0.41. At first,I created log4j.property file and placed under web page->Web-INF->classes->log4j.properties in net beans and it asks me to locate the file in my project,so I manually located that file to implement log4j in my application. After that I changed the place of the log4j.properties file to myproject->build->web->WEB_INF->classes->log4j.properties in location of my project saved, now its working fine, it did not ask me to manually locate the property file, It takes automatically when my class files executed. Now my problem is that once I committed the project and again checkout the project on some day, property file does not appear and it again ask for property file. So where can I create the log4j property file in my project so that my team mates can utilize it when they checkout project in their system.
Normally you put log4j.properties to src/main/resources/ and it will be copied to the right place by the build process.
I never use net beans, but I think put log4j.properties under Classpath will work.
Not sure how Net Beans handels this, but i think that the "build" directory is where the "compiled" project is put to.
So i would not recommend to put any files there which should be versioned because mostly those directories are ignored for versioning ( see .gitignore files for example when using git).
Resources like property files should be within the sources and your IDE should copy them to the correct place when building the project.
I have a newbie Java question.
I had to make suite of J/DBUnit tests for some stored procedures we use in SQL Server. These tests use some XML files in a couple of sub-directories that I originally had placed in the same directory as my Java project.
Anyway, upon checking these tests in, our SVN manager wanted to keep the .java files in one part of the tree, and resources (like the XML files and required JARs) in another part of the tree.
So, my tests had originally referenced the XML files with a relative path which doesn't work now.
My question is:
Can I make the directories with my XML files available with the CLASSPATH (I hope so).
Assuming that works, how do I reference a file in my code that was included this way?
If I shouldn't be using the CLASSPATH for this, I'm open to other solutions.
Forget calsspath. Provide your tests with a parameter/configuration which defines the root dir for the relative paths of the XML files.
Using the classpath is no problem, the standard maven project layout looks like the following:
src
main
java
resources
test
java
resources
target
classes
test-classes
The compiler compiles src/main/java to target/classes, the resources of src/main/resources are copied to the target/classes folder, similar for the tests. If the tests have a classpath containing classes and test-classes, all works fine.
How is your project layout is, how is it build?
No, you should not use CLASSPATH in this instance since it is used by Java. However, you can use a similar approach by loading a value from an environment variable or configuration file which indicates the directory where the XML files are stored.
You can do this without making any changes to your classpath. The idea is to store the resource files in a separate directory, but have them copied to a directory in your classpath when you run your build process.
Here is an example configuration:
source Directory is ${basedir}/src/main/java
resource directory is ${basedir}/src/main/resources
In your build script, copy both the .java files and the resource files (.xml) to a directory in your classpath, say:
${basedir}/target/classes
Your test code runs against the target dir. The target directory is not checked in to SVN, keeping your SVN admin happy, and you don't have to make changes to your code.
I use Hibernate in an extension-like server-side application, and I put my binaries to the Extensions folder of the server application.
The problem is, that if I run the server, the root folder is the root folder of the server application, and not the root of the binaries. Thus, Hibernate searches the config file in that folder, and it tries to find the classes from that folder. I guess this is related to the JVM, as the server launches it. The config file is not a problem though, because I can just copy my config files there, but all the paths gone invalid.
As a result, in my generated mapping file, I have a class org.gmate.data.SomeClass.java, but it won't find it.
My question: Is there any way, solution or workaround, that I can set the root folder of Hibernate to be the Extension folder, so it starts to get the classes from there?
Thanks for all the replies in advance,
gmate
I finally found the solution for my problem. The problem was not the assumed root folder as I thought, but the Class Loader that the server uses. As it was in my Extensions folder, a CustomClassLoader loaded my classes. and they did not appear in the classpath. As I moved my clsses into a jar file, and added that jar file to the classpath, I managed to use hibernate. Adding the Extension folder to the classpath resulted in loading these classes with SystemClassLoader, but then the advantages of the Custom ClassLoader dissappeared.
I am working on a Java project that I want to deliver to my client as a .jar file. However, I want to allow the client to be able to change the parameters of the program without having to recompile or recreate the .jar. Basically, I want to be able to load .properties files from classes inside the .jar but locate those .properties files outside of the .jar and even outside the working directory.
I have been testing my attempts inside Eclipse, which might be causing some of the problem but I don't see how at the moment. My setup is a follows. I have one project that contains a few classes that I build a .jar file from. I have a .properties file that is used to create a ResourceBundle whenever a class for the .jar is created. I specify that an additional directory, "conf/", be included in the .jar classpath within the .jar manifest.
Once the .jar file is built, it is copied to the lib/ directory of another project which I am using for testing. This test project includes the .jar file as a library ("Add External Jars..") in the Java Build Path. The .properties file is located the conf/ directory which is at the same level as lib/, src/, and bin/ but I am unable to accces it there. The only way I have been able to get it to work is to locate conf/ under src/ (and bin/) but I would like to be able to use it up one level. Is this possible?
Here's the entry in the .jar manifest file...
Class-Path: ../conf/
Here's the ResourceBundle call that I tried (didn't work)...
rb = ResourceBundle.getBundle("..conf.BaseProject");
Here's the directory structure that works now (names have been changed to protect the innocent)...
/Project
/Project/bin
/Project/bin/conf
/Project/bin/conf/BaseProject.properties
/Project/bin/TestClass.class
/Project/lib
/Project/lib/BaseProject.jar
Here's the directory layout I want (again, file names not important)...
/Project
/Project/bin
/Project/bin/TestClass.class
/Project/conf
/Project/conf/BaseProject.properties
/Project/lib
/Project/lib/BaseProject.jar
You can get it using the Classloader.
I like Spring's ClassPathResource