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.
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.
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?
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.
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 .
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With