tags:

views:

429

answers:

6

How to set the environment variables for java in windows (the classpath )

+3  A: 

Under Windows: http://vlaurie.com/computers2/Articles/environment.htm

Under Linux: http://lowfatlinux.com/linux-environment-variables.html

And of course, you can retrieve them from Java using:

String variable = System.getProperty("mykey");
Lastnico
+1  A: 

In programming context you can execute SET command (SET classpath=c:\java) or Right click on your computer > properties > advanced > environment variables.

In a batch file you can use

SET classpath=c:\java
java c:\myapplication.class
Cem Kalyoncu
Can not understand second line. What does it mean?
St.Shadow
@Shadow: fixed it
Cem Kalyoncu
+2  A: 

The JDK installation instructions explain exactly how to set the PATH, for different versions of Windows.

Normally you should not set the CLASSPATH environment variable. If you leave it unset, Java will look in the current directory to find classes. You can use the -cp or -classpath command line switch with java or javac.

Jesper
+1 for "Normally you should not set the CLASSPATH environment variable.".
BalusC
A: 

For deployment better to set up classpath exactly and keep environment clear. Or at *.bat (the same for linux, but with correct variables symbols):

CLASSPATH="c:\lib;d:\temp\test.jar;<long classpath>"
CLASSPATH=%CLASSPATH%;"<another_logical_droup_of_classpath" 
java -cp %CLASSPATH% com.test.MainCLass

Or at command line or *.bat (for *.sh too) if classpath id not very long:

java -cp "c:\lib;d:\temp\test.jar;<short classpath>"
St.Shadow
+6  A: 

In Windows inorder to set

Step 1 : Right Click on MyComputer and click on properties .

Step 2 : Click on Advanced tab

alt text

Step 3: Click on Environment Variables

alt text

Step 4: Create a new class path for JAVA_HOME

alt text

Step 5: Enter the Variable name as JAVA_HOME and the value to your jdk bin path ie c://Programfiles/Java/jdk-1.6/bin and

NOTE Make sure u start with .; in the Value so that it doesn't corrupt the other environment variables which is already set.

alt text

Step 6 : Follow the Above step and edit the Path in System Variables add the following ;c://Programfiles/Java/jdk-1.6/bin in the value column.

Step 7 :Your are done setting up your environment variables for your Java , In order to test it go to command prompt and type

 java

who will get a list of help doc

In order make sure whether compiler is setup Type in cmd

  javac

who will get a list related to javac

Hope this Helps !

YetAnotherCoder
Nice explanation. One small thing in step 6: Don't type `$java` or `$javac`, but just `java` or `javac`.
Jesper
And note that you must add the JDK bin directory to the `PATH` environment variable; just setting `JAVA_HOME` is not enough.
Jesper
yeah sorry since im a Linux lover . i missed it will change it , Thanks
YetAnotherCoder
A: 

Keep in mind that the %CLASSPATH% environment variable is ignored when you use java/javac in combination with one of the -cp, -classpath or -jar arguments. It is also ignored in an IDE like Netbeans/Eclipse/IntelliJ/etc. It is only been used when you use java/javac without any of the aforementioned arguments.

In case of JAR files, the classpath is to be definied as class-path entry in the manifest.mf file. It can be definied semicolonseparated and relative to the JAR file's root.

In case of an IDE, you have the so-called 'build path' which is basically the classpath which is used at both compiletime and runtime. To add external libraries you usually drop the JAR file in a (either precreated by IDE or custom created) lib folder of the project which is added to the project's build path.

BalusC
@Balus he is a begginer who is trying to set his java Environment on Windows PC .
YetAnotherCoder
Exactly. The one who would get greatly confused when he discovers that the classpath he configured doesn't work for JAR files nor IDE's. Just a warning at its place :)
BalusC