Guest User

Untitled

a guest
Jun 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. package com.helpdeskpreprocessor.utils;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Map;
  7.  
  8. public class EnvVarsManager {
  9.  
  10. public static void setEnvVariable(String var, String val) throws IOException {
  11. ProcessBuilder pb = new ProcessBuilder("CMD.exe", "/C", "SET"); // SET prints out the environment variables
  12. pb.redirectErrorStream(true);
  13. Map<String,String> env = pb.environment();
  14. //String path = env.get(var);
  15.  
  16. env.put(var, val);
  17. Process process = null;
  18. try {
  19. process = pb.start();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  24. String line;
  25. while ((line = in.readLine()) != null)
  26. {
  27. System.out.println(line);
  28. }
  29. }
  30.  
  31. public static void appendToPath(String pathToAppend) throws IOException {
  32. ProcessBuilder pb = new ProcessBuilder("CMD.exe", "/C", "SET"); // SET prints out the environment variables
  33. pb.redirectErrorStream(true);
  34. Map<String,String> env = pb.environment();
  35. String path = env.get("Path") + ";" + pathToAppend;
  36. env.put("Path", path);
  37. Process process = null;
  38. try {
  39. process = pb.start();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  44. String line;
  45. while ((line = in.readLine()) != null)
  46. {
  47. System.out.println(line);
  48. }
  49. }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. //and in Main:
  56.  
  57. try {
  58. EnvVarsManager.setEnvVariable("CUDA_VISIBLE_DEVICES", "0");
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
Add Comment
Please, Sign In to add comment