Triggering UNIX Script from a JAVA program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Triggering UNIX Script from a JAVA program
# 1  
Old 12-08-2015
Triggering UNIX Script from a JAVA program

Hi I am trying to implement one program, where JAVA needs to trigger the backend UNIX script. Tried with options like

Code:
String[] cmdArray = {"/bin/ksh","-c","/SCRIPT_ABSOLUTE_PATH/sampleScript.ksh /FILE_ABSOLUTE_PATH Test_File.dat TEST E SFTP"}

When I trigger the script from front end with the required parameters, it is just printing the echo statements of the script, one exception is first echo statement which is writing to a file is also not executing.
But the commands inside the script (like rm, gpg) are not getting executed.

When I run the same script with the same parameters from the command line, I don't have any issues.

Code:
#!/bin/ksh

SCRIPT_LOG=/MY_ABSOULTE_PATH/Script.log
echo "Script Triggered at `date`" >> $SCRIPT_LOG
DIRECTORY=$1
FILE_NAME=$2
CODE=$3
ACTION=$4
TRANSFER=$5

echo "File Path : $DIRECTORY"
echo "File Name to process : $FILE_NAME"
echo "Code selected : $CODE"
echo "Action Required : $ACTION"
echo "File Transfer : $TRANSFER"

cd $DIRECTORY
echo "`pwd`"

if (test -s $FILE_NAME.pgp); then
   echo "Deleting the already existed file in server."
   rm $FILE_NAME.pgp
fi

if [ $CODE == 'TEST' ]; then
echo "I am in if"
/usr/local/bin/gpg -r "sample@abc.com" --batch --passphrase mypassphrase --output $FILE_NAME.pgp -e $FILE_NAME
echo "exiting if"
fi
echo "script completed."
exit 0

How can I make the script execute same as the one from the command line through JAVA.

My Java code and UNIX script are on same server.
AIX 6.1

---------- Post updated at 11:37 PM ---------- Previous update was at 03:35 AM ----------

ANy ideas ?
# 2  
Old 12-08-2015
The problem is: the java creates a child process that does not have environment variables like you would expect.

The script java executes has to source the startup files in your account (or where ever) to define PATH, and other important environment variables. This is a problem with the script, not necessarily java.

Example:
Use the dot operator . /path/to/.profile
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Triggering remote UNIX shell script from Remote desktop

I m trying to run a batch script in remote desktop which executes unix commands on the unix server...the problem is i wnt the output in HTML format.so in my batch script i m giving the cmd like ssh hostname path ksh HC_Report.ksh>out.html ...but it generates the HTML file in remote desktop .i... (2 Replies)
Discussion started by: navsan
2 Replies

2. Shell Programming and Scripting

how to execute a unix shell script from a java program

Hi All, well , i am facing this problem.. i have tried a few sample codes but there isn't any solution . could anyone please give a sample code as of how to do this... Please see the below details...and read the details carefully. I have written some code, logic is 1)from... (4 Replies)
Discussion started by: aish11
4 Replies

3. Shell Programming and Scripting

How to pass the environment name while calling java program from unix script?

Hi, I'm trying to test one unix shell script in dev environment. But I'm not sure how to pass the environment in my java program calling code. I'm trying to use -DconsumerEnv="DEV" but unfortunately I get 'null' while trying to print the value from java class. System.out.println("Environment: "+... (4 Replies)
Discussion started by: Pramit
4 Replies

4. UNIX for Dummies Questions & Answers

using a variable from java program in unix

Hi, I need to execute a java program in a shell script.I then need to assign the value being returned by the program into a unix variable.Is there any way i can access the value in a variable in java program? As in if my java code has a variable, . . . . valueA =x.getValue() ..... can... (1 Reply)
Discussion started by: inquisitive101
1 Replies

5. Shell Programming and Scripting

Triggering my Unix script....

Hi All, i dont have any idea about perl scripting... i need some suggestion so that i can put my effort to find out the solution:D let me explain....one of my tedious task which will taken care by Unix shell script which i prepared. its a kind of routine work that i am running the... (4 Replies)
Discussion started by: Shahul
4 Replies

6. Shell Programming and Scripting

How to invoke a perl script from java program

Hi: I need to invoke a perl script from a java applet program in UNIX operating system.. Can you please suggest me!!! (4 Replies)
Discussion started by: grajesh_955
4 Replies

7. UNIX for Dummies Questions & Answers

Connection problem with gui java program to postgreaql database using unix

Having problem in connecting my gui java program to postgreaql database. I first used setenv classpath /home/share/postgresql/java/postgresql.jar:proj1, where proj1 is my folder conatining all java and class file, to set classpath. Then javac *.java. Then java proj1.Login. It gives me... (2 Replies)
Discussion started by: uci
2 Replies

8. Shell Programming and Scripting

Invoke java program in script

Hey all, My boss tasked me with the job to write a script which would invoke various java programs, the thing is I don't know much about shell scripting so would you experts help me out? Here is the requirement - 2 applications written in java: App_A and App_B -... (0 Replies)
Discussion started by: mpang_
0 Replies

9. UNIX for Dummies Questions & Answers

Java program calling a UNIX command

I was wondering if it was possible to call a unix command directly from a Java program during run-time. This command could very very basic e.g. "ps -ef" returned as a string, all I need is a starting place so if anyone has any suggestion or examples I would be very grateful (2 Replies)
Discussion started by: QUartz Ite
2 Replies
Login or Register to Ask a Question