Your question is not clear.
its working fine when the script is invoked from IKJEFT01.
but not working fine when it is invoked some other way??
My guess is that you are invoking the compiled program as a batch job with // EXEC PGM=PGMNM
Think of this environment as an application stack. You cannot invoke TSO services without first making those services available by starting TSO. I think you had a similar post about 'urgent!!'... similarly, you cannot invoke ISPF services without first starting ISPF.
You need to do something like this:
your program #1 -> start TSO -> start ISPF -> your program #2
Where program #1 only uses MVS services
and program #2 may use TSO and ISPF services
My suggestion for program #1 is to try:
myispf = 'ISPSTART CMD(%myprog2)'
Address ATTCHMVS 'IKJEFT01 myispf'
I do not think you need to compile program #2.