Eclipse

Eclipse Environment Variable Setup Example

In this article we will see how to set the environment variables in Eclipse. For this example we will make use of Eclipse Luna 4.4.2.

1. Introduction

Eclipse is a Java-based open source platform that allows a software developer to create a customized development environment from plug-in components built by Eclipse members. Eclipse is managed and directed by the Eclipse.org Consortium. Eclipse is the most common Integrated Development Environment (IDE) used by Java developers. The best thing that eclipse is cabable of is that it uses plugins for adding more features. So if you don’t need a feature you can simple ignore the required plugin.

Many operating systems use environment variables to pass configuration information to applications. Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters.

2. Create Simple Project

In this section we will see how to create a simple project. Then we will set the environment variable for this newly created project.

Click on File=>New=>Java Project.

Figure 1. Java Project
Figure 1. Java Project

Please note that you might need to click ‘Other… ‘ link to go to the Java Project section. In the Project name text box give the name of the project. We will use EnvironmentVariableSetup. Leave the other dafault values as it is. Click the Finish button.

Figure 2. Project Details
Figure 2. Project Details

Eclipse will create a default src folder and will include some jar files in the classpath (shown as below). Please note that the list of jar files included can be different depending on the java and Eclipse version.

Figure 3. Jar files
Figure 3. Jar files

2.1 Create New Package and Class

In this section we will see how to create a new java package in Eclipse.

Right click on the src folder and go to New=>Package. In the Java Package pop-up give the package name. We will use com.javacodegeeks. Click Finish.

Figure 4. New Package
Figure 4. New Package

Now to create a new class in this package right click on the package and choose New=>Class. Enter the Class name. You can choose some other options as well to configure the newly created class.

Figure 5. New Class
Figure 5. New Class

3. Set Environment variable

In this section we will see how to set an environment variable in eclipse. Right click on the class (SimpleClass), go to Run As=>Run Configurations… Click on the Environment Tab.

Figure 6. Run Configurations
Figure 6. Run Configurations

On the Environment Tab click on the New… button. In the Name textbox enter the name of the environment variable – ‘TEST_ME’. In the Value textbox enter ‘Tested’. Click OK.

Figure 8. Environment Setup
Figure 8. Environment Setup

Figure 7. New Environment Variable
Figure 7. New Environment Variable

In the window above you can see all the environment variables that have been defined. You can also Edit or Remove these variable from this window.

4. Use environment Variable

In this section we will see how we can use the newly defined environment variable.

On the Java platform, an application uses System.getenv to retrieve environment variable values. Without an argument, getenv returns a read-only instance of java.util.Map, where the map keys are the environment variable names, and the map values are the environment variable values. With a String argument, getenv returns the value of the specified variable. If the variable is not defined, getenv returns null

In the class previously defined create a main method. In this main method we will use the java.lang.System class to get the value of environment variable.

String envValue = System.getenv("TEST_ME");

Now we will print the value returned. Below is the full code for this class

SimpleClass.java

package com.javacodegeeks;

public class SimpleClass {

  public static void main(String... args) {
    String envValue = System.getenv("TEST_ME");
    System.out.print(envValue);
  }
}

In the console you will see that the value of the variable will get printed (Tested).

5. Conclusion

In this article we saw how to setup a simple java project and how to create a new package and class. We then saw how we can setup environment variable using the Run Configuration setting of Eclipse. Wehave also examined how to use this newly created environment variable.

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
David Paavada
David Paavada
4 years ago

How can we add multiple environment variables all together ??

Back to top button