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

2

2 Answers 2

24

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.

1
  • 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. Apr 18, 2016 at 1: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.

1
  • Please change it to getenv, I want to copy and paste without thinking :). May 6, 2014 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.