Environment variables not visible to java

Project

The project is a large C # project that is used to automate testing. To do this, I must use a java tool, which is a tool that saves all the results to a file that can be loaded into the test environment.

Interface

I got a DLL from a test environment provider that is built in C ++, this DLL loads the java environment and loads the jar files.

Current situation

The java environment was loaded successfully, setting it up using environment variables set in C # using this method:

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

After that, 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); 

What actually should work, it shows the correct value when using Environment.GetEnvironmentVariable("ITEPCLASSPATH") , but the C ++ - DLL tells me that it is not working.

When setting the class path using an external bat file, it works. Some more facts:

  • The application is launched by the bat file.
  • The path is copied from my generated dll path
  • I am not commenting anything, so the path is still set by C #

It seems that java does not have access to the env.-i variable set in C #, but recognizes that I installed it in the bat file.

I really need to set a variable via C #, how to do this?

+7
source share
3 answers

This is not explicitly stated in the Microsoft System.Environment Documentation , but the Process target value appears to limit the scope to the current process only. By default, the CreateProcess method inherits the current process environment for the child process. Perhaps the settings used here violate this default behavior.

Therefore, I suggest that you first test EnvironmentVariableTarget.User in SetEnvironmentVariable to see if it works better.

By the way, I think you will have to diagnose an additional environment variable and create processes using a tool such as Process Monitor .

+1
source

Make sure the environment variable works for every purpose: process, user, and machine. See 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); 
+1
source

Java allows you to pass environment variables as parameters using:

 java -DMYPROP=MYVALUE myclass.class 

syntax of arguments. Check the -D flag.

These system properties are then applied to this instance of the JVM process. Wouldn't that be easier than trying to change the OS environment?

0
source

All Articles