Wednesday, November 19, 2008

How to get version number from Spring Jar.

Do this

System.out.println(SpringVersion.getVersion());

Or

Look at manifest file.

hibernate environment specific configuration where there is no JNDI access

http://java-aap.blogspot.com/2007/05/configuration-of-environment-settings.html

Here are the contents of the above link

MAD Code Monkeys

MAD Code Monkeys Blog. Marcel Panse and Daniel Rijkhof are software engineers from the netherlands specialized in techniques like Java, Ruby, Spring, Hibernate, DWR and Flex
Tuesday, May 15, 2007
Configuration of Environment Settings
written by Daniel Rijkhof

I get a lot of questions asking how I deal with configuring environment specific settings. I'll explain my solution here.

The following solution depends on the Spring framework.

1) have a separate package per environment (e.g. production, test, local)
2) put a properties file in this directory (e.g. webapp.properties)
3) configure the spring context
4) set a system environment variable to choose your environment


I'll use the following packages:


com.mad.project.env.junit
com.mad.project.env.local
com.mad.project.env.test
com.mad.project.env.acceptance
com.mad.project.env.production



The webapp.properties files i'm using are simular to my junit.properties file:


hibernate.schemaUpdate=true

hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=false

dataSource.driverClassName=org.hsqldb.jdbcDriver
dataSource.url=jdbc:hsqldb:mem:test
dataSource.username=sa
dataSource.password=

webapp.logDir=../logs/junit

env.dir=junit
env.name=junit environment


Note: the env.dir line is only present in my junit env properties file.


The context:







classpath:com/venspro/payment/env/junit/junit.properties
classpath:com/venspro/payment/env/${env.dir}/webapp.properties










...




${hibernate.dialect}
${hibernate.show_sql}







Main DataSource






...



Take a close look at the propertyPlaceHolderConfigurer; there is a default configured, and one with an EL expression. If ${env.dir} is not specified, it will not find the properties file, and ignore this because of the ignoreResourceNotFound setting.

The properties in the junit.properties file, are overwritten by any settings in the next properties file.

The only thing left to do is specify the environment setting 'env.dir'. This can be done in several ways; in the servlet container configuration, or on the system environment, or as a parameter to the jvm (-Denv.dir=test).
Posted by Daniel Rijkhof at Tuesday, May 15, 2007
Labels: hibernate, java, spring

All about java and jar files.

Here is a good article on java and jars.

http://fraaargh.wordpress.com/2008/07/13/how-to-java-jars-and-manifestmf/




Snap Shots Options [Make this Shot larger] [Close]
Options
Disable
Get Free Shots


Close
Snap Shares for charity
Some thoughts about things
No forward thinking leads to afterward sinking
« Blagounette graveleuse
Use ANT to auto-generate the list of jar files in a MANIFEST.MF »
How-to java jars and MANIFEST.MF

Here’s a brief “how-to” for remembering what is possible with a jar file, how to use it, launch its main class and define where the other jars (the libraries used) are stored. I could not find anywhere on the net a good explanation of all this, so decided to write my own.
So imagine you have your own jar file named myApp.jar which main class (the one with the “main” method) is marot.francois.MyMainClass which needs to receive 2 arguments: arg1 and arg2 (passed in String[] args).

1- Without using any MANIFEST.MF fil

If you don’t have a MANIFEST.MF file in your jar, or if you want to set some specific places where Java should look for the libraries used by myApp.jar, then you should use the following command. Beware, the use of the star is only available since Java 6 (or is it Java 5 ?)

on Windows:
java -cp tmmerge.jar;myCustomLibPath1\*;myCustomLibPath2\*;myCustomLibPath3\* marot.francois.MyMainClass arg1 arg2
on Linux:
java -cp tmmerge.jar:myCustomLibPath1\*:myCustomLibPath2\*:myCustomLibPath3\* marot.francois.MyMainClass arg1 arg2

Same as previous one but with all the libraries’ jars listed instead of using *:
on Windows:
java -cp ./myApp.jar;myCustomLibPath1\sctm.jar;myCustomLibPath2\ant.jar;myCustomLibPath2\ant-launcher.jar;myCustomLibPath3\scdata.jar marot.francois.MyMainClass arg1 arg2

on Linux:
java -cp ./myApp.jar:myCustomLibPath1\sctm.jar:myCustomLibPath2\ant.jar:myCustomLibPath2\ant-launcher.jar:myCustomLibPath3\scdata.jar marot.francois.MyMainClass arg1 arg2

At first, I thought such a command line was overriding the classpath in MANIFEST.MF, if any. But that is not the case, the MANIFEST.MF is just bypassed.


2- Using a MANIFEST.MF file in the jar
Launch myApp using the jar files listed in myApp.jar’s own manifest
java -jar myApp.jar arg1 arg2

The content of the META-INF\MANIFEST.MF file must be:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.6.0_02-b06 (Sun Microsystems Inc.)
Main-Class: marot.francois.MyMainClass
Product-Name: myApp
Package-Title: marot.francois
Package-Version: 1.00.00
Package-Vendor: Elsys Design Avisto
Class-Path: myCustomLibPath2/ant-launcher.jar myCustomLibPath2/ant.jar myCustomLibPath3/scdata.jar myCustomLibPath1/sctm.jar

In a next post, I’ll show you how to use ANT to automatically generate the content listed here. And more specifically the list of libraries which would be error prone to write by hand.
By the way, I think it’s good to point to something important: in the “Class-Path”line of the manifest, you CAN’T use a star as you could do when specifying the classpath on the command line.
Also be aware that the space is the jars path delimiters as opposed to the “;” (windows) or “:” (Linux) on the command line. This last remark is very very error prone…

3- What I would like to be able to do (but it seems like Java doesn’t handle this case)

Warning: the exemple given hereafter does not work. I spent a lot of time trying to find some explanation of the reasons why, but was not able to find.
So the problem seems to be that you can’t override the default classpath specified in the jar’s manifest if you use the -cp switch at the same time. So the basic rule of thumb is:
“in java, do not use both switches -jar and -cp at the same time”: it does not work (at least up to Java 6).
I thought it would be cool to be able to run a jar but defining another place where it can find its dependancies. In case you want the users of your app to be able to use a shared folder containing the lib jars but don’t want him to have to know the main class’ name.
Here’s the command line I desperatly tryed to run:
REM java -cp myCustomLibPath1\*;myCustomLibPath2\*;myCustomLibPath3\* -jar myApp.jar arg1 arg2

Nevertheless, the same goal can be reached (successfuly) by using method 1. Only that the user needs to know the main class.

Tags: java jar manifest

This entry was posted on 2008-07-13 at 22:34 and is filed under Java, dev. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
6 Responses to “How-to java jars and MANIFEST.MF”

1. Use ANT to auto-generate the list of jar files in a MANIFEST.MF « Some thoughts about things Says:
2008-07-13 at 23:04

[...] Some thoughts about things Computing, IT, society… « How-to java jars and MANIFEST.MF [...]
2. Swathi Says:
2008-08-06 at 06:53

Thank you very much for the valuable inputs…
I am facing an issue.. I am using a junit library and built a test library testXXX.jar now this has a number of dependent jars say XXX.jar etc but how do i ask the testXXX.jar to use XXX.jar and execute the test library?
3. fraaargh Says:
2008-08-08 at 13:57

Swathi, I don’t really understand your problem there… Why can’t you use for testXXX.jar the same process used for your application’s main jar ?
4. Swathi Says:
2008-08-20 at 07:08

Actually i have one jar xxxlib.jar . Now from command line i ll be executing the jar yyylib.jar(inside that i have one property file prop.properties) which inturn use the xxxlib.jar. Now my problem is when i try to access the prop.properties while executing the yyylib.jar from cmd prompt i am not able to read the property file inside the yyylib.jar. I want to know how i can read this property file which is actually inside the jar?
5. fraaargh Says:
2008-08-20 at 21:45

If I understand, you try to access the *.properties file in xxxlib.jar from yyylib.jar… I never faced such a problem. Perhaps you could add a utility class in xxxlib.jar that would provide such an access through public methos ? Otherwise, try to find information on how to read files in jar files.
6. Swathi Says:
2008-09-10 at 13:18

Actually thats my doubt i am unable to read the property file inside the xxxlib.jar.

Leave a Reply

Name (required)

Mail (will not be published) (required)

Website

Blog at WordPress.com.
Entries (RSS) and Comments (RSS).

Thursday, November 13, 2008

Spring: Basic Application and Environment Set up.

To get started on spring.

http://static.springframework.org/docs/Spring-MVC-step-by-step/part1.html

How to create spring-hibernate project structure with maven archetype.

http://sezera.blogspot.com/2008/01/how-to-create-project-directory.html

Nowadays i am working on a new dynamic web project which uses spring-hibernate-richfaces trio.My first task to set up development environment of project.It involves many steps.I will write sth about it occasionally.

Here I will use Maven Archetype plugin to create directory structure of my project.Assume that dynamic webproject has 3 directory initially.

core
web
security

Because we will created an internal artifactory repository and configured it we dont care about jars anymore.Project will be simply involves java classes,resources like gifs,jpegs for web side,and our pom.xmls.Jars will reside in M2_REPO which is = Your home\.m2\repository as default.

1.Create an empty directory e.g myproject
2.Go inside it
3.write in command prompt

mvn archetype:create -DgroupId=com.mycompany.myproject.core
-DartifactId=myproject-core -DarchetypeArtifactId=maven-archetype-quickstart

It will create myproject-core directory.

4.write in command prompt

mvn archetype:create -DgroupId=com.mycompany.myproject.web
-DartifactId=myproject-web -DarchetypeArtifactId=maven-archetype-webapp

It will create myproject-web directory.

5..write in command prompt

mvn archetype:create-DgroupId=com.mycompany.myproject.security
-DartifactId=myproject-security-DarchetypeArtifactId=maven-archetype-quickstart

It will create myproject-security directory.

You will see that every directory has a pattern.Thanks to this plugin you dont need to think about directory structure of your project.

For more information
http://maven.apache.org/plugins/maven-archetype-plugin/