What would be a good solution to ensure information security and integrity in the following scenario:

A Java application needs to do some initial work, then pass credentials (for example, a username and a password) to a shell command which will do some processing (possibly make requests over https, amongst other operations) and then return a private token to the Java application.
Let's assume for this discussion that the functionality of the shell command cannot be replicated in Java without reasonable effort and most of it is closed-source.
What would be a good manner to ensure neither the credentials nor the token can easily be hijacked by a third party?
Assume, that the IO can be customized as necessary in both the Java application and the shell command.
Icons from Saki & Untergunter
Runtime.getRuntime().exec("command here")is safe enough?