This discussion is archived
1 Reply Latest reply: Mar 26, 2013 8:35 AM by gimbal2 RSS

Runtime.exec

mjmjava Newbie
Currently Being Moderated
hello , i have 3 versions of java ie. jdk1.6.0_23 , jdk1.6.0_27 , jre6

i have removed java variable from " Path,Classpath "
i have written code for this to get version of java i am using , my code is

public static void execute(){
          Runtime runtime = Runtime.getRuntime();
          String directory = "D:\\program files\\Java\\jdk1.6.0_23\\bin";
          System.out.println("\"java -version\"");
          Process proc;
          try {
               proc = runtime.exec(" java -version ", null, new File(directory));
               BufferedReader input = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
               String s = input.readLine();
               System.out.println(s);
               String[] arr = s.split("\"");
               for(String j : arr){
                    System.out.println(j);
               }
               System.out.println(arr[1]);
               
          } catch (IOException er) {
               // TODO Auto-generated catch block
               er.printStackTrace();
          }
     }

but i am getting different version from the directory which i am giving to this program

example : if i am in jdk1.6.0_23 it is giving me version like 1.6.0_35 , why is it so , if i am executing this

Runtime.exec (" jar" , null , "new File(directory));
it is printing proper results

Regards
  • 1. Re: Runtime.exec
    gimbal2 Guru
    Currently Being Moderated
    Well based on your test results you can draw a conclusion: apparently setting the working directory does not make the working directory the primary place to look for the executable to run; it (or rather: the OS) is still picking your "default" Java installation because that is the first one it finds on its search path. You'd have to put the full path to the executable you want to invoke in the first parameter to force a specific executable to be run. I'm not sure, but I believe using the Process class in stead of the Runtime class may also help if I remember correctly from similar threads posted not too long ago.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points