javapathgetclasspathenvironment-variables

How Do I check PATH and CLASSPATH environment variables from java?


I am making a java program to read an audio.wav file with JMF. I have to set path from cmd every time my computer restarts like this

set CLASSPATH=%WINDIR%\java\classes\jmf.jar;%WINDIR%\java\classes\sound.jar;.;%CLASSPATH%

and

set PATH=%WINDIR%\System32;%PATH%  

otherwise the program will compile but not run I wanted to do it through

System.setProperty(key,value);

I don't know cmd commands,so in order to check the value of CLASSPATH and PATH after setting it through cmd I tried

public void checkProperty (){
    System.setProperty("temporaryvar","blahblah");
    System.out.println(""+System.getProperty("temporaryvar"));//prints out blahblah
    System.out.println(""+System.getProperty("CLASSPATH"));//prints out null
    System.out.println(""+System.getProperty("PATH"));//prints out null
}

I get it printed out as

blahblah
null
null

What's the reason I am getting the value of variable I set from the program back but not the one I set from the cmd? Is this the right approach? I need to set both these paths from java.


Solution

  • Because CLASSPATH and PATH are environment variables, not Java System Properties. System properties can be passed to your java process using -Dkey=value.

    Try using System.getenv() instead.