Question

Hi I have been struggling to get the 'getenv' to work. it will keep on returning "Exception in thread "main" java.lang.UnsupportedOperationException". I have been reading about the ProcessBuilder but i am not quite sure on how and where to implement it based on my code below.

What I want to exactly do is, to set a variable ("REGRESSION_STATUS", "UPDATED") and ("REGRESSION_STATUS", "OUTDATED") when the condition is met, and return the value "UPDATED" and "OUTDATED" as appropriate when executed through the cmd in Windows.

public static void main(String[] args) throws ClassNotFoundException {
    String run_type = args[0];
    String inputFile = args[1];

    System.out.println("RUN TYPE = "  + run_type);
    System.out.println("INPUT FILE = "  + inputFile);

    MiniData data = getValue(run_type, "LEM");

    if(run_type.equals("BUILD")){
        System.out.println("Script = " + data.getScript());
    }
    else if (run_type.equals("DEPLOY")){
        System.out.println("Script = " + data.getScript());
    }
    else if (run_type.equals("REGRESSION")){
        System.out.println("Runtime Version (DB) = " + data.getRuntime());
        String file_name =inputFile;

        if(data.getRuntime().equals(getRuntimeVersion(file_name)))
        {
            System.out.println("The version is up-to-date");
            System.getenv().put("REGRESSION_STATUS", "UPDATED");
            System.getenv().put("REGRESSION_VER", data.getRuntime());   
        }
        else 
        {
            System.out.println("This version is outdated");
            System.getenv().put("REGRESSION_STATUS", "OUTDATED");
            System.getenv().put("REGRESSION_VER", data.getRuntime() );
        }
    }
    else {
        System.out.println("You have not the correct value. Enter either BUILD/DEPLOY/REGRESSION");
    }
}   

Thanks!

Was it helpful?

Solution

The System.getenv() method returns an unmodifiable view of the environment variables. You cannot use it to set environment variables like you're doing here.

The only time you can "set" environment variables is when you are creating an environment for a child process, using the ProcessBuilder class or the Runtime.exec method, but even then you are not modifying your copy of the environment.

OTHER TIPS

You must use C putenv and JNI, there is no way to do that from Java.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top