wood burning stoves 2.0*
The moose likes General Computing and the fly likes How return a string from java program to the shell which invokes thee java command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "How return a string from java program to the shell which invokes thee java command" Watch "How return a string from java program to the shell which invokes thee java command" New topic
Author

How return a string from java program to the shell which invokes thee java command

Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
Hi,

I have an encryption program completely written in java , which will ecnrypt and decrypt the password.

But after decryption it should return the decrypted password to the SHELL script where we issue java command , so that that decrypted password can be stored in the variable in the shell script.

Also i m little bit confused about invoking java program form shell script and storing the return value in the variable like below.
password1=java EncryptProgram
considering EncryptProgram as java Class file.

can any one help me to do this.

Thanks in advance.

Sekhar.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5772
    
    7

You did not say which shell you are using so I will assume the bash shell. You could try this:

password1=`java EncryptProgram`

Those are backward-ticks around the java command. Then have your program write to System.out.


JBoss In Action
Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
Hi , thanks peter for valuable information, i m using bash shell.

i have decryptPassword() method from that method i need get the ecnrypted password which can be stored in password1 variable in shell script.

my program looks like below

import stuff here;
public class EncryptAndDecrypt{

public static void main(String args[] ){

------------
-------
checkUserExistence();
encryptPassword();
decryptPassword(); //the decrypted password in this method should get stored in "password1" variable in shell script.

}

public static void checkUserExistence(){ ------------}
public static void encryptPassword(){
// This will encrypt and stores the password in the pasword config file
}

public static void decryptPassword(){
//This will decyrpt and prints the password.
// REQUIREMENT is the decrypted password should get stored in "password1" varaiable in the shell script.
/}

}//class

Can anyone help me in this regard.

Thanks,
Sekhar.




Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
In Otherwords , here is my shells script

ed.sh
-----
#!/bin/bash
PATH=$PATH:/opt/was61/WebSphere/AppServer/java/bin
CLASSPATH=/opt/was61/WebSphere/AppServer/java/lib:/tmp/ch:.:$CLASSPATH
echo 'Shell script to execute encryption program'
echo 'enter username'
read user

password1=java -cp $CLASSPATH GetThePassword $user
echo $password1

---------
When we ran tha above ed.sh it's showing the following error.


./ed.sh: line 8: -cp: command not found

Can anyone solve this problem.

When i remove "password1" in the below line

password1=java -cp $CLASSPATH GetThePassword $user

It's working fine.

but i want to store the result of into password1 variable.

Thanks ,
Sekhar.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5772
    
    7

My previous post contains the solution to the "./ed.sh: line 8: -cp: command not found" problem. Go and read it again and then tell me what is missing from this script line:

password1=java -cp $CLASSPATH GetThePassword $user
Jagan Sirigiri
Greenhorn

Joined: Aug 12, 2009
Posts: 1
Hi ,
I have similar requirement but with a small change.My java program is running on a remote machine instead of on local machine.I want to call this program from a remote unix machine and need to get a string to be returned from java program.How can I change the shell script for this.I appreciate any help.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5772
    
    7

Jagan, welcome to Java Ranch!

Please do not hijack someone else's topic - instead open a new topic and ask your question there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How return a string from java program to the shell which invokes thee java command
 
Similar Threads
Multiple programs/processes
How to call Java program main method from shell script
java console input doesn't stop linux "rpm" execution
how to get output string parameter from Java to shell script
how can i call a shell script method in other program