Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • The application is started by the bat file
  • The path is copied from my generated path of the dll
  • I don't comment anything out, so the path is still set by C#

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?

like image 995
Felix K. Avatar asked May 10 '12 07:05

Felix K.


People also ask

How do I enable environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do you read an environment variable in UNIX Java?

For obtaining only one System Variable use the following code: String sysEnvStr = System. getenv("JAVA_HOME"); If it returns null then make changes in your .

How do I check if Java environment variable is set in Windows?

Verify JAVA_HOME Open a Command Prompt window (Win⊞ + R, type cmd, hit Enter). Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder. If it doesn't, your JAVA_HOME variable was not set correctly.


2 Answers

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.

like image 53
Yves Martin Avatar answered Oct 14 '22 01:10

Yves Martin


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);
like image 1
Emmie Lewis-Briggman Avatar answered Oct 14 '22 01:10

Emmie Lewis-Briggman