Thursday, August 27, 2009

Environment Variables (Java) and jar file creation

Setting java enviornment variables : -
Setting environment variables is the first thing you have to do before starting working with Java. Well its a quite simple thing and if you are a java programmer you know this. But still I have seen many people getting confused over setting environment variable.

So here are the simple things you need to do.
1. Create environment variable CLASSPATH and set the value to following
"C:\Program Files\Java\jdk1.6.0_13\\lib\tools.jar"

2. Create environment variable JAVA_HOME and set the value to "Directory where java installed"
example JAVA_HOME=C:\Program Files\Java\jdk1.6.0_1

3. Add path till bin folder of your jdk in PATH variable
example PATH = ;C:\Program Files\Java\jdk1.6.0_13\bin\

That's it you are now all set to use compile and run your java code.

Creating a executable jar file is again a very simple task in java you just need to follow following steps.
1. Compile your java code and put the class file in a separate folder.

2. Create a file in the same folder and write the mainclass in your Manifest file
Main-Class: JarDemo (or give path with package names)

3. Go to the same folder via command prompt and execure following command
jar -cvfm demo.jar *.class
4. This will create the executable jar file in the same folder with name demo.jar
execute the jar using the below command
java -jar demo.jar

No comments:

Post a Comment