You'll have to provide more details including relevant code. Where are you trying to use this construct and what do you mean by "want to get .jar"?
File mappingJar = new File(Thread.currentThread().getContextClassLoader().getResource("mappings.jar").getFile());
this was the piece of code we were using to get the jar file, but in JBoss AS 7 its not working its giving null even the mappings.jar exist. our project structure is like follows
I'm trying to call the above mentioned method from a class belongs to abcjava.jar but when i debugged the following line of code
its coming back as null.
I hope this would be enough to understand the problem, please let me know if u need more specification
Any idea please ?
I don't think "mappings.jar" is a resource in this context. On the other hand, it's content (if that's what you're looking for) could be.
Yes absolutly that jar contains all hibernate mapping xml files which i want to pass it to Hibernate configuration like below.
final Configuration configuration = new Configuration();
File mappingJarsLocation = new File(Thread.currentThread().getContextClassLoader().getResource("mappings.jar").getFile());
but the Thread.currentThread().getContextClassLoader().getResource("mappings.jar") returns null
JBoss AS 4.2.3 it was working.
Did you try "lib/mappings.jar"?
By the way, where and when is that code being exceuted? The thread context classloader won't be null if it's being run as part of the usual EE request. But if you are running that code at some other point then the TCCL might not be set.
TCCL is there but TCCL#getResource("mappings.jar") is returning null.
I tried all possible way to pass mappings.jar location.
Its says to embbed mappings.jar inside another jar file. In our deployment plan its a little difficult task, Can you please suggest some straight way to get the path of deployment/xyz.ear/lib programatically.
I suspect that there's no easy solution to this.
Consider biting the bullet and adding mapping elements to your hibernate.cfg.xml file. Hibernate will then locate your mappings as regular resources on the classpath. Even if you have a 100 mappings, you should be up and running in an hour or so.
Its pretty tuff task for us to include all the hbm inside cfg, sice our ERP product is having more than 500 hbm files and its located in various module jars. Right now we are doing some hack to get the mappings.jar location. But we are literally disappointed with that approach. If anybody knows a neat way to get the path upto /lib folder would be great.
1 of 1 people found this helpful
I agree that 500 mapping files is too much for manual intervention.
In the short term I would consider the following steps:
- Write a quick and dirty jar analyzer that will write a list of all the mappings in a provided list of mapping jar files to a file. You could copy the logic in org.hibernate.cfg.Configuration.addJar(File) for this purpose.
- Include this file in your build in a well known location so that you can read it as a classpath resource.
- Change your initialisation code to call org.hibernate.cfg.Configuration.addResource(String) for each mapping record in the generated file
This would be a portable solution that will work without needing to access the mapping files through the file system at runtime.