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
Right click on My Computer and go to properties (or) Press Windows + Pause to open up System Properties. Now traverse to Advanced Tab and click on “Environment Variable”. In order to check the classpath which is set, type echo %CLASSPATH% in command prompt, it will display the CLASSPATH which is set.
Select Start select Control Panel. double click System and select the Advanced tab. Click Environment Variables. In the section System Variables find the PATH environment variable and select it.
Classpath is an environment variable that is used by the application ClassLoader or system to locate and load the compiled Java bytecodes stored in the . class file. To set CLASSPATH. the CLASSPATH can be overridden by adding classpath in the manifest file and by using a command like set -classpath.
Now to check the value of Java classpath in windows type "echo %CLASSPATH" in your DOS command prompt and it will show you the value of the directory which is included in CLASSPATH.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With