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

52,526

Solution 1

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.

Solution 2

Use System.getenv instead of System.getProperty. Note that you can also get the effective classpath for the current Java process with:

System.getProperty("java.class.path");

And that this value can, and in most cases will, be different from whatever your CLASSPATH environment variable is setup to be.

Share:
52,526
Soul Enrapturer
Author by

Soul Enrapturer

Updated on July 09, 2022

Comments

  • Soul Enrapturer
    Soul Enrapturer almost 2 years

    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..plz help