Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 860
Soul Enrapturer Avatar asked Dec 20 '12 06:12

Soul Enrapturer


People also ask

How do I find my Java CLASSPATH?

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.

How do I find my Environment Variables in PATH?

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.

What is CLASSPATH environment variable in Java?

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.

How do I find the CLASSPATH directory?

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.


2 Answers

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.

like image 198
Perception Avatar answered Nov 13 '22 07:11

Perception


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.

like image 33
Charlie Avatar answered Nov 13 '22 08:11

Charlie