Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.charset.Charset;
- public class RuntimeRunner {
- public static void main(String[] args) throws IOException {
- String conda = "D:\\Miniconda\\condabin\\conda.bat";
- String[] condaEnvs = {
- "PATH=" +
- "D:\\Miniconda\\envs\\py37;" +
- "D:\\Miniconda\\envs\\py37\\Library\\mingw-w64\\bin;" +
- "D:\\Miniconda\\envs\\py37\\Library\\usr\\bin;" +
- "D:\\Miniconda\\envs\\py37\\Library\\bin;" +
- "D:\\Miniconda\\envs\\py37\\Scripts;" +
- "D:\\Miniconda\\envs\\py37\\bin;" +
- "D:\\Miniconda\\condabin;" +
- System.getenv("PATH")
- };
- //Runtime.exec
- System.out.println("--- Runtime.exec()");
- System.out.println(">>>>>> single command:");
- runtimeTester(conda + " info --envs");
- System.out.print("packages count (base): ");
- runtimeTester(conda + " list | wc -l");
- System.out.println("\n>>>>>> consecutive command:");
- System.out.print("packages count (py37): ");
- runtimeTester(conda + " activate py37 && conda list | wc -l");
- // Runtime.exec with environment variables
- System.out.println("\n--- Runtime.exec() with environment variables");
- System.out.println(">>>>>> python");
- runtimeWithEnvsTester("cmd /c python -c \"import os;" +
- "print('\\n'.join([path for path in os.environ['PATH'].split(';')" +
- "if 'conda' in path]))\"", condaEnvs);
- }
- static void runtimeWithEnvsTester(String command, String[] envs) throws IOException {
- Process process = Runtime.getRuntime().exec(command, envs);
- BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("UTF-8")));
- String line;
- while ((line = in.readLine()) != null) {
- System.out.println(line);
- }
- }
- static void runtimeTester(String command) throws IOException {
- runtimeWithEnvsTester(command, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement