Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sekhar Choudary
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sekhar Choudary
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jagan, welcome to Java Ranch!

Please do not hijack someone else's topic - instead open a new topic and ask your question there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic