Java Return Value to Unix Shell Script

Today I faced a situation where I need to verify something though Java class in database . My problem was I have to do this inside unix shell script. I created the below solution to achieve that, I am posting here sample working code:

JAVA CLASS

public class Sample
{

public static void main(String args[])
{
//Do the complete logic and print the result using system out
System.out.println(“Sample Testing”);    
}
}

Shell Script

JAVA_HOME=/apps/Benefits/SecuritySync/java/bin/java

CLASSPATH=.
#Store the result(printed through system out) of program in variable
result=$(${JAVA_HOME} -cp ${CLASSPATH} Sample)
#Print the result.
echo  “My Results =>” $result

About nitingautam

I am Tech Lead (Java/J2EE/ExtJs) with a MNC located @ Gurgaon.
This entry was posted in HowTo, Java, unix. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *