Home > Linux > Executing Java Code From Linux Console using Shell Script

Executing Java Code From Linux Console using Shell Script

Sometimes it becomes necessary to execute the java code from console but we cannot write everytime the whole java command including the classpath and properties in the console. There has to some easy way by which we can execute the java code from console. Similar to batch files that we have on windows linux has shell scripts that can run on console. In this article we will learn on how we can execute java code from shell script.

Why we need to execute java code from shell script?

  • Execute complex java code that has lot of external libraries dependencies
  • Executing Java Application as part of Cron Job
  • Perform certain task based on output received from java code(Might require shell script programming as well)

Below is the shell script code for execute java code:

export JAVA_HOME=/usr/java/jdk1.6.0_02
export CLASSPATH=.:lib/log4j-1.2.14.jar:lib/commons-beanutils.jar
 
cd /home/hitesh
$JAVA_HOME/bin/java  -classpath $CLASSPATH com.hiteshagrawal.Test

Code Explanation:

  • Shell Script does not know where is the java command on your machine so we need to goto the JDK installation path.
  • Line No:1 we create an system variable JAVA_HOME having the current machine jdk path
  • Line No:2 Your java code might require some external libraries, that can be specified in the classpath separated by “:”. But remember not to remove the “.” that is the first character in the classpath.
  • Line No: 3 Now goto the directory where you have your java class files that you want to execute.
  • Line No: 4 Here we are executing the java code using the Java command.


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: Linux Tags:
  1. No comments yet.
  1. No trackbacks yet.