Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.helpdeskpreprocessor.utils;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Map;
- public class EnvVarsManager {
- public static void setEnvVariable(String var, String val) throws IOException {
- ProcessBuilder pb = new ProcessBuilder("CMD.exe", "/C", "SET"); // SET prints out the environment variables
- pb.redirectErrorStream(true);
- Map<String,String> env = pb.environment();
- //String path = env.get(var);
- env.put(var, val);
- Process process = null;
- try {
- process = pb.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
- String line;
- while ((line = in.readLine()) != null)
- {
- System.out.println(line);
- }
- }
- public static void appendToPath(String pathToAppend) throws IOException {
- ProcessBuilder pb = new ProcessBuilder("CMD.exe", "/C", "SET"); // SET prints out the environment variables
- pb.redirectErrorStream(true);
- Map<String,String> env = pb.environment();
- String path = env.get("Path") + ";" + pathToAppend;
- env.put("Path", path);
- Process process = null;
- try {
- process = pb.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
- String line;
- while ((line = in.readLine()) != null)
- {
- System.out.println(line);
- }
- }
- }
- //and in Main:
- try {
- EnvVarsManager.setEnvVariable("CUDA_VISIBLE_DEVICES", "0");
- } catch (IOException e) {
- e.printStackTrace();
- }
Add Comment
Please, Sign In to add comment