Search code examples
javacommand-lineanacondaprocessbuilder

ProcessBuilder - cannot execute the conda command


When I run the following command in command line I get no problems and the environment changes to jaydev

conda activate jaydev my command line after this looks something like this (jaydev) C:\Users\jay dev>

(I am writing this command in windows command line and not in anaconda prompt , I have made the necessary configurations to do so)

But when I use process builder and try executing the same command it does not execute the problem is seen below.

ProcessBuilder pb = new ProcessBuilder("conda", "activate", "jaydev");]
Process p = pb.start();

error -

    java.io.IOException: Cannot run program "conda": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at test.Test.main(Test.java:23)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
    at java.lang.ProcessImpl.start(ProcessImpl.java:137)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 1 more

any help will be appreciated


Solution

  • This worked for me :

        List<String> processParameters = new ArrayList<String>();
        processParameters.add("cmd.exe");
        processParameters.add("/C");
        processParameters.add("conda");
        processParameters.add("activate");
        processParameters.add("myENV");
    
        ProcessBuilder pb = new ProcessBuilder(processParameters);