8

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

Soul Enrapturer
  • 367
  • 2
  • 3
  • 14

2 Answers2

25

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Did you mean the reverse of your opening sentence? Should it be: “Use `System.getProperty` instead of `System.getenv`. ”? Plus it would be helpful if you expanded with a bit more explanation. – Basil Bourque Apr 18 '16 at 01:33
11

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.

Charlie
  • 7,181
  • 1
  • 35
  • 49