0
votes

I have azure-cli installed in my mac and I can successfully execute all azure commands from cli. I want to do the same from a java application. I have seen examples of using REST endpoints and azure-sdk-for-java. But what I want is to execute the azure-cli commands directly from java.

I tried running 'azure login' from a java program, but I got the following error.

Cannot run program "azure": error=2, No such file or directory

2
I had to give the full path to azure. For eg, Runtime.getRunTime.exec('/usr/local/bin/azure account show') worked. - harshitha bt

2 Answers

0
votes

The problem is in a lack of your PATH variable. If you want to enjoy the same "find the command" capabilities that you have on the command line, you need to set it somehow within your Java program: import the PATH variable from the environment, set it within the code you're writing, or whatever mixture covers your needs.

Giving the absolute (full) path is the surest way, but you may not want to read such long command names in your source codes.

0
votes

@harshithabt Per my experience, the issue was caused by the command azure could not be searched in the directories listed in PATH. There are two ways to solve the issue for running commands in Java, please see below.

  1. Setting up the environment variable PATH for the current shell session or the configuration files ~/.profile(or ~/.bashrc).

    • If you command export PATH=<your-node-path>/bin:$PATH in a shell session, you only run the Java program with azure command in the current shell session, even run via the Java IDE which must be opened in the current shell session (it means you should open Eclipse via command <your-ide-path>/eclipse).
    • If you configure the files ~/.profile or ~/bashrc to add the node runtime as below, please make sure the configuration files have been reloaded via command source ~/.profile in a current session or restart sessions via logout & login or reboot.
  2. A simple way is that adding the command links into the dirs listed in PATH, such as /bin. You just need to run the commands below.

    sudo ln -s /bin/node /bin/node sudo ln -s /bin/azure /bin/azure

Then you can run azure-cli command from Java successfully.