Felix K.
Felix K.

Reputation: 6281

Environment variables not visible to java

The project

The project is a large C# project which is used for test-automation. For this purpose i've to use a java-tool which is the tool which saves all results into a file which can be loaded into a test-environment.

The interface

I got a DLL from the vendor of the test-environment which is build in C++, this dll loads the java environment and loads the jar files.

Current situation

The java environment is loaded with success, its configured with environment-variables set in C# with this method:

String java = GetJavaInstallationPath();
Environment.SetEnvironmentVariable("PATH", String.Format("{0};{1}", Environment.GetEnvironmentVariable("PATH"), Path.Combine(java, @"bin\client")), EnvironmentVariableTarget.Process);

After this i set the path to the java classes using this code:

Environment.SetEnvironmentVariable("ITEPCLASSPATH",
                String.Format("{0};{1}",
                Path.Combine(iTepPath, "itep.jar"),
                Path.Combine(iTepPath, "libs\\itorx.jar")), EnvironmentVariableTarget.Process);

Which actually should work, it shows the correct value when using Environment.GetEnvironmentVariable("ITEPCLASSPATH") but the C++-DLL tells me that it isn't working.

When setting the class path by using a external bat-file it works. Some more facts:

It seems that java is not accessing the env.-variable i set in C# but recognizes that i set it in the bat file.

I really need to set the variable via C#, how do i archive this?

Upvotes: 7

Views: 2154

Answers (3)

Larry Hector
Larry Hector

Reputation: 183

Java allows you to pass environment variables as parameters using the:

java -DMYPROP=MYVALUE myclass.class

argument syntax. Check out the -D flag.

Those system properties then apply to that JVM process instance. Wouldn't that be simpler than trying to modify the OS Environment?

Upvotes: 0

Emmie Lewis-Briggman
Emmie Lewis-Briggman

Reputation: 855

Make sure the environment variable works with each target: Process, User and Machine. See this MSDN article.

// Set the environment variable for the default target (the current process).
Console.WriteLine(fmt2x, "(default)", myVarA, existsA);
Environment.SetEnvironmentVariable(myVarA, existsA);

// Set the environment variable for the the current process.
Console.WriteLine(fmt2x, "Process", myVarB, existsB);
Environment.SetEnvironmentVariable(myVarB, existsB, 
    EnvironmentVariableTarget.Process);

// Set the environment variable for the the current user.
Console.WriteLine(fmt2x, "User", myVarC, existsC);
Environment.SetEnvironmentVariable(myVarC, existsC, 
    EnvironmentVariableTarget.User);

// Set the environment variable for the the local machine.
Console.WriteLine(fmt2x, "Machine", myVarD, existsD);
Environment.SetEnvironmentVariable(myVarD, existsD, 
    EnvironmentVariableTarget.Machine);

Upvotes: 1

Yves Martin
Yves Martin

Reputation: 10361

It is not explicitly written in Microsoft System.Environment documentation but the target value Process seems to limit scope to the current process only. By default, the CreateProcess method inherits current process environment for the child process. Maybe the parameters used there breaks this default behavior.

So I propose you test first with EnvironmentVariableTarget.User in SetEnvironmentVariable to see if it works better.

By the way, I think you will have to diagnose further environment variable and create process operations with tool like Process Monitor.

Upvotes: 1

Related Questions